anthropic

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package anthropic implements Anthropic's Messages API using llm's neutral request, response, tool, and stream contracts. It supports generation, image input and image tool results, signed/redacted thinking replay, prompt cache controls, non-streaming client tool use, and typed text/thinking/tool streaming. Anthropic Messages has no audio input content block, so CapabilityAudio is unsupported. JSON response mode is not implemented.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Type      string `json:"type"`
	Message   string `json:"message"`
	RequestID string `json:"-"`
}

APIError is an error returned by Anthropic's API.

func (*APIError) Error

func (e *APIError) Error() string

Error returns the provider's message, type, or a generic description.

type Client

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

Client is an immutable Anthropic client. It is safe for concurrent use when its configured HTTP client is safe for concurrent use.

func New

func New(config Config) (*Client, error)

New constructs a Client from config using protocol defaults.

func NewWithOptions

func NewWithOptions(config Config, options Options) (_ *Client, err error)

NewWithOptions constructs a Client with model compatibility overrides.

func (*Client) Generate

func (c *Client) Generate(ctx context.Context, request llm.Request) (_ *llm.Response, err error)

Generate performs one non-streaming Messages API request.

func (*Client) Info

func (c *Client) Info() llm.ModelInfo

Info describes the configured model.

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, request llm.Request) (_ llm.Stream, err error)

Stream starts one streaming Messages API request. Provider and transport failures after validation are delivered by the returned stream.

type Config

type Config struct {
	Provider               string
	Model                  string
	Capabilities           []llm.Capability
	APIKey                 string
	AccessToken            string
	BaseURL                string
	APIVersion             string
	DefaultMaxOutputTokens int
	HTTPClient             *http.Client
	Headers                http.Header
}

Config configures an Anthropic-compatible client. Model is required. Provider identifies the service in ModelInfo and errors; it defaults to "anthropic". BaseURL defaults to https://api.anthropic.com/v1, APIVersion defaults to 2023-06-01, and DefaultMaxOutputTokens defaults to 4096. Capabilities opts the configured model into optional protocol features; generation is always enabled, while streaming, client tool use, and vision must be listed explicitly. APIKey is optional to support gateways that authenticate through custom Headers. AccessToken selects Anthropic's provider-private Claude subscription OAuth identity, including its bearer, beta, CLI identity headers, and mandatory system identity; this unofficial flow is unstable. A non-nil HTTPClient and custom Headers are used as supplied, without being mutated by Client. A nil HTTPClient uses http.DefaultClient, which has no overall request timeout; callers should use context deadlines or configure a client timeout.

type Options

type Options struct {
	// ModelCompatibility is copied during construction.
	ModelCompatibility *llm.AnthropicCompatibility
}

Options configures model-specific Anthropic protocol compatibility.

Jump to

Keyboard shortcuts

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