anthropic

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 16 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

type Client struct {
	*client.Client
	// contains filtered or unexported fields
}

Client is a client of Anthropic's API, which generates replies, and counts the tokens of text

func New

func New(endPoint, apiKey string, opts ...client.ClientOpt) (*Client, error)

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

func (c *Client) GetModel(ctx context.Context, name string) (llm.Model, error)

GetModel returns the model with the name. Returns ErrNotFound when there's none.

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context) ([]llm.Model, error)

ListModels returns the models, a page at a time, until they're all listed

func (*Client) Measure added in v1.2.2

func (c *Client) Measure(ctx context.Context, model llm.Model, text string) (uint, error)

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) Name

func (*Client) Name() string

Name returns the provider name

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

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) Self

func (c *Client) Self() llm.Provider

Self returns the underlying client implementation

func (*Client) URL added in v1.2.2

func (c *Client) URL() *url.URL

URL returns the client's normalized endpoint

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) Aliases added in v1.2.2

func (m *Model) Aliases() []string

Aliases returns no aliases, as the API lists none

func (*Model) Capabilities added in v1.2.2

func (m *Model) Capabilities() llm.ModelCap

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

func (m *Model) Created() time.Time

Created returns when the model was created, or the zero time, when it isn't known

func (*Model) Description added in v1.2.2

func (m *Model) Description() string

Description returns the model's name for people, such as Claude Opus 5.5

func (*Model) InputTokenLimit added in v1.2.2

func (m *Model) InputTokenLimit() *uint

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) Name added in v1.2.2

func (m *Model) Name() string

Name returns the model's identifier, such as claude-opus-5-5

func (*Model) OutputTokenLimit added in v1.2.2

func (m *Model) OutputTokenLimit() *uint

OutputTokenLimit returns the most tokens the model replies with, when it's known

func (*Model) Provider added in v1.2.2

func (m *Model) Provider() llm.Provider

Provider returns the model's provider

Jump to

Keyboard shortcuts

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