How to handle API errors — retries, backoff, and when retrying makes it worse

Someone else's service blinked for eight seconds. Your code dutifully retried the request. And again. And again.
So did every other client at the same time — and the service, just getting back on its feet, took three times its usual traffic. An eight-second blip became a half-hour outage.
That's a retry storm, and it's usually caused by the most caring code in the building. Let's do this properly, step by step.
Step 1. Sort errors into two buckets
First and most important: half of all errors are pointless to retry. They will not "go through" no matter how long you wait.
Use the status code as your guide:
| Code | Meaning | Retry? | |---|---|---| | 400, 422 | the request is malformed | no, fix the request | | 401, 403 | you weren't let in | no, fix the key or the permissions | | 404 | no such thing | no | | 408 | request timed out | yes | | 429 | too many requests | yes, but specially | | 500, 502, 503, 504 | their problem | yes | | network dropped | no connection | yes |
Rule of thumb: "my fault" — don't retry. "They fell over" — retry.
Retrying a 400 is an infinite loop that never exits. Retrying a 401 is the surest way to get your key blocked by the service for suspicious activity.
Step 2. Never retry immediately
A second request ten milliseconds later lands on exactly the same overloaded server. The pause has to grow — that's exponential backoff:
delay = min(cap, base × 2^attempt)
With a base of 300 ms you get 300 ms → 600 → 1200 → 2400. The cap is mandatory, otherwise attempt six goes off to wait a minute.
const delay = Math.min(10_000, 300 * 2 ** attempt);
Step 3. Add jitter — or you get a synchronised volley
The subtlety nearly everyone skips.
If a thousand clients dropped off a service at once and they all compute the pause with the same formula, they all retry in the very same millisecond. You've replaced one volley with a series of smaller, equally synchronised ones.
The cure is randomness — jitter. The simplest working version ("full jitter", from the classic AWS write-up): pick a random number between zero and the computed delay.
const backoff = Math.min(10_000, 300 * 2 ** attempt);
const wait = Math.random() * backoff; // ← this line is the whole trick
One line turns a crowd at the door into an orderly queue.
Step 4. If the server told you how long to wait, listen
When you hit a rate limit (status 429), services often send a Retry-After header — the number of seconds to wait.
That's not a suggestion, it's a direct answer to your "when can I?" question. Your own formula always loses here: the server knows when its window opens, you're guessing.
const after = res.headers.get("Retry-After");
const wait = after ? Number(after) * 1000 : backoffWithJitter(attempt);
Step 5. Make sure retrying is safe
The nastiest scenario: the request arrived and executed, and the response got lost on the way back. From your code's point of view that looks like an ordinary timeout. So it retries — and charges the card a second time.
So before retrying, ask: what happens if this operation runs twice?
- Reads (fetch a list, open a record) — retry freely.
- Full replacement and deletion — usually fine too: same end state.
- Creating something (an order, a payment, an email) — dangerous.
For the dangerous cases there's a ready answer: an idempotency key. You generate a unique string per operation and send it as a header; the server remembers it and answers a repeat with the stored result instead of executing again. Every serious payment API works this way — check their docs, it's probably already there.
What you end up with
The logic all five steps collapse into:
async function callApi(url, options = {}, attempt = 0) {
const res = await fetch(url, options);
if (res.ok) return res.json();
const retryable = [408, 429, 500, 502, 503, 504].includes(res.status);
if (!retryable || attempt >= 4) throw new Error(`API ${res.status}`);
const after = res.headers.get("Retry-After");
const wait = after
? Number(after) * 1000
: Math.random() * Math.min(10_000, 300 * 2 ** attempt);
await new Promise((r) => setTimeout(r, wait));
return callApi(url, options, attempt + 1);
}
Four attempts, a growing randomised pause, respect for Retry-After, hopeless codes thrown straight up. All that's left is logging the failure so it's visible, instead of silently returning an empty array.
How many attempts should I make?
Three or four is plenty. If a service hasn't answered across four growing pauses, it didn't blink — it's down, and you're only adding load. Better to fail honestly and show the user a clear message.
Why does AI-written code usually have no retries?
Because you asked for "make a request to this API", not "make a resilient request". The model writes the happy path — it's shorter and reads cleaner. Retries, timeouts and 429 handling have to be asked for separately and explicitly.
Do I need retries if the API is my own?
Yes, for the same reason: between your frontend and your server there's still a network, mobile data, and a restart during deploys. Your own side fails less often, but just as suddenly.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





