From e38f673edc7428c0c836c39f40707cb681defb14 Mon Sep 17 00:00:00 2001 From: Claude Sonnet 5 Date: Fri, 3 Jul 2026 09:03:01 +0000 Subject: feat(provider): add native Anthropic Messages API adapter (Phase 2) Adds internal/provider/anthropic, the first genuinely new provider.Provider implementation on top of Phase 1's provider-neutral tool-use loop, alongside (not replacing) the existing Docker/CLI-subprocess ContainerRunner path for the "claude" agent type: - internal/provider/anthropic: translates the neutral ChatRequest/ChatResponse shape to/from Anthropic's Messages API content-block format (system as a top-level field, tool_use/tool_result blocks, no "tool" role -- tool results become user-role messages), with a per-model-prefix pricing table for CostUSD - internal/retry: IsRateLimitError additively extended to recognize Anthropic's rate_limit_error/overloaded_error/529 shapes - internal/config: RunnersConfig.Anthropic/AnthropicEnabled() gate - internal/cli/serve.go, run.go: register runners["anthropic"] as a NativeRunner when [providers.anthropic].api_key is set and enabled -- tracked as a distinct executions.agent="anthropic" budget bucket, separate from the CLI-subprocess "claude" runner even though both bill the same Anthropic account go build/vet/test -race all pass. No live Anthropic API key is available in this environment, so verification is via fake-httptest-server adapter tests (12 cases, incl. multi-turn tool_result round-trip and rate-limit error matching) plus a Pool/NativeRunner routing test proving agent.type: "anthropic" actually reaches the new provider. Live end-to-end verification against the real API is a follow-up once a key is configured. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01V1moSNCJRcP6kykA4tyUSs --- internal/provider/anthropic/pricing.go | 70 ++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 internal/provider/anthropic/pricing.go (limited to 'internal/provider/anthropic/pricing.go') diff --git a/internal/provider/anthropic/pricing.go b/internal/provider/anthropic/pricing.go new file mode 100644 index 0000000..c5b5298 --- /dev/null +++ b/internal/provider/anthropic/pricing.go @@ -0,0 +1,70 @@ +package anthropic + +import "strings" + +// modelPrice is USD cost per million tokens for one model (or model family +// prefix). +type modelPrice struct { + InputPerMTok float64 + OutputPerMTok float64 +} + +// pricingTable maps a model-name *prefix* to its per-million-token pricing. +// Keyed by prefix (rather than exact model string) so dated/versioned model +// IDs (e.g. "claude-3-5-sonnet-20241022") still resolve without needing an +// entry per snapshot. Edit this table as Anthropic's pricing or model +// lineup changes — it intentionally has no other logic attached. +// +// Prices below are cached from training/skill data as of mid-2026 and are +// not fetched live; verify against https://platform.claude.com/docs/en/pricing +// before relying on them for real billing decisions. +var pricingTable = map[string]modelPrice{ + // Claude Fable 5 / Mythos family (most capable, most expensive tier). + "claude-fable-5": {InputPerMTok: 10.00, OutputPerMTok: 50.00}, + "claude-mythos-5": {InputPerMTok: 10.00, OutputPerMTok: 50.00}, + "claude-mythos-preview": {InputPerMTok: 10.00, OutputPerMTok: 50.00}, + + // Opus tier. + "claude-opus-4-8": {InputPerMTok: 5.00, OutputPerMTok: 25.00}, + "claude-opus-4-7": {InputPerMTok: 5.00, OutputPerMTok: 25.00}, + "claude-opus-4-6": {InputPerMTok: 5.00, OutputPerMTok: 25.00}, + "claude-opus-4-5": {InputPerMTok: 5.00, OutputPerMTok: 25.00}, + "claude-opus-4-1": {InputPerMTok: 5.00, OutputPerMTok: 25.00}, + "claude-opus-4-0": {InputPerMTok: 5.00, OutputPerMTok: 25.00}, + "claude-opus-4": {InputPerMTok: 5.00, OutputPerMTok: 25.00}, + "claude-3-opus": {InputPerMTok: 15.00, OutputPerMTok: 75.00}, + + // Sonnet tier. + "claude-sonnet-5": {InputPerMTok: 3.00, OutputPerMTok: 15.00}, + "claude-sonnet-4-6": {InputPerMTok: 3.00, OutputPerMTok: 15.00}, + "claude-sonnet-4-5": {InputPerMTok: 3.00, OutputPerMTok: 15.00}, + "claude-sonnet-4-0": {InputPerMTok: 3.00, OutputPerMTok: 15.00}, + "claude-sonnet-4": {InputPerMTok: 3.00, OutputPerMTok: 15.00}, + "claude-3-7-sonnet": {InputPerMTok: 3.00, OutputPerMTok: 15.00}, + "claude-3-5-sonnet": {InputPerMTok: 3.00, OutputPerMTok: 15.00}, + "claude-3-sonnet": {InputPerMTok: 3.00, OutputPerMTok: 15.00}, + + // Haiku tier. + "claude-haiku-4-5": {InputPerMTok: 1.00, OutputPerMTok: 5.00}, + "claude-3-5-haiku": {InputPerMTok: 0.80, OutputPerMTok: 4.00}, + "claude-3-haiku": {InputPerMTok: 0.25, OutputPerMTok: 1.25}, +} + +// defaultPricing is used for unrecognized models rather than erroring — +// Sonnet-tier pricing is a reasonable mid-point default. +var defaultPricing = modelPrice{InputPerMTok: 3.00, OutputPerMTok: 15.00} + +// pricingFor returns the per-million-token input/output pricing for model, +// matching the longest pricingTable prefix that model starts with. Falls +// back to defaultPricing for unrecognized models. +func pricingFor(model string) (inputPerMTok, outputPerMTok float64) { + price := defaultPricing + bestLen := -1 + for prefix, p := range pricingTable { + if len(prefix) > bestLen && strings.HasPrefix(model, prefix) { + bestLen = len(prefix) + price = p + } + } + return price.InputPerMTok, price.OutputPerMTok +} -- cgit v1.2.3