summaryrefslogtreecommitdiff
path: root/internal/retry
diff options
context:
space:
mode:
authorClaude Sonnet 5 <noreply@anthropic.com>2026-07-03 09:03:01 +0000
committerClaude Sonnet 5 <noreply@anthropic.com>2026-07-03 09:03:13 +0000
commite38f673edc7428c0c836c39f40707cb681defb14 (patch)
tree97a704b2d83b9d572ae67d057315a6781ddf1470 /internal/retry
parentbbd58e8d77f7c996add29b69f600e835a922542e (diff)
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1moSNCJRcP6kykA4tyUSs
Diffstat (limited to 'internal/retry')
-rw-r--r--internal/retry/backoff.go11
-rw-r--r--internal/retry/backoff_test.go21
2 files changed, 31 insertions, 1 deletions
diff --git a/internal/retry/backoff.go b/internal/retry/backoff.go
index b91abc4..a372b37 100644
--- a/internal/retry/backoff.go
+++ b/internal/retry/backoff.go
@@ -17,6 +17,12 @@ const maxBackoffDelay = 5 * time.Minute
// IsRateLimitError returns true if err looks like a transient rate-limit
// (e.g. HTTP 429, "too many requests", "overloaded") that is worth retrying.
+//
+// Patterns are additive across providers: the original set matches
+// OpenAI-compatible error text (internal/llm); "rate_limit_error",
+// "overloaded_error", and "529" additionally match Anthropic's Messages API
+// error shape (HTTP 429 with error.type "rate_limit_error", or HTTP 529
+// with error.type "overloaded_error" — see internal/provider/anthropic).
func IsRateLimitError(err error) bool {
if err == nil {
return false
@@ -25,7 +31,10 @@ func IsRateLimitError(err error) bool {
return strings.Contains(msg, "rate limit") ||
strings.Contains(msg, "too many requests") ||
strings.Contains(msg, "429") ||
- strings.Contains(msg, "overloaded")
+ strings.Contains(msg, "overloaded") ||
+ strings.Contains(msg, "rate_limit_error") ||
+ strings.Contains(msg, "overloaded_error") ||
+ strings.Contains(msg, "529")
}
// ParseRetryAfter extracts a Retry-After duration from an error message.
diff --git a/internal/retry/backoff_test.go b/internal/retry/backoff_test.go
index a963fc2..d605f29 100644
--- a/internal/retry/backoff_test.go
+++ b/internal/retry/backoff_test.go
@@ -38,6 +38,27 @@ func TestIsRateLimitError_Overloaded(t *testing.T) {
}
}
+func TestIsRateLimitError_AnthropicRateLimitError(t *testing.T) {
+ err := errors.New(`anthropic: http 429 rate_limit_error: Number of requests has exceeded your rate limit.`)
+ if !IsRateLimitError(err) {
+ t.Error("want true for Anthropic 'rate_limit_error' shape, got false")
+ }
+}
+
+func TestIsRateLimitError_AnthropicOverloadedError(t *testing.T) {
+ err := errors.New(`anthropic: http 529 overloaded_error: Overloaded`)
+ if !IsRateLimitError(err) {
+ t.Error("want true for Anthropic 'overloaded_error' shape (HTTP 529), got false")
+ }
+}
+
+func TestIsRateLimitError_HTTP529(t *testing.T) {
+ err := errors.New("anthropic: http 529 unexpected_error: service unavailable")
+ if !IsRateLimitError(err) {
+ t.Error("want true for '529', got false")
+ }
+}
+
func TestIsRateLimitError_NonRateLimitError(t *testing.T) {
err := errors.New("claude exited with error: exit status 1")
if IsRateLimitError(err) {