anthropic

package
v0.24.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
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.

Variables

View Source
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

func CalculateCost(model string, usage *llm.Usage) float64

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

func DefaultOptions() []llm.Option

DefaultOptions returns the default options for Anthropic.

func FillCost added in v0.21.0

func FillCost(model string, usage *llm.Usage)

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 ParseStream added in v0.16.0

func ParseStream(ctx context.Context, body io.ReadCloser, events *llm.EventStream, meta StreamMeta)

ParseStream parses an Anthropic SSE stream and sends events to the stream.

Types

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider implements the direct Anthropic API backend.

func New

func New(opts ...llm.Option) *Provider

New creates a new Anthropic provider.

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

func (p *Provider) CreateStream(ctx context.Context, opts llm.StreamRequest) (<-chan llm.StreamEvent, error)

func (*Provider) Models

func (p *Provider) Models() []llm.Model

func (*Provider) Name

func (p *Provider) Name() string

type RequestOptions added in v0.16.0

type RequestOptions struct {
	Model         string
	MaxTokens     int
	SystemBlocks  []SystemBlock
	UserID        string
	StreamOptions llm.StreamRequest
}

RequestOptions contains options for building an Anthropic request.

type StreamMeta added in v0.16.0

type StreamMeta struct {
	RequestedModel string
	ResolvedModel  string
	StartTime      time.Time
}

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.

Directories

Path Synopsis
Package claude provides an Anthropic provider using Claude OAuth tokens.
Package claude provides an Anthropic provider using Claude OAuth tokens.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL