anthropic

package
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: Apache-2.0 Imports: 13 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-4-7"

DefaultModel is used when LLMRequest.Model is empty. We follow the claude-api skill's guidance and default to the most capable Opus.

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

func WithCacheSystem(on bool) Option

WithCacheSystem enables prompt caching on the last system block by default. Off by default — turn it on once you've confirmed the system prompt is stable across turns (otherwise the cache write premium is paid for nothing).

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").

Jump to

Keyboard shortcuts

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