E3 · Lesson 05
Retry the server, not the model: errors, backoff and jitter
Two kinds of failure reach the loop. They go to different places. When a tool fails (the file does not exist, the customer ID is wrong), the error goes back to the model as the tool's result, because the model can do something about it. When the model call fails (the server timed out, answered 503, or refused with 429), the model never hears of it: the loop waits and asks again. Mixing them up produces either an agent that gives up on a busy server or one that silently retries "file not found" until its budget runs out.
Retry what may pass, fail what will not
| Failure | Retry? | Why |
|---|---|---|
| timeout, refused connection, server hung up mid-reply | yes | transient by nature |
| 429 Too Many Requests | yes, after Retry-After | the server said "later" |
| 500, 502, 503, 504 | yes | overloaded or restarting |
| 408, 425 | yes | the request was early or slow, not wrong |
| 400, 401, 403, 404, 422 | no | the same request fails the same way every time |
a reply that will not parse, a KeyError in your code |
no | a bug: retrying hides it |
classify in loop/retry.py encodes this table. The last row matters more than it looks. A retry policy that catches every exception turns a bug in your adapter into four slow failures, and then a report of "server unreliable".
The arithmetic of retries
A model call fails independently with probability p. With k attempts it fails only if all k fail, so it succeeds with probability 1 - p^k. A task that needs n successful calls succeeds with probability
Reliability compounds badly: at p = 0.05, a five-call task with no retries (k = 1) succeeds 0.95^5 = 77.4\% of the time. The study checks the formula against the loop itself. A scripted five-call task runs against a client that fails each call with probability p, 4,000 runs per cell:
| p | attempts k | measured | 95% interval | (1-p^k)^5 | model calls per run |
|---|---|---|---|---|---|
| 0.02 | 1 | 90.0% | 89.1% to 90.9% | 90.4% | 4.80 |
| 0.02 | 2 | 99.8% | 99.6% to 99.9% | 99.8% | 5.09 |
| 0.05 | 1 | 77.4% | 76.1% to 78.7% | 77.4% | 4.52 |
| 0.05 | 2 | 98.5% | 98.1% to 98.8% | 98.8% | 5.22 |
| 0.05 | 3 | 100.0% | 99.9% to 100.0% | 99.9% | 5.26 |
| 0.2 | 1 | 32.4% | 31.0% to 33.9% | 32.8% | 3.35 |
| 0.2 | 3 | 95.9% | 95.2% to 96.4% | 96.1% | 6.09 |
| 0.2 | 4 | 99.1% | 98.8% to 99.4% | 99.2% | 6.21 |
Every theoretical value lies inside its interval. On the live model the same thing happens (the build): with a 10% failure rate injected into every model call, 5 of 20 Module 1 runs crashed on their first 503, the number 1 - 0.9^{2.7} predicts for runs of 2.7 calls, and no E3 run did. Two retries take a flaky server from 77% to near certainty, at a cost of about 0.7 extra calls per run. Module 1's loop is the k = 1 row: at p = 0.2 it finishes one run in three. Real failures are not independent (a server that is down stays down for a while), so treat the formula as a best case and the interval column as the cost of believing it.
How long to wait: exponential backoff with full jitter
Retrying at once hits a struggling server when it can least cope. The standard wait doubles with each attempt up to a cap. The standard mistake is to make it exact. When a thousand agents fail at the same moment (a deploy, a network blip), exact backoff makes them retry at exactly 0.5 s, then 1 s, then 2 s: the herd stays a herd. Full jitter draws each wait uniformly from [0, \min(\text{cap}, \text{base} \cdot 2^n)] (Brooker, 2015):
def backoff(attempt, base=0.5, cap=8.0, rng=None):
rng = rng or random
return rng.uniform(0.0, min(cap, base * 2 ** attempt))
The study's herd: 1,000 clients fail at t = 0 and each retries four times; load is counted in 100 ms buckets.
| retry 1 | retry 2 | retry 3 | retry 4 | |
|---|---|---|---|---|
| no jitter: peak requests in one bucket | 1,000 | 1,000 | 1,000 | 1,000 |
| full jitter | 211 | 120 | 56 | 29 |
Without jitter, every retry is the original spike moved later. With full jitter, the first retry already spreads the herd five-fold, and later retries spread it further, because each wait is drawn over a wider window.
Retry-After
A 429 or 503 may carry Retry-After: 7, the server's own estimate of when to come back (RFC 9110, section 10.2.3). Honour it: wait at least that long, and wait longer if the backoff draw says so. That is max(backoff, retry_after) in call_with_retries. A thousand clients that all honour the same Retry-After exactly come back together, which is a herd again. Problem 7 asks how to fix that without ignoring the server.
What the budget sees
A failed model call costs no tokens, so run charges the budget only for successful calls. It still costs wall-clock time, and the seconds budget (lesson 6) counts that, but only between calls: a retry sequence runs to its end whatever max_seconds says (lesson 6, question 3). With ChatClient's default timeout of 300 s and four attempts, a hung server holds one call for about 20 minutes. Problem 11 asks how to let the time limit cut it short.
Check yourself
- Your adapter has a bug that raises
KeyError('choices')on every reply. What does a retry-everything policy report after four attempts, and what doesclassifydo instead? - At p = 0.1, how many attempts per call keep a ten-call task's success above 99%?
- Why does full jitter spread the herd more at the fourth retry than at the first?
Answers
- After four attempts and about 2 s of backoff (three waits, uniform on [0, 0.5], [0, 1] and [0, 2]: 1.75 s on average, at most 3.5 s) it reports the same
KeyError, now looking like a flaky server.classifycallsKeyError"fail", so the loop stops on the first attempt and reports the bug as it is. - Three. (1 - 0.1^2)^{10} = 0.99^{10} = 90.4\%, too low. (1 - 0.1^3)^{10} = 0.999^{10} = 99.0\%, just enough. (1 - 0.1^4)^{10} = 99.9\%.
- Each wait is uniform on [0, \text{base} \cdot 2^n], so the window doubles with each attempt, and because the waits add up, the spread of the cumulative retry times grows too. The first retry spreads 1,000 clients over 0.5 s, five buckets. The fourth spreads them over the sum of four draws, about 7.5 s wide.