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.AssistantMsg
- func MaybeRegister(reg *llm.Registry)
- func ParseStream(ctx context.Context, body io.ReadCloser, events chan<- llm.StreamEvent, ...)
- 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 ID constants for programmatic use.
const (
EnvAnthropicAPIKey = "ANTHROPIC_API_KEY"
)
Environment variable names for Anthropic configuration.
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.AssistantMsg
FindPrecedingAssistant finds the assistant message preceding the given index.
func MaybeRegister ¶ added in v0.13.0
MaybeRegister registers the Anthropic API provider if ANTHROPIC_API_KEY is set.
Note: The Claude OAuth provider (provider/anthropic/claude) is not auto-registered. Use claude.New() with a TokenProvider to create a Claude OAuth provider.
func ParseStream ¶ added in v0.16.0
func ParseStream(ctx context.Context, body io.ReadCloser, events chan<- llm.StreamEvent, meta StreamMeta)
ParseStream parses an Anthropic SSE stream and sends events to the channel.
Types ¶
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider implements the direct Anthropic API backend.
func (*Provider) CreateStream ¶
func (p *Provider) CreateStream(ctx context.Context, opts llm.StreamOptions) (<-chan llm.StreamEvent, error)
type RequestOptions ¶ added in v0.16.0
type RequestOptions struct {
Model string
MaxTokens int
SystemBlocks []SystemBlock
UserID string
StreamOptions llm.StreamOptions
}
RequestOptions contains options for building an Anthropic request.
type StreamMeta ¶ added in v0.16.0
StreamMeta passes context into the stream parser for StreamEventStart.
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 SystemMsg from messages and returns them as SystemBlocks. It filters out empty content. This allows multiple system messages to be accumulated into an array format as supported by the Anthropic API.
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.