Lesson 69.5 · 5 of 5 · 4 min read · Reviewed 24 Nov 2026
Worked example: rate-limit tiers, headers and 429 handling
By the end, you can
- Read Claude's rate-limit headers.
- Handle 429 and overload errors correctly.
How limits are measured
Claude API rate limitsRate limit: A cap on how many requests or tokens you can use in a period, such as per minute. are set per organisation and per model class, in:
- Requests per minute
- Input tokensInput tokens: The tokens you send in a request: system prompt, tool definitions, conversation history and attached content. per minute
- Output tokensOutput tokens: The tokens the model generates in its reply, including any tool calls and thinking. per minute
Limits rise as your organisation moves up usage tiers. The rate limits page lists current values. Limits are applied continuously, so a big burst at the start of a minute can be limited even if the minute’s average would be fine. With prompt cachingPrompt caching: Reusing the processed form of a repeated prompt prefix across requests, which cuts cost and latency., cached input reads count differently toward input limits on most current models, which is another reason to cache long, stable prefixes.
Headers
Responses include headers such as:
anthropic-ratelimit-requests-remaining
anthropic-ratelimit-input-tokens-remaining
anthropic-ratelimit-output-tokens-remaining
anthropic-ratelimit-tokens-reset
retry-after (on 429 responses)
Feed the remaining values into your own limiter so it slows down before the wall.
Handling errors
| Error | Meaning | Action |
|---|---|---|
429 rate_limit_error |
You exceeded a limit | Wait for retry-after, back off with jitter |
529 overloaded_error |
The API is busy for everyone | Back off and retry; consider a fallback model |
| 5xx | Server-side problem | Retry a few times with backoff |
| 400-level other | Your request is wrong | Don’t retry; fix it |
The official SDKs retry some of these automatically with backoffExponential backoff: Waiting longer after each failed retry, for example 1, 2, 4 then 8 seconds.. Know their defaults so your own retry layer doesn’t multiply attempts.
On other providers
OpenAI returns similar headers, such as x-ratelimit-remaining-requests, x-ratelimit-remaining-tokens and matching reset times. Gemini enforces quotas per project, visible in the cloud console, and signals overload with a 429429 error: The HTTP status for too many requests: you hit a rate limit. RESOURCE_EXHAUSTED error. Whatever the source, feed what you learn back into your own limiter (69.2).
Key points
- Claude API limits are measured in requests per minute and input and output tokens per minute, per model, and grow with your usage tier.
- Responses include headers showing remaining budget and reset times; a 429 includes a retry-after header.
- On 429 or overload errors, back off with jitter and honour retry-after; don't hammer the API.
Honour retry-after on 429 errors
Retry rate-limited calls the way the server asks, with jitter, instead of hammering it.
You will have: A polite() retry loop that succeeds in fewer attempts than blind exponential backoff.
Steps
- Run the starter: both strategies make the same number of attempts.
- Read the retry-after header in polite() and wait at least that long.
- Add jitter with the seeded random generator.
- Compare attempts and success time for both strategies.
- Explain why many clients without jitter would all return at the same moment.
Done when
- honour retry-after succeeds in 2 attempts.
- Blind backoff takes 6 attempts.
- Every wait in polite() is at least the retry-after value.
Open in the Playground No API key needed.
Starter code (Python)
# Handle 429 errors: honour retry-after and add jitter, instead of blind fast retries.
import random
WINDOW_RESETS_AT = 20.0 # the rate-limit window frees up at t = 20 s
def api(now):
if now < WINDOW_RESETS_AT:
return 429, {"retry-after": str(int(WINDOW_RESETS_AT - now) + 1)}
return 200, {}
def naive(max_attempts=8):
now, wait = 0.0, 1.0
for attempt in range(1, max_attempts + 1):
status, _ = api(now)
if status == 200:
return attempt, now
now += wait
wait *= 2
return max_attempts, now
def polite(max_attempts=8, cap=60.0):
rng = random.Random(7)
now, backoff = 0.0, 1.0
for attempt in range(1, max_attempts + 1):
status, headers = api(now)
if status == 200:
return attempt, now
# TODO: wait at least the retry-after header, plus random jitter from rng.uniform(0, 1)
delay = backoff
now += min(delay, cap)
backoff = min(backoff * 2, cap)
return max_attempts, now
a, t = naive()
print(f"blind backoff: {a} attempts, success at t={t:.1f} s")
a, t = polite()
print(f"honour retry-after: {a} attempts, success at t={t:.1f} s")
Show a solution
# Handle 429 errors: honour retry-after and add jitter, instead of blind fast retries.
import random
WINDOW_RESETS_AT = 20.0 # the rate-limit window frees up at t = 20 s
def api(now):
if now < WINDOW_RESETS_AT:
return 429, {"retry-after": str(int(WINDOW_RESETS_AT - now) + 1)}
return 200, {}
def naive(max_attempts=8):
now, wait = 0.0, 1.0
for attempt in range(1, max_attempts + 1):
status, _ = api(now)
if status == 200:
return attempt, now
now += wait
wait *= 2
return max_attempts, now
def polite(max_attempts=8, cap=60.0):
rng = random.Random(7)
now, backoff = 0.0, 1.0
for attempt in range(1, max_attempts + 1):
status, headers = api(now)
if status == 200:
return attempt, now
retry_after = float(headers.get("retry-after", 0))
delay = max(retry_after, backoff) + rng.uniform(0, 1) # jitter spreads clients out
now += min(delay, cap)
backoff = min(backoff * 2, cap)
return max_attempts, now
a, t = naive()
print(f"blind backoff: {a} attempts, success at t={t:.1f} s")
a, t = polite()
print(f"honour retry-after: {a} attempts, success at t={t:.1f} s")
Do it Practise 429 handling in the Playground. No API key needed.
Finished the chapter? Take the chapter check.