Documentation
¶
Index ¶
- Constants
- Variables
- func EphemeralCache() *llms.CacheControl
- func EphemeralCacheOneHour() *llms.CacheControl
- func MapError(err error) error
- func WithBetaHeader(header string) llms.CallOption
- func WithCacheStrategy(strategy CacheStrategy) llms.CallOption
- func WithExtendedOutput() llms.CallOption
- func WithInterleavedThinking() llms.CallOption
- func WithPromptCaching() llms.CallOption
- type CacheStrategy
- type CachedContent
- type ErrModelRefusal
- type LLM
- type Option
- func WithAnthropicBetaHeader(value string) Option
- func WithBaseURL(baseURL string) Option
- func WithDefaultCacheStrategy(strategy CacheStrategy) Option
- func WithHTTPClient(client anthropicclient.Doer) Option
- func WithLegacyTextCompletionsAPI() Option
- func WithModel(model string) Option
- func WithToken(token string) Option
- type ToolResult
Constants ¶
const ( RoleUser = "user" RoleAssistant = "assistant" RoleSystem = "system" )
const MaxTokensAnthropicSonnet35 = "max-tokens-3-5-sonnet-2024-07-15" //nolint:gosec // This is not a sensitive value.
MaxTokensAnthropicSonnet35 is the header value for specifying the maximum number of tokens when using the Anthropic Sonnet 3.5 model.
Variables ¶
var ( ErrEmptyResponse = errors.New("no response") ErrMissingToken = errors.New("missing the Anthropic API key, set it in the ANTHROPIC_API_KEY environment variable") ErrUnexpectedResponseLength = errors.New("unexpected length of response") ErrInvalidContentType = errors.New("invalid content type") ErrUnsupportedMessageType = errors.New("unsupported message type") ErrUnsupportedContentType = errors.New("unsupported content type") )
Functions ¶
func EphemeralCache ¶
func EphemeralCache() *llms.CacheControl
EphemeralCache creates a standard ephemeral cache control for Anthropic with 5-minute duration.
func EphemeralCacheOneHour ¶
func EphemeralCacheOneHour() *llms.CacheControl
EphemeralCacheOneHour creates a 1-hour ephemeral cache control for Anthropic.
func WithBetaHeader ¶
func WithBetaHeader(header string) llms.CallOption
WithBetaHeader adds a custom beta header for accessing Anthropic's experimental features. This is useful for testing new features before dedicated support is added.
Usage:
llm.GenerateContent(ctx, messages,
anthropic.WithBetaHeader("new-feature-2025-01-01"),
)
func WithCacheStrategy ¶
func WithCacheStrategy(strategy CacheStrategy) llms.CallOption
WithCacheStrategy enables automatic cache control placement based on strategy. This option applies to a single GenerateContent/Call invocation.
Example for AI agent with many tools:
llm.GenerateContent(ctx, messages,
anthropic.WithCacheStrategy(anthropic.CacheStrategy{
CacheTools: true, // Cache tool definitions
CacheSystem: true, // Cache system prompt
}),
)
func WithExtendedOutput ¶
func WithExtendedOutput() llms.CallOption
WithExtendedOutput enables 128K token output for Claude 3.7+. Standard models are limited to 8K tokens, but this beta feature allows generating much longer responses.
Usage:
llm.GenerateContent(ctx, messages,
llms.WithMaxTokens(50000),
anthropic.WithExtendedOutput(),
)
func WithInterleavedThinking ¶
func WithInterleavedThinking() llms.CallOption
WithInterleavedThinking enables thinking between tool calls for Claude 3.7+. This allows the model to use reasoning tokens to plan tool usage and interpret results.
Usage:
llm.GenerateContent(ctx, messages,
llms.WithTools(tools),
llms.WithThinkingMode(llms.ThinkingModeMedium),
anthropic.WithInterleavedThinking(),
)
func WithPromptCaching ¶
func WithPromptCaching() llms.CallOption
WithPromptCaching enables Anthropic's prompt caching feature. This allows frequently-used prompts and system messages to be cached for improved performance and reduced costs.
Usage:
llm.GenerateContent(ctx, messages,
anthropic.WithPromptCaching(),
)
Types ¶
type CacheStrategy ¶
type CacheStrategy struct {
// CacheTools enables caching for tool definitions (placed after last tool).
CacheTools bool
// CacheSystem enables caching for system messages (placed after system content).
CacheSystem bool
// CacheMessages enables caching for conversation history (placed after last message).
CacheMessages bool
// TTL specifies cache duration ("5m" or "1h"). Defaults to "5m" if empty.
TTL string
}
CacheStrategy defines where to apply automatic caching.
type CachedContent ¶
type CachedContent struct {
llms.ContentPart
CacheControl *llms.CacheControl `json:"cache_control,omitempty"`
}
CachedContent represents content with caching instructions for Anthropic. This wraps any ContentPart and adds cache control metadata.
func WithCacheControl ¶
func WithCacheControl(content llms.ContentPart, control *llms.CacheControl) CachedContent
WithCacheControl wraps content with cache control instructions for Anthropic. This allows explicit control over what content should be cached.
Usage:
anthropic.WithCacheControl(
llms.TextPart("long context..."),
anthropic.EphemeralCache(),
)
type ErrModelRefusal ¶
type ErrModelRefusal struct {
Message string
Category string
Explanation string
InputTokens int
OutputTokens int
}
ErrModelRefusal is returned when the model declines to respond (Anthropic stop_reason "refusal"), e.g. a creative model such as Claude Fable 5 hitting a content boundary. It is distinct from an empty or failed response; any refusal text the model returned is in Message. Category and Explanation carry the API's stop_details so callers can pick a fallback by classifier; InputTokens and OutputTokens preserve billed usage (a refusal is still billed for what ran).
func (*ErrModelRefusal) Error ¶
func (e *ErrModelRefusal) Error() string
type LLM ¶
func (*LLM) GenerateContent ¶
func (o *LLM) GenerateContent(ctx context.Context, messages []llms.MessageContent, options ...llms.CallOption) (resp *llms.ContentResponse, err error)
GenerateContent implements the Model interface.
type Option ¶
type Option func(*options)
func WithAnthropicBetaHeader ¶
WithAnthropicBetaHeader adds the Anthropic Beta header to support extended options.
func WithBaseURL ¶
WithBaseUrl passes the Anthropic base URL to the client. If not set, the default base URL is used.
func WithDefaultCacheStrategy ¶
func WithDefaultCacheStrategy(strategy CacheStrategy) Option
WithDefaultCacheStrategy sets the default caching strategy for all requests made by this client. This strategy will be applied to all GenerateContent/Call invocations unless overridden by a call-level WithCacheStrategy option.
This is safe and cost-effective when: - You have stable tools that don't change (CacheTools: true saves 90% on tools from 2nd request) - You use the same system prompt across conversations (CacheSystem: true) - You want automatic conversation history caching (CacheMessages: true for multi-turn)
Example for AI agent with stable tools:
llm, err := anthropic.New(
anthropic.WithDefaultCacheStrategy(anthropic.CacheStrategy{
CacheTools: true, // Tools cached once, reused forever
CacheSystem: true, // System prompt cached once
}),
)
Call-level strategies override client-level on a per-field basis.
func WithHTTPClient ¶
func WithHTTPClient(client anthropicclient.Doer) Option
WithHTTPClient allows setting a custom HTTP client. If not set, the default value is http.DefaultClient.
func WithLegacyTextCompletionsAPI ¶
func WithLegacyTextCompletionsAPI() Option
WithLegacyTextCompletionsAPI enables the use of the legacy text completions API.