anthropic

package
v2.9.0-dev.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package anthropic implements models.Provider for Anthropic / Claude.

ADK Go ships only the Gemini and Apigee model backends, so this package adapts the official Anthropic Go SDK (github.com/anthropics/anthropic-sdk-go) to the ADK's model.LLM interface. genai-shaped requests are translated to Anthropic's Messages API; streaming responses are accumulated back into genai-shaped events the ADK runner expects.

Conversation history is preserved automatically by the ADK runner (the in-memory session service replays prior events on each turn); this provider is stateless aside from the API client.

Index

Constants

View Source
const (
	EnvVertexProject = "ANTHROPIC_VERTEX_PROJECT_ID"
	EnvVertexRegion  = "CLOUD_ML_REGION"
)

Env vars consulted by the Vertex constructor when project / region are not supplied explicitly via config. Names match Anthropic SDK conventions; the GCP-standard fallbacks let the same env that drives Vertex Gemini also drive Vertex Anthropic.

View Source
const DefaultMaxTokens = 16_384

DefaultMaxTokens caps a single response when the caller hasn't set one. 16K is a comfortable middle ground: plenty for most turns, well under the streaming SDK's HTTP timeouts.

View Source
const DefaultModel = "claude-opus-5"

DefaultModel is used when LLMRequest.Model is empty. We follow the claude-api skill's guidance and default to the most capable Opus — the LATEST one, per the policy documented on taskclass.ModelForTier. Not the Mythos-class tier above it (claude-fable-5 / claude-mythos-5), which costs 2x and isn't a general-purpose default.

Pinned to taskclass.ModelForTier("anthropic", "frontier") by TestDefaultModel_MatchesFrontierTier: an operator who sets --task=implement and one who sets nothing should land on the same model, or the task-class flag reads as a silent downgrade.

View Source
const DefaultSmallModelID = "claude-haiku-4-5"

DefaultSmallModelID is the Anthropic cheap-tier model used by default for agentic subtasks when the operator hasn't pinned one with --agentic-small-model. Same value for the first-party and Vertex backends; the Vertex publication name resolves at call time.

View Source
const DefaultVertexRegion = "us-east5"

Default Vertex region for Claude. Most current Anthropic Vertex deployments live in us-east5; override per call site as needed.

View Source
const EnvAPIKey = "ANTHROPIC_API_KEY" // #nosec G101 -- env var name, not a credential

EnvAPIKey is the environment variable consulted when no key is supplied via config.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuiltinTools

type BuiltinTools struct {
	WebSearch bool // Server-side web search; per-search billing on top of tokens.
}

BuiltinTools toggles Anthropic's server-side built-in tools surfaced by core-agent. Each enabled flag becomes one entry on the request's Tools slice alongside any user-defined function declarations.

Defaults: everything OFF. Anthropic's server-side tools are billed per use on top of token cost (web_search is per-search), so we apply the same "active surface = opt in" rule that keeps Gemini's CodeExecution off by default. The library caller decides explicitly whether the cost and external-action posture are acceptable.

To turn one on:

provider, _ := anthropic.New(key, anthropic.WithWebSearch(true))

To replace the whole set:

provider, _ := anthropic.New(key, anthropic.WithBuiltinTools(anthropic.BuiltinTools{
    WebSearch: true,
}))

Other Anthropic server-side tools (web_fetch, code_execution, text_editor, bash, memory) aren't surfaced today. Add them under the same struct when a concrete consumer needs one.

func DefaultBuiltinTools

func DefaultBuiltinTools() BuiltinTools

DefaultBuiltinTools returns the on-by-default baseline applied to every Provider unless overridden via WithBuiltinTools or one of the per-tool helpers. Currently empty — see BuiltinTools doc for why.

type CacheOptions added in v2.9.0

type CacheOptions struct {
	// System marks the last system block. Since the render order is
	// tools → system → messages, that one marker caches the tool
	// schemas and the system prompt together.
	System bool
	// History places rolling breakpoints over the tail of the
	// conversation so a growing transcript is re-read at the cache
	// rate instead of re-billed in full every turn (#714).
	History bool
}

CacheOptions selects which parts of a request carry Anthropic cache_control breakpoints. Both halves are prefix-cached by the same mechanism; they're separable because they fail differently — System is worthless if the instruction carries a per-turn timestamp, History is worthless for one-shot calls that never replay a prefix.

The zero value disables caching entirely.

func DefaultCacheOptions added in v2.9.0

func DefaultCacheOptions() CacheOptions

DefaultCacheOptions is what every constructor starts from: cache the stable prefix and the conversation tail. On by default because the break-even is two requests against a 5-minute TTL and core-agent's agentic loop issues its second request seconds after the first — the shape that loses (a single request whose prefix is never seen again) is the rare one, and it is the one an operator can turn off.

func (CacheOptions) Enabled added in v2.9.0

func (o CacheOptions) Enabled() bool

Enabled reports whether any breakpoint would be placed.

type Option

type Option func(*Provider)

Option configures a Provider at construction.

func WithBuiltinTools

func WithBuiltinTools(b BuiltinTools) Option

WithBuiltinTools replaces the Provider's whole BuiltinTools set.

func WithCacheSystem deprecated

func WithCacheSystem(on bool) Option

WithCacheSystem caches the last system block and nothing else.

Deprecated: use WithPromptCache. This option predates the history breakpoints and keeps its original all-or-nothing meaning — it REPLACES the policy rather than editing one field, so WithCacheSystem(false) still means "no caching at all", the way it did when system blocks were the only thing that could be marked. A caller who wrote it to avoid the write premium keeps that outcome instead of silently acquiring rolling history breakpoints.

func WithPromptCache added in v2.9.0

func WithPromptCache(o CacheOptions) Option

WithPromptCache sets the whole prompt-caching policy, replacing DefaultCacheOptions. Pass a zero CacheOptions to turn caching off — worth doing for a request shape whose prefix varies every call, where the write premium buys reads that never come.

func WithWebSearch

func WithWebSearch(on bool) Option

WithWebSearch toggles Anthropic's server-side web_search tool. Off by default — opt in when you've decided the per-search cost and external-call posture are acceptable.

type Provider

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

Provider is the Anthropic implementation of models.Provider. The same struct serves both the first-party API and Vertex AI backends — only the embedded client differs. name carries which one this is so telemetry and Resolve() see the right identity.

func New

func New(apiKey string, opts ...Option) (*Provider, error)

New constructs a Provider with the given API key (first-party api.anthropic.com). Pass options to tune behavior. Empty key falls back to the ANTHROPIC_API_KEY env var.

func NewVertex

func NewVertex(ctx context.Context, project, region string, opts ...Option) (*Provider, error)

NewVertex constructs a Provider that talks to Claude via Google Vertex AI. project and region are required. Authentication uses Application Default Credentials (run `gcloud auth application-default login`, or set GOOGLE_APPLICATION_CREDENTIALS, or rely on workload identity in production).

We deliberately load credentials via google.FindDefaultCredentials ourselves and pass them to vertex.WithCredentials — vertex.WithGoogleAuth panics on missing creds, which we don't want at startup.

func (*Provider) DefaultSmallModel

func (p *Provider) DefaultSmallModel() string

DefaultSmallModel satisfies models.SmallModelDefaulter so core-agent can route subtask digesting to a cheap-tier Claude model without requiring the operator to set --agentic-small-model.

func (*Provider) Model

func (p *Provider) Model(_ context.Context, modelID string) (adkmodel.LLM, error)

Model returns a model.LLM for the given model ID. modelID may be empty, in which case DefaultModel is used.

Note: Vertex AI sometimes serves Claude under date-suffixed model IDs (e.g. "claude-opus-4-5@20251101"). When using "anthropic-vertex", pass the exact ID Vertex expects via cfg.Model.Name; the SDK plugs it into the Vertex URL path verbatim.

func (*Provider) Name

func (p *Provider) Name() string

Name reports the provider identity ("anthropic" or "anthropic-vertex").

func (*Provider) PromptCache added in v2.9.0

func (p *Provider) PromptCache() CacheOptions

PromptCache reports the currently installed policy. Lets a host log what it wired without keeping its own copy.

func (*Provider) SetPromptCache added in v2.9.0

func (p *Provider) SetPromptCache(o CacheOptions)

SetPromptCache installs the caching policy after construction. Exists for the daemon's wiring order: the provider comes out of the registry (models.Resolve, which sees only config) before the CLI kill switch can be applied, and Model() copies the policy into each LLM it builds. Call it before the first Model() call — like the Gemini provider's cache hooks, it is startup wiring, not a live control.

Jump to

Keyboard shortcuts

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