Documentation
¶
Overview ¶
Package anthropic implements chat.Client with the Anthropic Messages API.
Index ¶
- Constants
- type APIKeyProvider
- type Auth
- type Client
- func (c *Client) DeleteFile(ctx context.Context, id string) error
- func (c *Client) Info() chat.ModelInfo
- func (c *Client) Stream(ctx context.Context, req chat.Request) iter.Seq[chat.Event]
- func (c *Client) UploadFile(ctx context.Context, name, mimeType string, r io.Reader, ttl time.Duration) (chat.ContentBlock, error)
- type Compat
- type Config
- type Endpoint
- type Model
- type PrefixMismatch
- type ToolSearch
Constants ¶
const (
// API is the protocol this client speaks, see chat.ModelInfo.API.
API = "anthropic-messages"
)
const DefaultMaxTokens = 32_000
DefaultMaxTokens is the max_tokens of a request that sets no MaxTokens to a model that declares no MaxOutputTokens. The protocol requires the field, and the framework does not know the limits of models.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIKeyProvider ¶
APIKeyProvider resolves the API key dynamically, for short-lived tokens.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps the Anthropic SDK.
func New ¶
New creates an Anthropic chat.Client. It returns an error when cfg.Model.Name is empty or cfg.Endpoint.Auth is unknown.
func (*Client) DeleteFile ¶
DeleteFile deletes an uploaded file. Messages in history that still refer to it fail on the Anthropic side, so drop or replace those references first, or keep a fallback on the blocks.
func (*Client) Stream ¶
Stream sends a streaming request. Tool call arguments are assembled inside the provider before they are emitted.
func (*Client) UploadFile ¶
func (c *Client) UploadFile(ctx context.Context, name, mimeType string, r io.Reader, ttl time.Duration) (chat.ContentBlock, error)
UploadFile uploads a file to the Files API and returns a media block referring to it, ready to put in a message. A ttl of 0 keeps the file until it is deleted; otherwise it must be between one hour and 90 days. The returned block has no fallback: to keep the session usable on other models, set its URL or data too.
type Compat ¶
type Compat struct {
// NoMedia means media input is not accepted; media is replaced with a note before the request is sent.
NoMedia bool
// NoAdaptiveThinking means only budget thinking is known (thinking.type=enabled with budget_tokens), not
// adaptive thinking with output_config.effort.
NoAdaptiveThinking bool
// NoDisableThinking means thinking cannot be turned off: chat.ReasoningOff asks for the lowest effort instead,
// and forced tool calls, which do not go with thinking, are rejected before the request is sent.
NoDisableThinking bool
// NoXHigh means effort has no xhigh; high is used instead.
NoXHigh bool
// NoMaxEffort means effort has no max; xhigh is used instead, or high with NoXHigh.
NoMaxEffort bool
// NoThinkingBinding means thinking.block_binding and its beta header are not accepted, so Config.PrefixMismatch
// is not sent and the service's own default applies.
NoThinkingBinding bool
// NoThinkingSignature means thinking blocks carry no signature and are replayed as they are; otherwise unsigned
// thinking blocks are replayed as text, because Anthropic rejects them.
NoThinkingSignature bool
// NoStructuredOutput means output_config.format is not supported; the format requirements go into the system
// prompt instead.
NoStructuredOutput bool
// NoToolSearch means there is no server-side tool search, so deferred tools (chat.ToolDef.Deferred) are sent up front.
NoToolSearch bool
}
Compat lists what is missing compared with the full Anthropic Messages API, as its current models speak it. The zero value lacks nothing. It is set on an Endpoint for what holds for every model of the service and on a Model for what holds for that model only; a Client lacks whatever either of them lacks.
type Config ¶
type Config struct {
// Endpoint is the service the client talks to: the zero value is Anthropic's own, and a compatible
// endpoint is described by filling one in.
Endpoint Endpoint
// Model is the model the client talks to; its Name must be set. Another model is another Client.
Model Model
APIKey string
APIKeyProvider APIKeyProvider
Timeout time.Duration
MaxRetries int
Headers map[string]string
HTTPClient *http.Client
// ToolSearch picks the server-side tool search used when a request has deferred tools (chat.ToolDef.Deferred):
// ToolSearchRegex by default. ToolSearchOff, or Compat.NoToolSearch, sends every tool up front instead.
ToolSearch ToolSearch
// PrefixMismatch says what the API does with a replayed thinking block whose conversation changed since it was produced,
// on models that bind thinking to the conversation: PrefixMismatchDrop by default. Endpoints and models that do not
// accept the setting say so with Compat.NoThinkingBinding.
PrefixMismatch PrefixMismatch
// FileScope names the Files API store this key reads, see chat.FileRef.Scope. It defaults to "anthropic" for the
// official endpoint and to none for a custom BaseURL; set it per workspace when keys of several workspaces are in use.
FileScope string
// HTTPHooks are debugging hooks at the raw HTTP layer.
HTTPHooks chat.HTTPHooks
}
Config configures the Anthropic Client.
type Endpoint ¶
type Endpoint struct {
// Provider names the vendor in messages, usage and errors; "anthropic" by default.
Provider string
// BaseURL is the address of the API; empty for Anthropic's.
BaseURL string
// Auth is how the API key is sent; AuthAPIKey by default.
Auth Auth
// Compat says where every model of the endpoint differs from the full protocol; see Compat.
Compat Compat
}
Endpoint is a service that speaks the Anthropic Messages API: Anthropic itself (the zero value) or a compatible one. It says where the service is and how to reach it; the model served from it is described by Model.
type Model ¶ added in v0.3.0
type Model struct {
// Name is the model name sent to the API.
Name string
// MaxInputTokens is the context limit of the model; 0 declares none, and the runner then never compacts history.
MaxInputTokens int
// MaxOutputTokens is the output limit of the model. It is used as max_tokens when a request sets no MaxTokens;
// 0 declares none and DefaultMaxTokens is used instead.
MaxOutputTokens int
// Compat says where this model differs from the full protocol; see Compat.
Compat Compat
}
Model is the model a Client talks to. Beyond the zero value only Name must be set: a model is taken to support the whole protocol, and one that does not says what it lacks in Compat.
type PrefixMismatch ¶
type PrefixMismatch string
PrefixMismatch is the handling of a replayed thinking block that fails the API's conversation check: the system prompt, tools or earlier messages changed since the block was produced, for example after compaction or when run-scoped context is left out of later runs. Enforced accounts reject such a request by default.
const ( // PrefixMismatchDrop has the API drop the failing blocks and answer without that reasoning; it is the default. // In testing only the stale blocks were dropped: those produced after the change stayed valid. PrefixMismatchDrop PrefixMismatch = "drop_block" // PrefixMismatchError rejects the request, for finding where history is edited. PrefixMismatchError PrefixMismatch = "error" )
type ToolSearch ¶
type ToolSearch string
ToolSearch is the variant of Anthropic's server-side tool search.
const ( // ToolSearchRegex searches with regular expressions written by the model; it is the default. It matches substrings, // so it finds tools described in Chinese and tools sharing a name prefix such as mcp__{server}__. ToolSearchRegex ToolSearch = "regex" // ToolSearchBM25 searches with natural language queries. It splits words on spaces, so in testing it did not find // tools described in Chinese. ToolSearchBM25 ToolSearch = "bm25" // ToolSearchOff disables tool search: deferred tools are sent up front like any other. ToolSearchOff ToolSearch = "off" )