AI Coding Agent Retry Logic: Review It Before It Ships
AI coding agent retry logic often hammers a failing API with instant repeats. Use this five-point review and a corrected TypeScript example to fix it.
AI coding agent retry logic usually looks fine in review and fails exactly when you need it. The typical version wraps a call in a loop, catches any error, waits a fixed second or not at all, and tries again three times. Against a service that is already struggling, that pattern adds load at the worst possible moment. Amazon's engineers put the principle plainly in their write-up on timeouts, retries and backoff with jitter: "When failures are caused by overload, retries that increase load can make matters significantly worse."
This is a review checklist for the retry code your agent writes, with a corrected example at the end.
Where AI coding agent retry logic goes wrong
Here is a typical first draft, the kind you get when you ask for "make this API call more reliable":
async function callApi(url: string) {
for (let i = 0; i < 3; i++) {
try {
const res = await fetch(url);
return await res.json();
} catch (e) {
console.log("retrying...");
}
}
}Count the problems. It retries immediately. It never checks the status code, so a 400 Bad Request is not retried, which is right, but a 503 is also not retried because fetch does not throw on HTTP errors. It has no timeout, so a hung connection never reaches the catch block at all. After three failures it returns undefined with no error. And if the call creates something, a retry after a lost response may create it twice.
The five-point review
Run any retry code through these questions.
Does it wait, and does the wait grow? The most common pattern, per the AWS article, is exponential backoff, "where the wait time is increased exponentially after every attempt."
Is there jitter? Without randomness, every client that failed at the same moment retries at the same moment. The same source explains that "jitter adds some amount of randomness to the backoff to spread the retries around in time."
Is the retry count limited? The AWS authors write that in almost all cases their solution is to limit the number of times the client retries. Unbounded retry loops turn a short outage into a long one.
Does it retry only what is worth retrying? Network errors, timeouts, 429 (too many requests) and 5xx server errors, yes. Validation failures and authentication errors, never; they will fail the same way every time.
Is the operation safe to repeat? Reads usually are. Payments, emails and record creation are not unless the request carries an idempotency key, which, in the article's words, "guarantees that the side effects happen only once no matter how often you retry." The pattern is covered step by step in adding idempotency keys to an AI-built app.
A corrected version
This version adds a timeout, status handling, capped exponential backoff with full jitter, respect for a Retry-After header given in seconds, and a visible failure at the end instead of a silent undefined.
const RETRYABLE = new Set([408, 429, 500, 502, 503, 504]);
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
function backoffMs(attempt: number, baseMs = 500, capMs = 8000) {
return Math.random() * Math.min(capMs, baseMs * 2 ** attempt);
}
function retryAfterMs(res: Response): number | null {
const v = res.headers.get("retry-after");
const seconds = v ? Number(v) : NaN;
return Number.isFinite(seconds) ? seconds * 1000 : null;
}
export async function fetchWithRetry(url: string, init: RequestInit = {}, retries = 3) {
for (let attempt = 0; ; attempt++) {
const last = attempt >= retries;
let res: Response;
try {
res = await fetch(url, { ...init, signal: AbortSignal.timeout(10_000) });
} catch (err) {
if (last) throw err;
await sleep(backoffMs(attempt));
continue;
}
if (res.ok || !RETRYABLE.has(res.status) || last) return res;
await sleep(retryAfterMs(res) ?? backoffMs(attempt));
}
}Note what the function returns on the last attempt: the final response, even if it is a 503, so the caller can read the status and decide what to show. Network failures and timeouts throw instead. Either way the failure is visible.
Keep this in one shared helper rather than letting the agent paste a new retry loop into every file. One helper means one place to review, one place to tune, and one test: a fake server that returns 503 twice and then 200 should produce a success after exactly three calls, and a server that always returns 503 should produce a final 503 after four.
How long does the waiting actually take
With a base of 500 ms and a cap of 8 seconds, the maximum possible wait before each retry doubles until it hits the cap. Full jitter picks a random value between zero and that maximum, so the average is half.
Retry number | Maximum wait | Average wait with full jitter |
|---|---|---|
1 | 0.5 s | 0.25 s |
2 | 1 s | 0.5 s |
3 | 2 s | 1 s |
4 | 4 s | 2 s |
5 | 8 s (cap) | 4 s |
Three retries add at most 3.5 seconds of waiting. That is the trade: a few seconds of patience for a service that gets room to recover. If your user is staring at a spinner, decide how long they will tolerate and set the retry count to fit.
Rate limits are a special case
A 429 response is the server telling you to slow down, and many APIs include a Retry-After header with how long to wait. Honor it over your own calculation. For AI model APIs in particular, rate limits are routine rather than exceptional, so read how AI API rate limits work and consider limiting your own outgoing requests, as in adding rate limiting to an AI-built app. And when retries finally give up, the failure should be visible, not swallowed; see stopping an AI coding agent from swallowing errors.
The wider picture of reviewing agent output sits in the AI coding tools guide.
Frequently asked questions
What is exponential backoff with jitter?
Exponential backoff means each retry waits longer than the last. Jitter adds randomness to the wait so many clients do not all retry at the same instant.
How many times should an API call be retried?
Few. Two or three retries covers most brief glitches. Past that you are usually adding load to a service that needs it removed.
Should I retry a 400 or 401 error?
No. Those mean the request itself is wrong or unauthorized, and repeating it will fail the same way.
Do I need idempotency keys for retries?
For anything that creates, charges or sends, yes. Without them a retry after a lost response can duplicate the action.
How did this land?
About the author

Staff Engineer, Platform
Carlo works on the platform that turns prompts into running apps. He writes the engineering deep dives and the changelog notes worth reading.


