Retries with Backoff
Exponential backoff and jitter for transient provider and network failures.
In plain words
When a request fails for a temporary reason, the program tries again after waiting a little, waiting longer each time and adding a random delay so everyone doesn't retry at once.
Think of it like…
Redialling a busy phone line. You wait a bit before the second try, longer before the third, and not at exactly the same second as everyone else who also got a busy signal, or the line stays jammed; and after a few attempts you give up and try another route.
Where you’ve seen it
The LLM client library retries a call up to 4 times with full-jitter exponential backoff: base 0.5 seconds, cap 30 seconds, delay = random(0, min(30, 0.5 × 2ⁿ)). The upper bounds are 0.5, 1, 2 and 4 seconds, so total worst-case waiting is 7.5 seconds across 4 retries (and about 3.75 seconds on average); a longer schedule of five retries tops out at 15.5 seconds. It retries 429, 500, 502, 503, 504, connection resets and read timeouts; it never retries 400, 401, 403, 422 or a content-policy refusal, because repeating them cannot succeed. A server Retry-After: 12 overrides the computed delay. Jitter matters: when 1,000 clients fail together and back off without jitter, each wave arrives as a synchronised spike of 1,000 requests at 0.5 s, 1.5 s and 3.5 s; with full jitter the first wave spreads over 0 to 0.5 seconds (about 2,000 requests a second rather than 1,000 in an instant) and later waves spread further, giving the service room to recover. Layering is the hidden danger: the SDK retries 3 times, the application retries 3 times around it and the gateway retries 3 times around that, so one user request can cause 3 × 3 × 3 = 27 provider calls during an outage; the team keeps retries in one layer. A retry budget (extra attempts may be at most 10% of base traffic) caps the load factor at 1.1× instead of up to 4× at 100 requests a second, since 10 retries a second are allowed and the rest fail fast. Each user request has a total deadline of 20 seconds with an 8-second per-attempt timeout, and no attempt starts if less time remains than a typical call needs. Because the calls cost money and some have side effects, every retried POST carries the same idempotency key, and streams that already emitted tokens are resumed or surfaced as errors instead of silently regenerated. A circuit breaker opens after 50% failures over 20 requests, so during a real outage the service switches to a fallback model instead of retrying into a wall. Metrics track retry rate, success-after-retry rate and budget exhaustion.
Words you’ll hear
- Exponential backoff
- Waiting base × 2ⁿ before retry n, up to a cap.
- Jitter
- Randomness added to each delay so clients don't retry in sync.
- Retry budget
- A limit on retries as a fraction of normal traffic.
- Circuit breaker
- A switch that stops calls to a failing dependency for a cooling-off period.
Visual diagram
Theory & deep dive
Backoff, jitter and what to retry
A fixed-interval retry hammers a struggling service at a constant rate; exponential backoff spaces attempts further apart (0.5, 1, 2, 4 seconds and so on, capped at something like 30 seconds) so a recovering dependency sees less load over time. Without randomness, clients that failed together retry together. Jitter fixes that: with full jitter the wait is uniform between 0 and the current cap (sleep = random(0, min(cap, base × 2ⁿ))), which spreads 1,000 clients over each interval instead of stacking them on its edge; equal jitter keeps half the delay fixed and randomises the other half. Prefer full jitter unless you need a guaranteed minimum wait. Honour a server's Retry-After header, which reflects its actual recovery time.
Decide what is retryable. Transient conditions: network timeouts and resets, 429, 500, 502, 503, 504. Permanent conditions that retrying cannot fix: 400, 401, 403, 404, 422, context-length errors and policy refusals. Retry non-idempotent operations only with an idempotency key. Probability helps motivate retries: if each attempt independently fails 2% of the time, three attempts fail together only 0.02³ = 0.0008% of the time, but real failures are correlated (an outage affects all attempts), so retries beat blips, not outages.
Base 0.5 s, cap 30 s, full jitter. Retry 1: random(0, 0.5). Retry 2: random(0, 1). Retry 3: random(0, 2). Retry 4: random(0, 4). Worst case 7.5 s of waiting, average 3.75 s. Without jitter every client waits exactly 0.5, 1, 2, 4 s and arrives together.
Budgets, deadlines, layering and fallbacks
Retries add load exactly when a dependency is weakest. A retry budget limits it: allow retries up to about 10% of request volume, so at 100 requests a second no more than 10 extra calls a second are made and the dependency sees at most 1.1× normal load, instead of up to 4× when every request retries 3 times. Combine it with a total deadline (for example 20 seconds per user request, with 8-second per-attempt timeouts) and don't start an attempt that can't finish before the deadline. Layering multiplies attempts: an SDK, an application wrapper and a gateway each retrying 3 times turn one request into 3 × 3 × 3 = 27 calls; choose a single layer (usually the closest to the call) and have other layers pass errors through.
Use a circuit breaker to stop retrying into a sustained outage: after, say, 50% failures across the last 20 calls it opens for a cooling-off period, calls fail fast (or go to a fallback such as a smaller model or a cached answer) and a few probes test recovery. For streams, retrying after tokens were already shown is hazardous: resume from a buffer or surface an error rather than regenerating a different answer. Consider hedged requests (a second request after the p95 latency) only for idempotent, cheap calls, since they cost extra. Instrument retry rate, success-after-retry, budget exhaustion and breaker state; a rising retry rate is an early warning of dependency trouble even when users still see successes.
Provider fully down for one minute at 100 requests/s. SDK 3 retries × app 3 retries × gateway 3 retries = 27 attempts per user request → 2,700 calls/s into a dead dependency. One layer with 3 retries and a 10% budget → at most 110 calls/s, and the breaker opens after 20 failures.
How it works, step by step
Step 1 of 41. Classify errors
Mark which errors are transient and safe to retry and which are permanent; require idempotency keys for POSTs.
- Honour
- Retry-After
When to use it & trade-offs
What it is
Retrying repeats a failed operation in the hope that the fault was temporary. Exponential backoff waits longer after each failure (base × 2ⁿ, capped), jitter randomises each wait so clients don't retry in lockstep, a retry budget limits how much extra load retries may add and a deadline bounds the total time spent. Retry only transient failures (timeouts, connection resets, 429, 503, 504) and only operations that are idempotent or protected by an idempotency key. Respect a server's Retry-After header when present. For LLM and provider calls, retries sit alongside fallbacks and circuit breakers: they smooth brief blips, while longer outages call for failing fast or switching models.
Why it matters: Transient faults are routine in distributed AI systems: a provider returns 503 for two seconds, a connection resets, a rate limit trips. A sensible retry turns most of those into invisible hiccups. A careless one turns them into outages: synchronised retries hit a recovering service like a battering ram, layered retries multiply load and retried non-idempotent calls double-charge. Getting the policy right keeps success rates high, tail latency bounded and the provider's bill and mood intact.
When to reach for it
- Provider calls that return 429, 503 or time out
- Idempotent reads and keyed writes
- Worker tasks with transient dependency errors
When to avoid or be careful
- Retrying 4xx errors that cannot succeed
- Retries at several layers of the same call
- Retrying non-idempotent operations without a key
Retry strategies
Strengths: Simple.
Watch out: Constant pressure; synchronised retries.
Strengths: Spreads load and gives dependencies time to recover.
Watch out: Adds latency on failures.
Strengths: Caps amplification; enables fast fallback.
Watch out: More moving parts to tune.
Interview signals & numbers to know
When this topic comes up
- “How do you retry a flaky LLM provider safely?”
- Why add jitter
- Retry budgets and amplification
- Circuit breakers versus retries
- Idempotency and retries
Benchmarks & rules of thumb
- 0.5 · 1 · 2 · 4 s
- delay caps for four retries (7.5 s worst case)
- 3 × 3 × 3 = 27
- attempts per request with three retry layers
- 10%
- a common retry budget, capping load at 1.1×
Common mistakes & follow-ups
Common interview mistakes
- Using exponential backoff without jitter.
- Retrying inside every layer of the stack.
- Ignoring the overall deadline and retrying past the user's patience.
Likely interviewer follow-ups
- How would you retry a streaming response that failed midway?
- How do circuit breakers interact with retries?
- How do you choose the number of attempts?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Retries with Backoff with generative AI interview Q&A and hands-on practice.