← Blog

Claude API Rate Limit Retry Logic: A Practical Guide

2026-11-21 · 5 min read · SubToAPI Team

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:

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:

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:

  1. If the stream fails before any tokens arrive, retry the whole request with backoff as above.
  2. 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.
  3. 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

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.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →