Timeouts, Retries and Backoff
In this article
- Work out the wait before each new attempt of a failing call and the time left before the step's deadline.
- Tell from a status code whether a call gets another attempt, stops at once or goes to a check of what happened.
- Keep the total number of attempts fixed when several layers of code each want to retry.
- Explain why a write that timed out after it was sent is never retried blind.
- Terms defined here: call timeout, end-to-end deadline, exponential backoff, jitter, retry budget.
Overview
Your support agent is an AI model that acts by calling tools, ordinary functions such as get_order, which reads an order from the order service. The model picks a tool call, the runtime, the code around the model that sends its calls, runs it, and the result goes back to the model; each tool or model call is one step.
Say the agent asks for an order, and nothing comes back. Two seconds pass, then five. Do you keep waiting? If you give up and try again, how long do you pause first, and how many tries are too many? Get them wrong and a short outage turns into a flood of repeated requests, a step that runs past its time limit, or a customer refunded twice.
By the end you can compute the wait before each new attempt and the time left before a step's deadline, tell from an error which calls deserve another try, and cap the total attempts when several layers of code each want to retry.
Two clocks for one step
The running example is a made-up online shop. Its support agent handles one refund request per run: it reads the order with get_order and pays with create_refund, which calls an outside payments provider. Today's request is about order 1042, a 49.00 EUR item. Each run, one pass through a request, has 60 s of working time, split among its steps.
Reading the order is one tool call, and two clocks govern it. The first is the call timeout: how long one attempt may wait for a reply before the caller gives up on it [4]. Temporal, a workflow engine, applies its Start-To-Close Timeout to each attempt separately [3]. In the shop it is 5 s. [3,4]
The second is the end-to-end deadline: one fixed moment by which the whole step must be over, every attempt and pause included; Temporal sets one across all of an activity's retries too [3]. The runtime gives the get_order step 30 s, its share of the run's 60 s and not a second budget, so the step's deadline never falls later than the run's. That is deadline propagation inside one step: every call made for the request carries the same absolute deadline [4]. [3,4]
The clocks meet in two rules. An attempt gets the call timeout or the time left, whichever is shorter: with 3 s left, it gets 3 s. And if the next pause would not end before the deadline, the step stops now. Work that finishes after the caller gave up earns nothing, so a server should check the deadline before each stage [4]. [4]
Which failures get another attempt
A retry helps only when the same call could succeed a moment later, as with a transient fault: a brief loss of network, a service down for a moment, or a timeout because the service is busy, which usually clears after a pause [2]. A permanent error or a malformed request never succeeds, so retrying it only adds load [4]. Each kind is a failure class, and the response tells you which one you have. [2,4]
Read the table by its last column. Names in backticks are the Claude API's own error types: a 429 means the account hit its rate limit or its monthly spend cap, and a spend-cap 429 has no retry-after and keeps failing until access resumes [1]. A 529 means a temporary overload, so it gets the same rules as a 503 [1]. [1]
| Response | What it means | Action [1,2,4] |
|---|---|---|
429 rate_limit_error | too many requests for now | retry, wait at least retry-after; spend cap: stop |
529 overloaded_error | the API is overloaded for now | retry |
| 503 service unavailable | the server is turning work away | retry |
| connection reset | the network dropped the call | retry if read-only; a sent write: reconcile |
| read timeout on a read-only call | no reply in time | retry |
400 invalid_request_error | the request's format or content is wrong | stop |
401 authentication_error | the API key is bad | stop |
403 permission_error | the key may not use this resource | stop |
| tool arguments fail the schema | the request is malformed | stop |
| timeout on a write after sending | unknown | reconcile |
How long to wait
The naive loop retries at once. On a struggling server, Google's SRE book shows 100 failed requests per second becoming 200, then 300, as retries pile on and less useful work gets through [4]. [4]
The first fix is exponential backoff: a longer pause before each new retry, growing exponentially instead of staying fixed [2]. The shop starts at 1 s and doubles, with a cap of 8 s, because unlimited doubling soon reaches 32 s, longer than the step's whole deadline.
Backoff alone still breaks. If a thousand callers fail at the same instant, they all wait exactly 1 s and return together; retries that are not spread out at random can line up into waves that feed themselves [4]. The second fix is jitter. The shop uses full jitter: draw the wait anywhere from 0 to min(cap, base × 2^n), where n is the number of retries so far.
With 4 attempts there are three waits, and their ranges are these.
| Before attempt | n | Ceiling min(8, 1 × 2^n) | Wait drawn from |
|---|---|---|---|
| 2 | 0 | 1 s | 0 to 1 s |
| 3 | 1 | 2 s | 0 to 2 s |
| 4 | 2 | 4 s | 0 to 4 s |
The worst case fits: four attempts that all time out, with the longest draws, take 5 + 1 + 5 + 2 + 5 + 4 + 5 = 27 s, inside the 30 s. On a longer schedule the cap starts to act: a fifth retry has n = 4, so 2^4 = 16 s, held to 8 s.
Last, a server may say when to come back, and the official Claude SDK honours its retry-after header [1]. The shop's loop treats it as a minimum: the wait is the drawn delay or the retry-after, whichever is longer, because the server knows its own state better than a random number does.
Watching it run
Predict this one, taking each random draw at the middle of its range: attempt 1 of get_order hangs until its 5 s timeout, attempt 2 gets a 429 with a retry-after of 3 s half a second after it starts, and attempt 3 answers after 1 s. When does each attempt start, and how much of the 30 s is left?
The whole loop is one runnable Python program. The clock and the tool are fakes made up for the demo, and the middle draw makes the output repeat.
import random
RETRYABLE = {429, 503, 529, 'reset', 'timeout'}
class Clock: # fake clock, so the demo runs at once
t = 0.0
def sleep(self, s): self.t += s
def call_with_retries(call, clock, deadline, timeout, budget, write=False,
base=1.0, cap=8.0, draw=random.uniform):
n = 0
while budget['attempts'] > 0:
budget['attempts'] -= 1
start = clock.t
status, retry_after = call(clock, min(timeout, deadline - clock.t))
left = deadline - clock.t
print(f'{start:4.1f}s -> {status} at {clock.t:4.1f}s, {left:4.1f}s left')
if status == 200:
return 'done'
if write:
unknown = status in ('timeout', 'reset')
return 'reconcile: outcome unknown' if unknown else 'stop'
if status not in RETRYABLE:
return f'stop: {status} will fail again'
ceiling = min(cap, base * 2 ** n)
n += 1
delay = max(draw(0, ceiling), retry_after)
print(f' wait {delay:.1f}s (jitter range 0-{ceiling:g}s, '
f'Retry-After {retry_after:g}s)')
if delay >= left:
return 'stop: next wait does not fit before the deadline'
clock.sleep(delay)
return 'stop: attempt budget used up'
def scripted(replies): # fake tool: (seconds it takes, status, Retry-After)
def call(clock, timeout):
took, status, retry_after = replies.pop(0)
clock.sleep(min(took, timeout))
return ('timeout' if took > timeout else status), retry_after
return call
middle = lambda lo, hi: (lo + hi) / 2 # fixed draw, so the output repeats
get_order = scripted([(99, None, 0), (0.5, 429, 3), (1.0, 200, 0)])
print(call_with_retries(get_order, Clock(), 30, 5, {'attempts': 4}, draw=middle))
create_refund = scripted([(99, None, 0)])
print(call_with_retries(create_refund, Clock(), 30, 5, {'attempts': 4}, write=True))Its real output; the first six lines are the get_order step:
0.0s -> timeout at 5.0s, 25.0s left
wait 0.5s (jitter range 0-1s, Retry-After 0s)
5.5s -> 429 at 6.0s, 24.0s left
wait 3.0s (jitter range 0-2s, Retry-After 3s)
9.0s -> 200 at 10.0s, 20.0s left
done
0.0s -> timeout at 5.0s, 25.0s left
reconcile: outcome unknownAttempt 1 gets the full 5 s, shorter than the 30 s left, and times out at 5.0 s. A read timeout on a read-only call is retryable, so the loop draws from 0 to 1 s and gets 0.5 s. Attempt 2 starts at 5.5 s and gets the 429 at 6.0 s. The draw from 0 to 2 s gives 1.0 s, but the retry-after of 3 s is longer and wins, so attempt 3 starts at 9.0 s. It succeeds at 10.0 s with 20 s left and one attempt unused.
Many layers, one budget
The shop counts attempts, not retries: 4 attempts means the first try plus 3 retries. Now stack three layers, each with its own loop of 3 attempts: the runtime, the tool code and the HTTP client. With the order service down, that is 3 × 3 × 3 = 27 calls for one step.
Google's SRE book names this trap: one request at the top can produce the product of the attempts at every layer, and its example of three layers with 4 attempts each sends 64 [4]. [4]
The shop's fix is one retry budget, counted in attempts. The runtime creates it per step with 4 attempts and hands the same counter to every layer; in the program, budget is a dictionary passed in, never a number a layer owns. The worst case drops from 27 calls to 4. Google's book applies the idea to a whole server: for example 60 retries per minute, past which a request fails [4]. [4]
:::note In practice The official Claude SDK retries connection errors, rate limits and 5xx errors itself, twice by default, and max_retries changes or turns this off [1]. Inside your own loop of 4 attempts, one failing request can then send 12, so set max_retries to 0 or count the SDK's attempts in the shared budget. :::
The write that timed out
Now create_refund times out after the request had left. Did the provider pay? A service can process a request and then fail to send the reply, and a retry then runs the operation twice [2]. For a read that costs nothing; for a refund it is a second 49.00 EUR. [2]
So the call has an uncertain outcome (the uncertain result from Typed Tool Contracts), and the wrapper refuses to retry it: the last two output lines show reconcile instead of a wait. Before anything else the run does reconciliation: it asks the provider whether this refund was made, using an id the shop attaches to each write and sends with it (the idempotency key from Idempotency Keys for Agent Actions), and acts on the answer. An operation with idempotency, one that leaves the same result however many times it runs, is safe to retry; a plain refund is not [2]. [2]
The rule at work
In one line: retry only what can succeed later, wait with jitter, never past the deadline, from one shared budget.
The loop is code. The model never decides whether to retry. A client retry is not an agent step either, yet every attempt still spends seconds of the deadline and, for a model call, tokens and money.
:::note In an interview Asked how to call a flaky dependency: classify the error, back off exponentially with jitter, cap attempts per request and check the deadline before each attempt [4]. The trade-off to name is that each retry adds load to a service that may already be overloaded, and retries at several layers multiply [4]. [4] :::
Common mistakes
- Retrying at every layer. A loop added deep in a library, unseen by the step, multiplies attempts again; pass the shared budget down to it [4]. [4]
- A timeout far from real latency. Too short, and some expensive requests fail every time; far too long, and stuck requests hold resources [4]. Set it from how long the call normally takes. [4]
- Treating every 429 as temporary. A spend-cap 429 carries no
retry-afterand fails until access resumes, so a loop that retries it only spends the deadline [1]. [1]
In production
Log early failed attempts as information and only the final failure as an error, so nobody is paged for calls that recovered [2]. During an incident, a rising retry rate can be a cause that deepens the outage, not only a symptom [4]. [2,4]
Sources
- Claude API errors. Anthropic. Checked 24 Oct 2026.
- Retry pattern (Azure Architecture Center). Microsoft. Checked 21 Nov 2026.
- Detecting Activity failures. Temporal Technologies. Checked 24 Oct 2026.
- Addressing Cascading Failures. Google (SRE Book). Checked 24 Oct 2026.
Check yourself
Check yourself
Question 1 of 13
A call returns 401 authentication_error. What does the retry loop do?
Your first check counts towards your result.