Skip to content

Anthropic

The Anthropic provider supports the Claude family of models, including Claude 3.5 Sonnet and Haiku.

1. Setup & Authentication

Set the ANTHROPIC_API_KEY environment variable.

Terminal window
export ANTHROPIC_API_KEY='your-api-key-here'

2. Basic Usage

import (
"github.com/masterkeysrd/loom/llm"
"github.com/masterkeysrd/loom/providers/anthropic"
)
provider, err := loomanthropic.NewDefaultProvider()
if err != nil {
// handle error
}
model, err := llm.NewModel(provider, "claude-3-5-sonnet-latest", nil)
if err != nil {
log.Fatal(err)
}

3. Advanced Features

Thinking (Reasoning) Mode

Anthropic models support a dedicated “thinking” budget for extended reasoning.

model = model.WithThinking(4000) // 4000 tokens for internal reasoning

Adaptive Thinking

Enable the model to dynamically adjust its thinking process.

model = model.WithAdaptiveThinking()

Prompt Caching

Anthropic support “Prompt Caching” via ephemeral markers. Loom provides a structured way to leverage this at two levels:

1. Caching the “Header” or Auto-Caching (Call Extension)

This is the most common pattern. It caches the static parts of your request (system prompt and tool definitions) so they are reused across turns, or auto-caches the last cacheable block using top-level cache control.

// Cache the static invariant header (tools and system prompt)
model = model.WithExtension(loomanthropic.PromptCaching{
CacheHeader: true,
})
// Or auto-cache the entire conversation up to the last message turn
model = model.WithExtension(loomanthropic.PromptCaching{
Auto: true,
})

2. Manual Breakpoints (Message Extension)

You can mark specific messages in the conversation as cache checkpoints using Message Extensions.

// Add a breakpoint to this specific turn using the fluent API
msg := message.NewUserText("Analyze this 500-page PDF...").
WithExtension(&loomanthropic.MessageCache{Enabled: true})

Server-Side Tool Search & Deferred Tools

Modern Claude models support native server-side tool search and deferred tool loading. This allows you to define hundreds of tools without consuming prompt tokens upfront:

// 1. Mark tools as deferred
calcTool, _ := tool.New("calculate_mortgage", "...", "...", handler, tool.WithDeferLoading())
// 2. Enable server-side tool search (BM25 or Regex)
model = model.
BindTools(calcTool).
WithExtension(loomanthropic.ToolSearch{
Strategy: loomanthropic.ToolSearchBM25, // or loomanthropic.ToolSearchRegex
})

Mid-Conversation Tool Injection

You can dynamically activate or define tools mid-conversation without pre-registering them:

  1. By Reference (ToolReferenceBlock): Activates a pre-registered deferred tool:
// Claude activates the tool schema via the inline-tools-2026-09-15 beta header
msg := message.NewSystem(&message.ToolReferenceBlock{ToolName: "convert_currency"})
  1. By Value (ToolDefinitionBlock / tool.ToBlock()): Injects a complete tool definition inline by value:
calcTool, _ := tool.New("calculate_mortgage", "...", "...", handler)
msg := message.NewUser(
&message.TextBlock{Text: "Please run this calculation:"},
calcTool.ToBlock(), // *message.ToolDefinitionBlock
)

Model Support

  • Native Tool Search & Deferred Tools: Claude 4.5+ and Claude 5 families (claude-sonnet-4-6, claude-opus-4-6, claude-haiku-4-5, claude-opus-4-8, claude-sonnet-5, claude-fable-5, etc.).
  • Native Mid-Conversation Inline Tool Additions: Claude 4.8 (claude-opus-4-8, claude-opus-4.8), Claude 5 (claude-opus-5, claude-sonnet-5-5, claude-haiku-5-5, claude-fable-5, claude-mythos-5).
  • Fallback for Older Models: For Claude 3.5 Sonnet, Claude 4.6, or other earlier models, attach llm.DeferredToolsMiddleware(nil) and/or llm.InlineToolsMiddleware(nil) to your model. The middlewares handle client-side filtering, schema injection, and type coercion.

4. Model Catalog & Live Discovery

The provider maintains a curated static catalog with pricing and limits, and also implements llm.ModelLister against Anthropic’s /v1/models and /v1/models/{model_id} endpoints (mapping live max_input_tokens, max_tokens, modalities, thinking, and effort levels while preserving static pricing):

// 1. Static in-memory lookup
prof, found := provider.GetProfile("claude-sonnet-4-6")
if found {
fmt.Println("Context Limit:", prof.Limits.Context)
}
// 2. Live API discovery & single-model lookup
models, err := provider.ListModels(ctx)
liveProf, err := provider.GetModel(ctx, "claude-sonnet-4-6")