anthropic

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package anthropic provides a provider implementation for Anthropic's Claude API.

Anthropic uses a different API format than OpenAI, so this package implements custom parsing, enrichment, and extraction logic.

Key differences from OpenAI:

  • Endpoint: /v1/messages
  • Auth: x-api-key header (not Bearer token)
  • Required header: anthropic-version
  • System prompt is a separate field (not a message with role "system")
  • Response uses content array instead of choices
  • Token usage fields: input_tokens/output_tokens

Basic usage:

provider, _ := anthropic.New("sk-ant-your-key")
proxy := llmproxy.NewProxy(provider)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheCreationInfo added in v0.0.3

type CacheCreationInfo struct {
	Ephemeral5mInputTokens int `json:"ephemeral_5m_input_tokens,omitempty"`
	Ephemeral1hInputTokens int `json:"ephemeral_1h_input_tokens,omitempty"`
}

CacheCreationInfo tracks cache creation token breakdown.

type Content

type Content struct {
	Text  string        `json:"-"`
	Parts []ContentPart `json:"-"`
}

Content can be either a string or an array of content blocks.

func (*Content) UnmarshalJSON

func (c *Content) UnmarshalJSON(data []byte) error

UnmarshalJSON handles Anthropic's flexible content format (string or array).

type ContentBlock

type ContentBlock struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

ContentBlock represents a content block in an Anthropic response.

type ContentPart

type ContentPart struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

ContentPart represents a single content block.

type Enricher

type Enricher struct {
	// APIKey is the Anthropic API key.
	APIKey string
	// Version is the Anthropic API version (defaults to 2023-06-01).
	Version string
}

Enricher implements llmproxy.RequestEnricher for Anthropic's API. It sets the required x-api-key and anthropic-version headers.

func NewEnricher

func NewEnricher(apiKey string) *Enricher

NewEnricher creates a new Anthropic enricher with the given API key. The API version defaults to 2023-06-01 if not specified.

func NewEnricherWithVersion

func NewEnricherWithVersion(apiKey, version string) *Enricher

NewEnricherWithVersion creates a new Anthropic enricher with a specific API version.

func (*Enricher) Enrich

func (e *Enricher) Enrich(req *http.Request, meta llmproxy.BodyMetadata, rawBody []byte) error

Enrich adds Anthropic-specific headers to the request. Sets:

  • x-api-key: <APIKey>
  • anthropic-version: <Version> (defaults to 2023-06-01)
  • Content-Type: application/json

type Extractor

type Extractor struct{}

Extractor implements llmproxy.ResponseExtractor for Anthropic responses.

func NewExtractor

func NewExtractor() *Extractor

NewExtractor creates a new Anthropic response extractor.

func (*Extractor) Extract

func (e *Extractor) Extract(resp *http.Response) (llmproxy.ResponseMetadata, []byte, error)

Extract parses an Anthropic response and returns unified metadata.

Anthropic responses use:

  • content array instead of choices
  • input_tokens/output_tokens instead of prompt_tokens/completion_tokens

Returns metadata, raw body bytes, and any error.

type Message

type Message struct {
	Role    string  `json:"role"`
	Content Content `json:"content"`
}

Message represents a message in an Anthropic request.

type Parser

type Parser struct{}

Parser implements llmproxy.BodyParser for Anthropic's request format.

func (*Parser) Parse

func (p *Parser) Parse(body io.ReadCloser) (llmproxy.BodyMetadata, []byte, error)

Parse reads an Anthropic request body and extracts metadata. It handles Anthropic-specific fields like the system prompt.

type Provider

type Provider struct {
	*llmproxy.BaseProvider
}

Provider is an Anthropic provider implementation.

func New

func New(apiKey string) (*Provider, error)

New creates a new Anthropic provider with the given API key. The provider is configured to use Anthropic's API endpoint (https://api.anthropic.com).

Example:

provider, _ := anthropic.New("sk-ant-your-api-key")

func NewWithVersion

func NewWithVersion(apiKey, version string) (*Provider, error)

NewWithVersion creates a new Anthropic provider with a specific API version.

Example:

provider, _ := anthropic.NewWithVersion("sk-ant-your-api-key", "2024-01-01")

type Request

type Request struct {
	Model     string                 `json:"model"`
	Messages  []Message              `json:"messages"`
	MaxTokens int                    `json:"max_tokens,omitempty"`
	System    string                 `json:"system,omitempty"`
	Custom    map[string]interface{} `json:"-"`
}

Request represents an Anthropic messages API request.

func (*Request) UnmarshalJSON

func (r *Request) UnmarshalJSON(data []byte) error

UnmarshalJSON captures unknown fields into Custom.

type Resolver

type Resolver struct {
	// BaseURL is the Anthropic API base URL.
	BaseURL *url.URL
}

Resolver implements llmproxy.URLResolver for Anthropic's API. It constructs the messages endpoint URL.

func NewResolver

func NewResolver(baseURL string) (*Resolver, error)

NewResolver creates a new resolver with the given base URL.

func (*Resolver) Resolve

func (r *Resolver) Resolve(meta llmproxy.BodyMetadata) (*url.URL, error)

Resolve returns the full URL for the Anthropic messages endpoint. Appends "/v1/messages" to the base URL.

type Response

type Response struct {
	ID            string             `json:"id"`
	Type          string             `json:"type"`
	Role          string             `json:"role"`
	Model         string             `json:"model"`
	Content       []ContentBlock     `json:"content"`
	StopReason    string             `json:"stop_reason"`
	StopSequence  string             `json:"stop_sequence,omitempty"`
	Usage         UsageInfo          `json:"usage"`
	CacheCreation *CacheCreationInfo `json:"cache_creation,omitempty"`
}

Response represents an Anthropic messages API response.

type UsageInfo

type UsageInfo struct {
	InputTokens              int `json:"input_tokens"`
	OutputTokens             int `json:"output_tokens"`
	CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
	CacheReadInputTokens     int `json:"cache_read_input_tokens"`
}

UsageInfo tracks token usage in an Anthropic response.

Jump to

Keyboard shortcuts

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