# How Do You Handle Rate Limits and 429 Errors?

> What a 429 error means, how to retry it safely and how to stay under the limit, explained simply for your AI interview.

**In short:** A 429 error means your app has sent too many requests or tokens in a short window. Wait the time the Retry-After header gives, or back off with random waits under a growing ceiling, cap the retries, and keep your own traffic under the limit.

AI and LLM interview question Q330, Inference, performance and cost. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-do-you-handle-rate-limits-and-429-errors

## Say it in 30 seconds

A 429 means I've sent too many requests or tokens in a short time. First I read the Retry-After header and wait that long. If there's no header, I back off: each try gets a higher ceiling, and I pick a random wait under it, so many failed jobs don't retry together. I cap the tries, and I keep my own traffic under the limit. A spend cap is different: retrying won't help until someone raises it.

## Say it in 2 minutes

A 429 means the provider has said: too many requests or tokens in a short window. The first thing I do is read the error code, because a spend cap and a rate limit need different fixes.

If the reply has a Retry-After header, I wait that many seconds and try again. If there is no header, I back off: each try has a higher ceiling than the last, up to a maximum, and I pick a random wait under that ceiling, so a thousand failed jobs don't all retry at the same moment. I cap the tries, and anything still failing goes to a retry queue with the claim ID logged.

The better fix is to stay under the limit. I cap how many calls run at once, spread bulk work at a steady rate, and cache repeated text, because cache reads don't count toward the input token limit (cache writes do). Overnight jobs get their own batch lane, so a busy night can't take down live chat.

A spend cap is different. It sends no Retry-After, and retries keep failing until someone raises the cap or the month turns over. So the code stops, alerts a person, and keeps the work queued.

## What is a 429 error, in plain words?

A 429 is the provider's way of saying: too many requests, slow down. Model APIs (the doors your code uses to call the AI) cap how many requests, input tokens and output tokens (small chunks of text the model reads and writes) one organisation can use in a time window, for each model. Go over a cap and you get a 429 back instead of an answer. A 429 is about how much you send per minute, so a very long prompt can use up the token limit too.

Think of a claims desk that can take 60 calls a minute. The 61st caller gets a busy tone. Redialling in a tight loop only jams the line for everyone, so the sensible move is to wait a little and call back.

Here is a made-up case with a realistic shape. An insurer's claims agent reads 800 claim files after a storm, and the provider starts sending 429s halfway through. The team has to finish every file, without losing any and without hammering the API.

- **Rate limit:** The cap on requests, or tokens, that you can send in a time window.
- **429:** The standard web reply code for too many requests. It means try again later.
- **Retry-After:** A header in the reply saying how many seconds to wait before retrying.
- **Backoff:** Waiting longer after each failed try, instead of trying at once.
- **Jitter:** A random wait picked below each ceiling (the longest wait allowed for that try), so many clients don't retry together.

## What kinds of limit send a 429?

- **Rate: Too many per minute.** A cap on requests per minute, and on input and output tokens per minute, for each model.
- **Sudden jump: Too fast, even under the cap.** A sharp rise in use can get a 429 while the minute total still looks fine.
- **Spend cap: Monthly money limit reached.** Waiting does not help. The 429 keeps coming until the limit resets or someone raises it.

Bursts matter as much as totals. Anthropic's docs give a simple case: 60 requests a minute might be enforced as one request a second, so a short burst fails even when the minute looks fine. OpenAI says the same kind of thing: a slow_down error can come even when you are inside the per-minute limits.

Azure's docs warn that a 429 can arrive even while your usage shows below your quota. So the dashboard total is not the whole story, and the error reply is the thing to read.

*Diagram, What happens when a request hits a limit: Your request goes to the provider, which answers within its limits. Over a limit it returns a 429, and your code reads Retry-After, or backs off with a random wait. Nothing retries the same call with no wait.*
1. Your app sends one request to the provider.
2. The provider answers within its limits, so the claim moves on.
3. Over a limit, the provider sends back a 429 instead.
4. Your code reads the Retry-After header first.
5. Only when there is no header, it picks a random wait under a growing ceiling.

## How do you retry a 429 safely?

1. **Read Retry-After first:** If the reply has a Retry-After header, wait that many seconds, the number the provider gives.
2. **No header? Back off:** Double the ceiling after each failed try, up to a maximum, so the load falls away instead of piling on.
3. **Add jitter:** Pick a random wait under each ceiling. Without the randomness, a thousand jobs that failed together retry together and fail together again.
4. **Cap the attempts:** Stop after a few tries, log the claim ID, and put the work on a retry queue. An endless loop turns a slow API into an outage.
5. **Never retry a spend cap:** Check the error code first. A monthly cap needs someone to raise it, or a new month to start.

> "When the response has a retry-after header, wait that many seconds before retrying." (Anthropic)
>
> "Follow Retry-After when it's present, reduce your request rate, and then increase it gradually." (OpenAI)
>
> "Implement a retry strategy by using truncated exponential backoff." (Google Cloud)

Many SDKs, the ready-made code libraries vendors give you, do some of this for you. Anthropic's SDK retries a failed request twice by default, and without a Retry-After it waits from half a second up to eight seconds. AWS's architecture team found that adding jitter cut both the client work and the load on the server a lot. You still need to read the error code, because retrying a spend cap only fails again.

Here is a small version in plain Python. The provider is made up, and the code prints each wait instead of sleeping, so it runs at once.

```python
import random

random.seed(7)  # same numbers on every run

def full_jitter(attempt, base=1.0, cap=8.0):
    # A random wait up to a doubling cap, so many clients don't retry at the same moment.
    return random.uniform(0, min(cap, base * 2 ** attempt))

def provider(attempt):
    # Made-up provider: over its limit for the first three tries, then it answers.
    return 429 if attempt < 3 else 200

def send_claim(retry_after=None):
    for attempt in range(6):
        if provider(attempt) == 200:
            return f"attempt {attempt + 1}: answered"
        # Obey Retry-After when the provider sends it; otherwise back off with jitter.
        wait = retry_after if retry_after is not None else full_jitter(attempt)
        print(f"attempt {attempt + 1}: 429, wait {wait:.2f} s")
    return "gave up after 6 attempts"

print(send_claim())
print(send_claim(retry_after=2))
```

Output:

```text
attempt 1: 429, wait 0.32 s
attempt 2: 429, wait 0.30 s
attempt 3: 429, wait 2.60 s
attempt 4: answered
attempt 1: 429, wait 2.00 s
attempt 2: 429, wait 2.00 s
attempt 3: 429, wait 2.00 s
attempt 4: answered
```

The first call has no header, so each wait is a random time under a ceiling that doubles with each try. In this run the second wait (0.30 s) came out shorter than the first (0.32 s). The second call gets a Retry-After of 2 seconds and obeys it every time. Either way the loop stops as soon as the call succeeds, and never runs more than six tries.

## How do you stay under the limit in the first place?

- **Cap concurrency:** Limit how many calls run at the same time (concurrency), with a queue in front of the API. Ten workers, not a thousand.
- **Spread the bulk work:** Run batch jobs at a steady rate, not all at once. Google's advice is similar for pay-as-you-go models: if your model uses Standard pay-as-you-go, smoothing traffic and reducing large spikes can help.
- **Cache the repeated text:** Cache reads do not count toward Anthropic's input token limit, so a policy document read thousands of times uses far less of your allowance.
- **Use the batch lane for offline work:** Batch processing has its own limits, shared across all models. Overnight work does not spend the live chat's allowance.
- **Split the budget by workspace:** Anthropic lets you set spend and rate limits for each workspace, so one team's job can be kept from using up the whole organisation's allowance.

Worked example, with made-up numbers. Say the provider allows 300 requests a minute for the claims agent. If 800 files hit it in one burst, about 500 of them get a 429 in the same minute. Spread evenly at 250 a minute instead, the same 800 files take a little over three minutes, and none fail.

### How would you run 50,000 summaries overnight without breaking live chat?

Give the batch job its own lane. Run the summaries at a steady rate in the background, with a retry queue. Keep live chat on its own budget, so a heavy night never blocks a customer at 9 a.m.

- **Without a plan: One shared budget.** The batch uses most of the allowance, and live chat starts getting 429s when people log in.
- **With a plan: Two lanes.** The batch runs slowly in its own workspace, chat keeps its share, and both retry with Retry-After.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a 429 means too many requests or tokens in a time window.
- [ ] Read Retry-After first, then back off with jitter.
- [ ] Cap the retries and queue the work, so nothing loops forever.
- [ ] Keep your own traffic under the limit: concurrency, spreading, caching.
- [ ] Know the spend cap: retrying will not clear it.
- [ ] Watch how close you run to the limit and how often 429s happen, in production dashboards, not only in the logs.

**Common traps**

- Trap: "Just retry in a loop until it works." Better: A loop with no wait adds load to an API that is already saying slow down. Wait, add jitter, and stop after a few tries.
- Trap: "A 429 means my code has a bug." Better: Often it is the shape of the traffic, or prompts that are too long. Look at bursts, concurrency and prompt length before you blame the code.
- Trap: "A bigger tier fixes it." Better: A higher limit helps, but a burst still fails at any limit. Smooth the traffic too.

## Can you spot the risky fix?

**Spot the risky fix:** Your claims agent gets a 429 on every call. Which fix is safest to ship first?

A. Retry at once in a tight loop until a call works
B. Read Retry-After, wait with jitter, and stop after a few tries
C. Send all 800 files in parallel so they finish sooner
D. Delete the retry code so failures show up faster

**Answer:** B. Option two waits as the provider asks and gives up in the end. A tight loop adds load to a service that is already asking you to slow down. Parallel sends make the burst bigger. Deleting the retries only moves the failure to the claims team.

**Quick check:** A 429 arrives with no Retry-After header, and the error says the monthly spend cap is reached. What should the code do?

A. Retry every five seconds until it clears
B. Retry with random waits under a growing ceiling
C. Stop retrying, alert a person, and wait for the limit to be raised or the month to reset
D. Shorten the system prompt and send again

**Answer:** C. A spend-cap 429 has no Retry-After and keeps failing until access resumes. Retries only waste calls. Raising the cap or waiting for the new month is the fix.

## Can you answer these from memory?

**Flash cards**

- **What is a 429 error?** Too many requests or tokens in a time window. The provider asks you to slow down.
- **What does Retry-After mean?** How many seconds to wait before retrying. Obey it when the header is there.
- **What is exponential backoff?** Each retry's ceiling is higher than the last, up to a maximum.
- **Why add jitter?** A random wait under each ceiling stops many clients retrying at the same moment.
- **Is a spend-cap 429 worth retrying?** No. It has no Retry-After and keeps failing until access resumes.
- **Best first fix for bulk work?** Queue it and spread requests evenly, instead of sending everything at once.

## Likely follow-up questions

- Q331 When do you use batch APIs? https://designervenkat.online/interview/ai/when-do-you-use-batch-apis
- Q337 Timeouts, retries and backoff? https://designervenkat.online/interview/ai/timeouts-retries-and-backoff
- Q326 Prompt cache vs semantic cache vs KV cache? https://designervenkat.online/interview/ai/prompt-cache-vs-semantic-cache-vs-kv-cache
- Q324 How do you estimate token cost for a feature? https://designervenkat.online/interview/ai/how-to-estimate-token-cost
- Q334 How do you cut cost on a cloud-hosted LLM workload? https://designervenkat.online/interview/ai/how-do-you-cut-cost-on-a-cloud-hosted-llm-workload
