Documentation
¶
Overview ¶
anthropic implements an API client for Anthropic's Claude API, which generates replies with the Messages API, and counts tokens. https://platform.claude.com/docs/en/api/overview
Index ¶
- Constants
- type Client
- func (c *Client) GetModel(ctx context.Context, name string) (llm.Model, error)
- func (c *Client) ListModels(ctx context.Context) ([]llm.Model, error)
- func (c *Client) Measure(ctx context.Context, model llm.Model, text string) (uint, error)
- func (*Client) Name() string
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Self() llm.Provider
- func (c *Client) URL() *url.URL
- func (c *Client) WithoutSession(ctx context.Context, model llm.Model, messages []llm.Message, tools []llm.Tool, ...) (llm.Message, llm.Usage, error)
- type Model
- func (m *Model) Aliases() []string
- func (m *Model) Capabilities() llm.ModelCap
- func (m *Model) Created() time.Time
- func (m *Model) Description() string
- func (m *Model) InputTokenLimit() *uint
- func (m *Model) Meta() map[string]json.RawMessage
- func (m *Model) Name() string
- func (m *Model) OutputTokenLimit() *uint
- func (m *Model) Provider() llm.Provider
Constants ¶
const ( // Endpoint is the endpoint of Anthropic's API, which is the default Endpoint = "https://api.anthropic.com/v1" // APIVersion is the version of the API, which is sent with each request APIVersion = "2026-10-21" // PingInterval is how long the result of pinging the API is kept, before // it is pinged again, so the API isn't pinged too often PingInterval = 5 * time.Minute )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
Client is a client of Anthropic's API, which generates replies, and counts the tokens of text
func New ¶
New returns a client of Anthropic's API, at the endpoint, which is Endpoint when it's empty, with the API key, which it needs
func (*Client) GetModel ¶
GetModel returns the model with the name. Returns ErrNotFound when there's none.
func (*Client) ListModels ¶
ListModels returns the models, a page at a time, until they're all listed
func (*Client) Measure ¶ added in v1.2.2
Measure returns how many tokens the text is, for the model, which must be a model of this client, by counting the tokens of a message from the user with the text, less those of the message apart from its text. Text which was counted before isn't counted again. Empty text is none. When the tokens can't be counted, such as when counting is rate limited, they're an estimate, of a token for every estimateBytes bytes, so what's sent to the model is still fitted to its context, unless the context is done.
func (*Client) Ping ¶
Ping checks the connectivity of the client, and that the API key is accepted, by listing a model. The API is only pinged once every ping interval: otherwise, the result of the last ping is returned, unless it ended as its caller's context did.
func (*Client) WithoutSession ¶
func (c *Client) WithoutSession(ctx context.Context, model llm.Model, messages []llm.Message, tools []llm.Tool, stream llm.GeneratorStreamFn, opts ...llm.Opt) (llm.Message, llm.Usage, error)
WithoutSession returns the model's reply to the messages, which must be for a model of this client, and the tokens used, with the Messages API. The model can call the tools, and the reply has its calls. The reply is always streamed, so a long one doesn't time out, and when the stream function isn't nil, it's called with each piece of its thinking and text as it's generated. The request is cached, so the next one, which starts the same, such as with the same system prompt and tools, is cheaper. The request isn't timed out, as a model can take a while to reply, but is cancelled with the context.
type Model ¶ added in v1.2.2
type Model struct {
// contains filtered or unexported fields
}
Model is one of Anthropic's models, satisfying the llm.Model interface
func (*Model) Capabilities ¶ added in v1.2.2
Capabilities returns what the model can do: every model replies, calls tools, and doesn't embed, transcribe or translate, or take a temperature, as the API's version takes no sampling options, and whether it thinks, and understands images, is as the API says
func (*Model) Created ¶ added in v1.2.2
Created returns when the model was created, or the zero time, when it isn't known
func (*Model) Description ¶ added in v1.2.2
Description returns the model's name for people, such as Claude Opus 5.5
func (*Model) InputTokenLimit ¶ added in v1.2.2
InputTokenLimit returns the model's context window, when it's known
func (*Model) Meta ¶ added in v1.2.2
func (m *Model) Meta() map[string]json.RawMessage
Meta returns the model's capabilities, as the API returns them
func (*Model) OutputTokenLimit ¶ added in v1.2.2
OutputTokenLimit returns the most tokens the model replies with, when it's known