Anthropic api resilience
Skill mickolasjae/mick-applied-ai-toolkit/skills/anthropic-api-resilience
Use when adding rate-limit / overload resilience to any Anthropic SDK client (Claude API consumer). Generates round-robin key rotation + exponential backoff (1s→5min, 2x multiplier) + circuit breaker (10 consecutive failures). Triggers on: 'Anthropic API rate limit', 'Claude API retries', 'multiple Anthropic keys', 'round-robin Anthropic', 'production-grade Anthropic client', 'rate limit error 429', 'Anthropic overloaded_error'.From its SKILL.md
npx -y skills add mickolasjae/mick-applied-ai-toolkit --skill anthropic-api-resilienceAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
5.3 KB, ~1.2k tokens by cl100k_base, as published. Nobody here has run it
Anthropic API Resilience
Production-grade reliability wrapper for any @anthropic-ai/sdk (TS) or anthropic (Python) client. Adds three layers: round-robin key rotation, exponential backoff, and a circuit breaker on per-key failure streaks.
When to use
Trigger this skill whenever the user is:
- Hitting
429 RateLimitErroror529 overloaded_errorfrom Claude - Asking for "Anthropic API retries", "multiple Anthropic keys", "round-robin Anthropic", "production-grade Anthropic client"
- Running an agent loop, batch job, or any long-running pipeline that calls
client.messages.create()more than ~1/sec - Reviewing code that calls Anthropic with no retry/backoff wrapper
Do not trigger for: simple one-shot scripts, prompt-engineering questions, or non-Anthropic providers.
Quick assessment (ask this first)
How many Anthropic API keys do you have available?
- 1 key: round-robin buys nothing. Install the wrapper anyway — backoff + circuit breaker still prevent thundering-herd retries that get you rate-limited harder. Skip the multi-key env-var step.
- 2+ keys: full payoff. Distinct keys from different Anthropic accounts/orgs give independent rate-limit buckets. Same-account keys share the bucket and only help with key-specific failures (revoked, network, etc.).
Spec (verified pattern)
Sourced from RL-Studio-Anthropic/ANTHROPIC_KEY_MANAGEMENT.md:
| Knob | Value |
|---|---|
| Base backoff | 1 second |
| Max backoff | 5 minutes (300s) |
| Multiplier | 2.0x per consecutive failure |
| Circuit-break threshold | 10 consecutive failures |
| Circuit recovery | First success resets state |
| Key discovery | ANTHROPIC_API_KEY, ANTHROPIC_API_KEY_2, ANTHROPIC_API_KEY_3, … |
| Start-key | Randomized (avoid thundering herd on cold start) |
Key-selection algorithm
1. Start at current round-robin index
2. Skip keys in backoff window or circuit-broken
3. If all keys unhealthy → use the least backed-off one (graceful degradation)
4. On success: reset that key's failure counter to 0, clear backoff
5. On failure: increment failures, set backoff = min(base * 2^failures, max)
What to retry
Retry on these — they are transient:
RateLimitError(429)APIConnectionError(network)InternalServerError(5xx)overloaded_error(529)
Do not retry on BadRequestError, AuthenticationError, PermissionDeniedError, NotFoundError, or any other 4xx that isn't 429. These are deterministic — retrying just burns quota and produces the same error.
Integration steps
- Pick the template for the user's language:
- TypeScript / Node:
typescript-template.ts - Python:
python-template.py
- TypeScript / Node:
- Copy into the project under
src/lib/anthropic/(TS) orsrc/anthropic/(Py). Keep the filename meaningful:resilient_client.ts/resilient_client.py. - Wire env vars: add
ANTHROPIC_API_KEY_2,ANTHROPIC_API_KEY_3, … to.env,docker-compose.yml, deployment task definitions. Single-key setups skip this. - Swap the import at every call site:
Keep the same// before import Anthropic from "@anthropic-ai/sdk"; const client = new Anthropic(); // after import { ResilientAnthropicClient } from "./lib/anthropic/resilient_client"; const client = new ResilientAnthropicClient();client.messages.create(...)call shape — the wrapper is interface-compatible. - Expose health snapshot for observability. Add a
/health/anthropicroute (or equivalent) that returnsclient.getKeyStats(). Alert whenhealthy_keys < total_keys. - Verify: run a load test or wait for a real 429. Confirm logs show
backing off for NsandSelected ANTHROPIC_API_KEY_N for next request.
See integration-guide.md for project-specific wiring (LinguaMind, mcp-butterfly, Butterfly Security).
Anti-patterns
- Don't retry on
BadRequestErroror other deterministic 4xx — it's a code bug, not a transient failure. The template explicitly excludes these. - Don't bypass the wrapper for "fast paths". Once installed, all Anthropic traffic should go through it so the per-key stats are accurate.
- Don't share one key across many processes without backoff — concurrent requests amplify rate limits. The wrapper's backoff is per-process; for multi-process fleets add a shared Redis-backed limiter.
- Don't set max backoff < 60s — Anthropic rate-limit windows are minute-scale. Sub-minute caps just produce retry storms.
- Don't forget to remove explicit
apiKey: process.env.ANTHROPIC_API_KEYoverrides at call sites — they bypass the manager.
Files in this skill
SKILL.md— this filetypescript-template.ts—ResilientAnthropicClientfor@anthropic-ai/sdkpython-template.py—ResilientAnthropicClientforanthropicintegration-guide.md— project-specific wiring recipes
What ships with it: 3 files
20.7 KB alongside SKILL.md, 2 of them executable
- integration-guide.md5.2 KB
- python-template.pyruns7.6 KB
- typescript-template.tsruns7.8 KB