Help us shape the future of Go! Take the 2026 Go Developer Survey

anthropic

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package anthropic adapts Anthropic's Messages API to agent.Model. It translates the provider-neutral []agent.Message/Part model to Anthropic content blocks (text, thinking+signature, tool_use, tool_result), declares tools, opens the SSE stream, and normalizes Anthropic's event types into agent's Event taxonomy. Zero external deps — net/http + stdlib.

Native Anthropic is exactly what Google ADK Go still lacks (#225/#1097); this is a wedge. Streaming tool-call args arrive as input_json_delta fragments — the canonical case the agent core's finalize() json.Valid gate handles.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Model

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

Model is an Anthropic Messages API adapter implementing agent.Model.

func New

func New(apiKey string, opts ...Option) *Model

New constructs an Anthropic model adapter. apiKey is your Anthropic API key.

func (*Model) Describe added in v0.8.0

func (m *Model) Describe() agent.ModelInfo

Describe reports the adapter's identity: provider "anthropic", the configured model ID, and no JSON-schema response format (a request that sets one fails with agent.ErrConfig).

func (Model) Format added in v0.7.0

func (m Model) Format(f fmt.State, _ rune)

Format writes String for every verb, so no fmt verb (%v, %+v, %#v, %s, %x, ...) prints the key.

func (*Model) RequiresToolsForRequired added in v0.10.0

func (m *Model) RequiresToolsForRequired() bool

RequiresToolsForRequired implements agent.ToolRules: Anthropic refuses tool choice "required" with no tools declared.

func (*Model) Stream

func (m *Model) Stream(ctx context.Context, req agent.Request) (*agent.Stream, error)

Stream implements agent.Model.

func (Model) String added in v0.7.0

func (m Model) String() string

String describes the adapter by its model id. It never includes the API key (or the base URL, which can carry credentials of its own), so a config that holds a *Model can be logged.

func (*Model) ToolNameRule added in v0.10.0

func (m *Model) ToolNameRule() *regexp.Regexp

ToolNameRule implements agent.ToolRules: the tool names Anthropic accepts, `^[a-zA-Z0-9_-]{1,64}$`, so agent.New refuses any other name when the agent is built.

type Option

type Option func(*Model)

Option configures a Model in New.

func WithBaseURL

func WithBaseURL(u string) Option

WithBaseURL sets the API host, for a proxy or a compatible endpoint; /v1/messages is appended to it. The default is "https://api.anthropic.com".

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

WithHTTPClient sets the HTTP client requests are sent with. The default is http.DefaultClient.

func WithMaxResponseBytes added in v0.8.0

func WithMaxResponseBytes(n int64) Option

WithMaxResponseBytes caps how many bytes of one streamed reply the adapter reads: a reply that runs longer fails with agent.ErrResponseTooLarge, which middleware.Retryable does not retry. The default is provider.DefaultMaxResponseBytes (32 MiB); n <= 0 keeps it. Raise it for replies that legitimately run longer, such as large inline images.

func WithMaxTokens

func WithMaxTokens(n int) Option

WithMaxTokens sets the default limit on tokens in a reply; a request's Sampling.MaxTokens overrides it. The default is 4096 (Anthropic requires a limit on every request).

func WithModel

func WithModel(id string) Option

WithModel sets the model ID sent with every request. The default is "claude-sonnet-4-6".

func WithPromptCache

func WithPromptCache() Option

WithPromptCache turns on Anthropic prompt caching: cache_control breakpoints are placed on the system prompt and the tool definitions — the large, constant prefix an agent loop resends every turn — so repeat turns are billed at the cache-read rate. Cache hits/writes surface in agent.Usage (CacheReadTokens / CacheWriteTokens).

func WithToolResultCodec added in v0.2.0

func WithToolResultCodec(c provider.ToolResultCodec) Option

WithToolResultCodec encodes tool results sent to the model with c instead of raw JSON (for example GCF, to cut tokens on structured output). The journal keeps the JSON form; only what the model reads changes. Default is JSON.

Jump to

Keyboard shortcuts

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