Documentation
¶
Overview ¶
Package anthropic implements the Anthropic Messages API client (design doc §7.1, issue #69).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIError ¶ added in v0.4.6
type APIError struct {
StatusCode int
Body string
// RetryAfter is the wait parsed from Retry-After. HasRetryAfter is true when the header was
// present (including "0", meaning retry immediately) so a missing header can still take the
// exponential path (issue #532).
RetryAfter time.Duration
HasRetryAfter bool
}
APIError is a non-2xx Messages API response. It preserves the status code so a caller can attach diagnostic context on a 4xx (a request the provider rejected — issue #524) while keeping the same "anthropic: HTTP <code>: <body>" Error() string the adapter has always surfaced.
func (*APIError) IsClientError ¶ added in v0.4.6
IsClientError reports whether the status is a non-retryable 4xx — a request the provider rejected as malformed or unauthorized, as opposed to a 429/5xx/transport failure that a retry might clear.
func (*APIError) IsRetryable ¶ added in v0.4.7
IsRetryable reports whether the status is a transient rate-limit / overload that should be retried (429 and Anthropic/Cloudflare 529), rather than treated as a fatal client error (issue #532).
type ChatMessage ¶
type ChatMessage struct {
Role string
Content string
Blocks []ContentBlock
}
ChatMessage is one user or assistant turn (roles user|assistant only). Plain text uses Content (JSON string). Tool turns set Blocks (JSON array).
func (ChatMessage) MarshalJSON ¶
func (m ChatMessage) MarshalJSON() ([]byte, error)
MarshalJSON encodes Content as a string or Blocks as a content-block array.
type Client ¶
type Client struct {
APIKey string
BaseURL string
// WorkspaceID, when set, is sent as the anthropic-workspace-id header. It is
// required when authenticating with an identity-linked API key (which is not
// scoped to a single workspace); a plain workspace-scoped key leaves it empty.
WorkspaceID string
HTTPClient *http.Client
}
Client calls POST /v1/messages.
func (*Client) Generate ¶
Generate performs one non-streaming Messages request. System may be empty. Tools / tool_choice are omitted when unset. ToolCalls are populated only when StopReason is tool_use.
HTTP 429 and 529 are retried with Retry-After (when present) or exponential backoff; genuine 4xx (400/401/403/404/422) stay fatal (issue #532).
type ContentBlock ¶
type ContentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
ToolUseID string `json:"tool_use_id,omitempty"`
Content string `json:"content,omitempty"`
// IsError, on a tool_result block, marks the call as failed (Messages API tool_result.is_error)
// so the model reads the content as an error observation. It is emitted only for tool_result
// blocks and only when true (issue #524).
IsError bool `json:"is_error,omitempty"`
}
ContentBlock is a Messages API content block (text, tool_use, or tool_result).
func (ContentBlock) MarshalJSON ¶
func (b ContentBlock) MarshalJSON() ([]byte, error)
MarshalJSON keeps tool_result.content present even when empty. Anthropic requires that field; omitempty would drop a successful empty tool output.
type OutputConfig ¶ added in v0.4.2
type OutputConfig struct {
Format *OutputFormat `json:"format,omitempty"`
}
OutputConfig is the Messages API output_config object. Only the structured-output format is modeled.
type OutputFormat ¶ added in v0.4.2
type OutputFormat struct {
Type string `json:"type"`
Schema json.RawMessage `json:"schema"`
}
OutputFormat is output_config.format for JSON structured outputs: type "json_schema" plus the JSON Schema the completion must conform to. The model returns the JSON in a normal text block.
type Request ¶
type Request struct {
Model string
System string
Messages []ChatMessage
Tools []Tool
ToolChoice *ToolChoice
// MaxTokens is the max_tokens sent; a non-positive value falls back to defaultMaxTok (issue #514).
MaxTokens int
// Temperature is sent verbatim when non-nil; nil leaves the Messages API default (issue #388).
Temperature *float64
// OutputConfig requests structured output (output_config.format); nil leaves it unset (issue #510).
OutputConfig *OutputConfig
}
Request is one non-streaming Messages API call.
type Response ¶
type Response struct {
Text string
ToolCalls []ToolUse
StopReason string
InputTokens int
OutputTokens int
DurationMs int64
}
Response is a decoded Messages API result.
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema json.RawMessage `json:"input_schema"`
}
Tool is an Anthropic custom tool (name + JSON Schema input_schema).
type ToolChoice ¶
type ToolChoice struct {
Type string `json:"type"`
}
ToolChoice is the Messages API tool_choice object (type auto|any|none).