Documentation
¶
Index ¶
- Constants
- Variables
- func BuildRequest(reqOpts RequestOptions) ([]byte, error)
- func CalculateCost(model string, usage *llm.Usage) float64
- func DefaultOptions() []llm.Option
- func FillCost(model string, usage *llm.Usage)
- func FindPrecedingAssistant(messages llm.Messages, toolIdx int) llm.AssistantMessage
- func ParseStream(ctx context.Context, body io.ReadCloser, pub llm.Publisher, meta StreamMeta)
- type Provider
- type RequestOptions
- type StreamMeta
- type SystemBlock
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 ToolCallID constants for programmatic use.
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 BuildRequest ¶ added in v0.16.0
func BuildRequest(reqOpts RequestOptions) ([]byte, error)
BuildRequest 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 FindPrecedingAssistant ¶ added in v0.16.0
func FindPrecedingAssistant(messages llm.Messages, toolIdx int) llm.AssistantMessage
FindPrecedingAssistant finds the assistant message preceding the given index.
func ParseStream ¶ added in v0.16.0
func ParseStream(ctx context.Context, body io.ReadCloser, pub llm.Publisher, meta StreamMeta)
Types ¶
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 llm.TokenCountRequest) (*llm.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 RequestOptions ¶ added in v0.16.0
type RequestOptions struct {
Model string
MaxTokens int
SystemBlocks []SystemBlock
UserID string
StreamOptions llm.Request
}
RequestOptions contains options for building an Anthropic request.
type StreamMeta ¶ added in v0.16.0
type SystemBlock ¶ added in v0.16.0
type SystemBlock struct {
Type string `json:"type"`
Text string `json:"text"`
CacheControl *cacheControl `json:"cache_control,omitempty"`
}
SystemBlock represents a system message block in the Anthropic API.
func CollectSystemBlocks ¶ added in v0.16.0
func CollectSystemBlocks(messages llm.Messages) []SystemBlock
CollectSystemBlocks extracts all System from messages and returns them as SystemBlocks.
func NewSystemBlock ¶ added in v0.16.0
func NewSystemBlock(text string) SystemBlock
NewSystemBlock creates a new system block with text content.
func PrependSystemBlocks ¶ added in v0.16.0
func PrependSystemBlocks(prefix []SystemBlock, userBlocks []SystemBlock) []SystemBlock
PrependSystemBlocks prepends blocks to existing system blocks.