Documentation
¶
Index ¶
- Constants
- Variables
- func BuildRequestBytes(reqOpts RequestOptions) ([]byte, error)
- func CalculateCost(model string, usage *llm.Usage) float64
- func DefaultOptions() []llm.Option
- func FillCost(model string, usage *llm.Usage)
- func ParseStream(ctx context.Context, body io.ReadCloser, opts ParseOpts) llm.Stream
- func ParseStreamWith(ctx context.Context, body io.ReadCloser, pub llm.Publisher, opts ParseOpts)
- func PublishRequestParams(pub llm.Publisher, opts ParseOpts)
- type CacheControl
- type ContentBlock
- type ContentBlockDelta
- type ContentBlockDeltaEvent
- type ContentBlockStartEvent
- type ContentBlockStopEvent
- type CostFn
- type Message
- type MessageContent
- type MessageContentBlock
- type MessageContentBlockX
- type MessageDelta
- type MessageDeltaEvent
- type MessageStartEvent
- type MessageStartPayload
- type MessageStopEvent
- type MessageUsage
- type OutputUsage
- type ParseOpts
- type Provider
- func (p *Provider) CountTokens(_ context.Context, req tokencount.TokenCountRequest) (*tokencount.TokenCount, error)
- func (p *Provider) CreateStream(ctx context.Context, opts llm.Request) (llm.Stream, error)
- func (p *Provider) Models() llm.Models
- func (p *Provider) Name() string
- func (p *Provider) Resolve(modelID string) (llm.Model, error)
- type Request
- type RequestOptions
- type StreamErrorEvent
- type StreamErrorPayload
- type SystemBlocks
- type TextBlock
- type ThinkingBlock
- type ThinkingConfig
- type ToolDefinition
- type ToolResultBlock
- type ToolUseBlock
Constants ¶
const ( // Claude 4.6 (current). ModelOpus = "claude-opus-4-6" ModelSonnet = "claude-sonnet-4-6" // Claude 4.5 (Haiku latest). ModelHaiku = "claude-haiku-4-5-20251001" )
Model ID constants for programmatic use.
const ( // AnthropicVersion is the Anthropic API version header value used by all // providers that speak the Anthropic API (anthropic, claude). AnthropicVersion = "2023-06-01" )
Variables ¶
var ModelAliases = map[string]string{ "opus": ModelOpus, "sonnet": ModelSonnet, "haiku": ModelHaiku, }
ModelAliases maps short alias names to full model IDs. These are used by the auto package for provider-prefixed resolution (e.g., "claude/sonnet").
Functions ¶
func BuildRequestBytes ¶ added in v0.29.0
func BuildRequestBytes(reqOpts RequestOptions) ([]byte, error)
BuildRequestBytes builds a JSON Request body for the Anthropic API.
func CalculateCost ¶ added in v0.16.0
CalculateCost computes the total cost in USD for the given usage and model. Returns 0 if the model is unknown.
func DefaultOptions ¶ added in v0.12.0
DefaultOptions returns the default options for Anthropic.
func FillCost ¶ added in v0.21.0
FillCost calculates the cost for the given usage and model and populates both the total Cost field and the granular breakdown fields on the usage struct. No-op if usage is nil or the model is unknown.
func ParseStream ¶ added in v0.16.0
ParseStream reads an Anthropic-format SSE response body in a background goroutine and returns a stream of structured events.
Ownership: ParseStream takes ownership of body and closes it when the stream ends or ctx is cancelled.
func ParseStreamWith ¶ added in v0.35.0
ParseStreamWith starts parsing on an existing publisher. The caller is responsible for having already created the publisher and optionally published early events (e.g. RequestEvent) before the HTTP call.
Ownership: takes ownership of body and closes pub when done.
func PublishRequestParams ¶ added in v0.35.0
PublishRequestParams emits a RequestEvent at the start of every stream. Exported so that providers using ParseStreamWith can call it before the HTTP request.
Types ¶
type CacheControl ¶ added in v0.29.0
type CacheControl struct {
Type string `json:"type"` // always "ephemeral"
TTL string `json:"ttl,omitempty"` // "1h" for extended TTL; omit for default 5m
}
CacheControl is the Anthropic API wire type for cache breakpoints.
type ContentBlock ¶ added in v0.29.0
type ContentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
ContentBlock describes the type and initial state of a content block.
type ContentBlockDelta ¶ added in v0.29.0
type ContentBlockDelta struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
PartialJSON string `json:"partial_json,omitempty"`
Thinking string `json:"thinking,omitempty"`
// Signature is the cryptographic verification token emitted by a
// signature_delta event at the end of a ThinkingConfig block. It must be
// passed back verbatim to the API in subsequent requests.
Signature string `json:"signature,omitempty"`
}
ContentBlockDelta is the delta payload within a ContentBlockDeltaEvent.
type ContentBlockDeltaEvent ¶ added in v0.29.0
type ContentBlockDeltaEvent struct {
Index int `json:"index"`
Delta ContentBlockDelta `json:"delta"`
}
ContentBlockDeltaEvent carries an incremental delta within a content block.
type ContentBlockStartEvent ¶ added in v0.29.0
type ContentBlockStartEvent struct {
Index int `json:"index"`
ContentBlock ContentBlock `json:"content_block"`
}
ContentBlockStartEvent marks the beginning of a content block (text, tool_use, ThinkingConfig).
type ContentBlockStopEvent ¶ added in v0.29.0
type ContentBlockStopEvent struct {
Index int `json:"index"`
}
ContentBlockStopEvent signals the end of a content block.
type CostFn ¶ added in v0.29.0
CostFn calculates token costs for a given model and populates the cost fields on usage. Providers that reuse ParseStream with different pricing (e.g. MiniMax) supply their own CostFn instead of wrapping the publisher.
type Message ¶ added in v0.29.0
type Message struct {
Role string `json:"role"`
Content MessageContent `json:"content"`
}
type MessageContent ¶ added in v0.29.0
type MessageContent []MessageContentBlock
type MessageContentBlock ¶ added in v0.29.0
type MessageContentBlock interface {
// contains filtered or unexported methods
}
type MessageContentBlockX ¶ added in v0.29.0
type MessageContentBlockX struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
// === ToolUse Request ===
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input map[string]any `json:"input,omitempty"` // No omitempty — Anthropic requires this field
ToolUseID string `json:"tool_use_id,omitempty"`
Content string `json:"content,omitempty"`
IsError bool `json:"is_error,omitempty"`
CacheControl *CacheControl `json:"cache_control,omitempty"`
}
type MessageDelta ¶ added in v0.29.0
type MessageDelta struct {
StopReason string `json:"stop_reason"`
}
MessageDelta is the delta payload of a MessageDeltaEvent.
type MessageDeltaEvent ¶ added in v0.29.0
type MessageDeltaEvent struct {
Delta MessageDelta `json:"delta"`
Usage OutputUsage `json:"usage"`
}
MessageDeltaEvent carries the stop reason and final output token count.
type MessageStartEvent ¶ added in v0.29.0
type MessageStartEvent struct {
Message MessageStartPayload `json:"message"`
}
MessageStartEvent is emitted at the beginning of a streaming response.
type MessageStartPayload ¶ added in v0.29.0
type MessageStartPayload struct {
ID string `json:"id"`
Model string `json:"model"`
Usage MessageUsage `json:"usage"`
}
MessageStartPayload is the payload of a MessageStartEvent.
type MessageStopEvent ¶ added in v0.29.0
type MessageStopEvent struct{}
MessageStopEvent signals that the message stream is complete. It carries no payload.
type MessageUsage ¶ added in v0.29.0
type MessageUsage struct {
InputTokens int `json:"input_tokens"`
CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
CacheReadInputTokens int `json:"cache_read_input_tokens"`
}
MessageUsage carries input token counts from the message_start event.
type OutputUsage ¶ added in v0.29.0
type OutputUsage struct {
OutputTokens int `json:"output_tokens"`
}
OutputUsage carries output token counts from the message_delta event.
type ParseOpts ¶ added in v0.29.0
type ParseOpts struct {
Model string
// ProviderName is used in ModelResolvedEvent.Resolver when the API returns
// a different model than was requested. Set to the provider's own name
// (e.g. "anthropic", "claude", "minimax").
ProviderName string
// CostFn overrides the default Anthropic cost calculation.
// When nil, FillCost (Anthropic pricing) is used.
CostFn CostFn
// ResponseHeaders contains HTTP response headers, used to extract rate-limit info.
// Keys should be lowercase header names.
ResponseHeaders map[string]string
// RequestParams, when non-zero, is published as a RequestEvent at
// the beginning of the stream. Build it with llm.ProviderRequestFromHTTP
// after constructing the outgoing *http.Request.
RequestParams llm.ProviderRequest
// LLMRequest is the final llm.Request used to build the provider request.
// Included in the RequestEvent for observability.
LLMRequest llm.Request
}
ParseOpts configures how ParseStream processes an Anthropic-format SSE body.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider implements the direct Anthropic API backend.
func (*Provider) CountTokens ¶ added in v0.24.0
func (p *Provider) CountTokens(_ context.Context, req tokencount.TokenCountRequest) (*tokencount.TokenCount, error)
CountTokens estimates the number of input tokens for the given Request.
This is a local approximation using the cl100k_base BPE encoding — no network call is made. Anthropic's tokenizer is proprietary and not publicly available; cl100k_base gives ±5–10% accuracy for English text.
For exact counts, use the Anthropic /v1/messages/count_tokens API directly.
func (*Provider) CreateStream ¶
type Request ¶ added in v0.29.0
type Request struct {
Model string `json:"model"`
MaxTokens int `json:"max_tokens"`
Stream bool `json:"stream"`
System SystemBlocks `json:"system,omitempty"`
Messages []Message `json:"messages"`
Tools []ToolDefinition `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
Thinking *ThinkingConfig `json:"thinking,omitempty"`
Metadata *metadata `json:"metadata,omitempty"`
CacheControl *CacheControl `json:"cache_control,omitempty"`
TopK int `json:"top_k,omitempty"`
TopP float64 `json:"top_p,omitempty"`
OutputConfig *outputConfig `json:"output_config,omitempty"`
}
func BuildRequest ¶ added in v0.16.0
func BuildRequest(reqOpts RequestOptions) (Request, error)
BuildRequest builds a JSON Request body for the Anthropic API.
func (Request) ControlParams ¶ added in v0.35.0
ControlParams returns the request as a map[string]any with verbose fields (system, messages, full tool definitions) stripped out. What remains are the control knobs actually sent to the Anthropic API — useful for observability.
type RequestOptions ¶ added in v0.16.0
type RequestOptions struct {
SystemBlocks SystemBlocks
UserID string
LLMRequest llm.Request
}
RequestOptions contains options for building an Anthropic Request.
type StreamErrorEvent ¶ added in v0.29.0
type StreamErrorEvent struct {
Error StreamErrorPayload `json:"error"`
}
StreamErrorEvent is sent when the API returns a stream-level error. Named StreamErrorEvent to avoid collision with llm.ErrorEvent.
type StreamErrorPayload ¶ added in v0.29.0
type StreamErrorPayload struct {
Message string `json:"message"`
}
StreamErrorPayload carries the error message from a StreamErrorEvent.
type SystemBlocks ¶ added in v0.29.0
type SystemBlocks []*TextBlock
type TextBlock ¶ added in v0.29.0
type TextBlock struct {
Text string `json:"text"`
// contains filtered or unexported fields
}
func (*TextBlock) WithCacheControl ¶ added in v0.29.0
func (b *TextBlock) WithCacheControl(cc *CacheControl) *TextBlock
type ThinkingBlock ¶ added in v0.29.0
type ThinkingBlock struct {
Thinking string `json:"thinking"`
Signature string `json:"signature"`
// contains filtered or unexported fields
}
func Thinking ¶ added in v0.29.0
func Thinking(thinking, signature string) *ThinkingBlock
func (*ThinkingBlock) WithCacheControl ¶ added in v0.29.0
func (b *ThinkingBlock) WithCacheControl(cc *CacheControl) *ThinkingBlock
type ThinkingConfig ¶ added in v0.29.0
type ToolDefinition ¶ added in v0.29.0
type ToolDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema any `json:"input_schema"`
CacheControl *CacheControl `json:"cache_control,omitempty"`
}
type ToolResultBlock ¶ added in v0.29.0
type ToolResultBlock struct {
ToolUseID string `json:"tool_use_id"`
Content string `json:"content"`
IsError bool `json:"is_error"`
// contains filtered or unexported fields
}
func ToolResult ¶ added in v0.29.0
func ToolResult(id, content string, isError bool) ToolResultBlock