Claude API Rate Limit Retry Logic: A Practical Guide
When Claude's API returns a 429 Too Many Requests or 529 Overloaded response, the fix is almost never "just try again immediately." You need retry logic with exponential backoff, jitter, and a hard cap on attempts — otherwise you either hammer the API into a longer penalty window or you give up too early and surface errors to users that a two-second wait would have avoided.
This article covers exactly how to build that retry logic: which status codes to retry, how to read Anthropic's rate limit headers, how to calculate backoff delays, and how to handle streaming requests (which can't simply be retried from scratch once tokens have started flowing).
Which errors are retryable
Not every error should trigger a retry. Retrying a 400 Bad Request just repeats the same failure. The errors worth retrying are:
- 429 — rate limit exceeded (requests per minute, tokens per minute, or concurrent requests)
- 529 — the API is temporarily overloaded on Anthropic's side
- 500 / 502 / 503 — transient server-side errors
- Network-level failures — timeouts, connection resets, DNS hiccups
Do not retry 400, 401, 403, or 404 — these indicate a malformed request, bad credentials, or missing resource. Retrying them wastes time and can mask a bug in your code.
Read the rate limit headers before you retry
Claude's API returns headers that tell you exactly how much room you have left:
anthropic-ratelimit-requests-remaining: 12
anthropic-ratelimit-tokens-remaining: 4000
anthropic-ratelimit-requests-reset: 2024-01-15T10:32:00Z
If you're already tracking these, you can avoid a 429 entirely by throttling proactively instead of reactively. But even with careful tracking, bursts from other parts of your system (or other users on a shared key) will eventually trigger a 429, so you still need retry logic as a safety net.
Exponential backoff with jitter
The standard pattern: double the wait time after each failed attempt, add randomness so concurrent requests don't retry in lockstep, and cap the maximum delay and attempt count.
async function callWithRetry(fn, { maxRetries = 5, baseDelayMs = 500 } = {}) {
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await fn();
} catch (err) {
const status = err.status;
const retryable = [429, 500, 502, 503, 529].includes(status);
if (!retryable || attempt === maxRetries) {
throw err;
}
// Respect Retry-After if the API sends one
const retryAfter = err.headers?.get('retry-after');
const delay = retryAfter
? Number(retryAfter) * 1000
: baseDelayMs * 2 ** attempt + Math.random() * 300;
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
}
A few details that matter:
- Honor
Retry-Afterfirst. If the API tells you exactly how long to wait, use that instead of your own calculated backoff. - Cap the maximum delay (e.g. 30–60 seconds) so a long outage doesn't make a retry loop hang for minutes.
- Jitter is not optional at scale. If 50 of your workers all hit a 429 at the same millisecond and all back off identically, they'll all retry at the same millisecond too, causing a second wave of 429s.
- Set a total timeout budget, not just a max retry count, if your request has an upstream deadline (e.g. a user-facing HTTP request with its own timeout).
Retry logic for streaming requests
Streaming responses complicate retries because you can't "resume" a stream — if it drops mid-way, you have a partial response you can't safely reuse. The practical approach:
- If the stream fails before any tokens arrive, retry the whole request with backoff as above.
- If the stream fails after tokens have started arriving, don't silently retry and concatenate — that risks duplicated or inconsistent output. Either surface a clear "response interrupted" state to the user, or restart the request from scratch and discard the partial output.
- Keep a short idle timeout on the stream itself (e.g. no bytes for 30 seconds) so a hung connection doesn't block your retry logic indefinitely.
Queuing instead of retrying blind
If you're regularly hitting rate limits because of burst traffic rather than occasional transient errors, backoff alone is a band-aid. The more durable fix is a request queue with a concurrency limit, so you never send more simultaneous requests than your rate limit allows in the first place. Libraries like p-queue (Node) or a simple token-bucket implementation work well here — they turn rate limit handling from reactive (catch 429, retry) to proactive (never send a request that would exceed the limit).
Where SubToAPI fits
If you'd rather not build and maintain this retry/backoff/queue stack yourself, SubToAPI wraps your existing Claude access in a standard HTTPS API with application keys (sub_live_...), streaming, and usage metadata per key — so you can see which key or team member is driving rate limit pressure without instrumenting your own logging. It doesn't eliminate Anthropic's rate limits, but it gives you per-key visibility so you can isolate a noisy integration before it starts throwing 429s across your whole account. See the quickstart or pricing for details.
Checklist for production-grade retry logic
- Retry only on 429, 429-adjacent 5xx codes, and network errors
- Respect
Retry-Afterwhen present - Use exponential backoff with jitter, not fixed delays
- Cap both max attempts and max delay
- Never blindly retry a partially-streamed response
- Add a concurrency-limited queue if rate limits are a recurring problem, not an occasional one
- Log retry counts and final failures separately from first-attempt failures so you can tell the difference between "rare blip" and "systemic overload"
Questions
What status code does Claude's API return when rate limited? A 429 Too Many Requests for exceeded request/token limits, and a 529 when Anthropic's infrastructure is temporarily overloaded. Both are safe to retry with backoff; other 4xx codes generally are not.
How long should I wait before retrying? Use the Retry-After header if it's present. Otherwise, start around 500ms–1s and double it on each subsequent failure, adding a small random jitter, capped at roughly 30–60 seconds.
Can I retry a streaming request after it fails? Only safely before any tokens have been received. Once a stream has partially delivered content, retry by discarding the partial output and starting a fresh request rather than trying to resume mid-stream.