Most queue libraries ship with a retry count. For a cheap, idempotent call it is a reasonable default. For a call that can take forty seconds and cost several cents, the same default is a purchasing decision that somebody made by not making it.
This article is about the layer between your product and a model API: the workers, the queue, the retry rules and the dead-letter queue. My position sits awkwardly with the standard reliability advice, which is right for the problem it was written for. In a queue that feeds an LLM, a retry is a purchase, not a free second chance. The retry policy should be written as a budget of money and time per job, set separately for each class of failure, and not as a number of attempts.
What the standard advice gets right
The reliability canon is good, and I would keep it. Backoff with jitter spreads retries out so that a struggling service is not hit by a synchronised wave. Marc Brooker's Builders' Library article calls retries "selfish": the client spends more of the server's resources to improve its own odds. Google's SRE book does the multiplication that catches teams out. If the backend, the frontend and the JavaScript layer each retry three times, one user action becomes 4^3, or 64 attempts, on the database. It recommends a per-process retry budget, with 60 retries a minute as its example. The AWS SDKs turn the idea into code. In the behaviour AWS documents (opt-in through an environment variable until it becomes the default), a token bucket holds 500 tokens, charges 14 for a transient retry and 5 for a throttled one, refills as requests succeed, and stops retrying when it is empty, on top of a default of three attempts in standard mode.
Brooker's advice to retry at a single point in the stack is given for low-cost operations, and the same article notes that retrying only at the top may waste work from previous calls. Most of these designs were written to protect the server from its clients, and the cost of a failed attempt is mostly the server's time. With an LLM downstream the cost also lands on you, in tokens, and the work a retry throws away can be the costliest call in your pipeline.
Seven ways a call fails, and who pays for each
A single max_attempts treats all failures as one thing. The second column is my reading of the documentation I could verify, with Anthropic's as the example.
| Failure | Did the provider do the work? | Does a blind retry help? | What I would do |
|---|---|---|---|
| Rate limit (429 with a retry-after header) | No, rejected up front | Yes, but only after the wait; the documentation says earlier retries fail | Wait the stated time. One shared limiter, not one per worker |
| Tier spend cap reached (also a 429, but with no retry-after) | No | No. It keeps failing until access resumes, and the SDK's automatic retries fail too | Do not retry. Park the lane until access resumes and alert a person |
| Overload or server error (5xx, 529) | Unclear; test it | Often | Capped backoff with jitter, charged to the retry budget |
| Timeout or dropped connection | Unknown | Maybe | Treat as unknown. Prefer a longer deadline to a retry, and give every attempt the same job ID |
| Error after a 200, mid-stream | Partly | Resuming can beat restarting | Decide per job whether a partial answer is usable |
| Bad generation: a well-formed answer that fails validation | All of it | Only if something changes | Retry with the reason attached, or move up a model tier. Never a plain repeat by default |
| Duplicate delivery: the queue's lease expired while the first worker was still generating | All of it, twice | Not a retry at all | Fix the lease and key the result by job |
A caution on the second column: billing rules for calls you abandon differ between providers, and I found no primary source I would quote. Test yours. Start a long generation, cut the connection after two seconds, and compare the usage dashboard with what you think you ran.
The first three rows are what the reliability literature was written for: honour the header, back off with jitter, cap the attempts. The rest is where the money can go unseen. A timeout leaves you not knowing. A mid-stream error leaves you holding half an answer you have paid for. A bad generation returns a healthy 200. A duplicate delivery raises no error anywhere.
A retry has a price, and a yield
Every figure in this section is illustrative, not a measurement.
Take a nightly job that scores 50,000 documents, each call sending 20,000 input tokens and returning 1,200. At round prices of $3 per million input tokens and $15 per million output tokens (not any provider's list price), one attempt costs $0.078 and the first pass costs $3,900.
Say 6 per cent of jobs fail validation: 3,000 jobs. The inherited policy allows two more blind attempts each. What that costs depends on a number almost nobody measures, the retry yield: the share of retried jobs that come back valid.
If the yield is constant, you pay for 1 ÷ yield attempts per recovered job, however many retries you allow, so the unit price is fixed and the count only scales the total. At a 50 per cent yield a recovered job costs two attempts, about $0.16. At 25 per cent it costs four, about $0.31. At 10 per cent it costs ten, $0.78, which is ten times the price of a job that passed first time. In that last case the two retries cost $444.60 and recover 570 jobs. Against a $3,900 first pass that is about 11 per cent extra, which is not ruinous. The point is a unit price nobody chose.
In practice yield falls with each retry: the first rescues the near-misses and the second meets mostly the unrecoverable. Suppose 40 per cent of failing jobs are recoverable and each retry rescues half of those still left. The first retry then yields 20 per cent, about $0.39 per recovered job, and the second 12.5 per cent, about $0.62. The rule that falls out: retry only while a recovered job is worth more than the attempt cost divided by the yield you are actually seeing.
What a recovered job is worth is a product question, and it rarely gets asked, because the retry count lives in a config file that nobody thinks of as a price list.
Yield matters more than the count because bad generations are not random. Sampling variance can rescue an answer that sat near a decision boundary. It cannot rescue a document where the rubric and the input disagree, and a plain repeat buys the same wrong answer again. Measuring tells you which kind of failure you have. A retry that changes something, such as the validator's reason attached to the prompt, a stronger model tier or a shorter context, has a yield you can measure per change. A retry that changes nothing is a lottery ticket.
The validation article makes the in-run version of this argument: bound the repair loop and attach the reason. This is the queue-level counterpart.
Where the second payment hides
The lease
Standard queues deliver at least once. In SQS the default visibility timeout is 30 seconds. A message that is not deleted before it expires becomes visible again and can be taken by another consumer, and AWS says it does not guarantee against a second delivery even inside the window. The documented remedy is a heartbeat that extends the timeout while you work, up to 12 hours from the first receive.
Now put an LLM behind that default. A long document, a reasoning-heavy model or a slow afternoon at the provider can push a generation past 30 seconds. The message reappears, a second worker takes it, and both finish. Both are billed, neither raises an error, and the queue's metrics show an ordinary redelivery. Set the timeout from the p99 generation time plus a margin, not the median. Heartbeat for anything long. Store results under the job ID, so the second finisher finds the answer already there and discards its own. Other brokers call it a lease or an acknowledgement deadline; check yours.
Stacked retries
Count the layers that can retry one job: the provider's SDK, your framework, the worker, the orchestrator and the user's refresh button. Anthropic's SDK retries transient failures twice by default and honours retry-after; the AWS SDKs default to three attempts. Give three attempts to each of three layers, say the refresh button, the worker and the SDK, and one job can make 27 calls on a persistent fault, a worst case nobody chose. Five layers would make 243. For a bad generation the SDK never sees a failure, because the call returned a 200, so the full-price worst case is the layers above it: 3 x 3 = 9 complete generations for one job.
Many of those extra calls are rejected before any tokens are generated, so they show up as busy workers and a backlog that grows exactly when the provider is struggling, not on the invoice. The ones that do reach the invoice are the timeouts. One layer should own retries: the one that knows the job's deadline and spend, which is the worker. Set the SDK's own retries to zero for queued work so that the budget sees every attempt.
The dead-letter queue as a junk drawer
SQS defines the redrive threshold, maxReceiveCount, as the number of times a consumer can receive a message before it moves to the dead-letter queue. That counts deliveries, not reasons. After a forty-minute provider incident, every job that happened to run out of receives lands in the same place as the messages that are actually poisoned, and someone has to tell them apart by hand.
A bad generation is worse, because it is not a failed delivery at all. The consumer processed the message and got a 200 and a wrong answer. If your code acknowledges the message and writes the failure to a table, you have built a second queue that nobody named. If it raises an exception to force a redelivery, you have turned a judgement about quality into a transport retry, billed at full price.
Two destinations with different owners work better. A dead-letter queue for transport failures, redriven when the incident is over. And a decision lane for bad generations, carrying the validator's reason code, a hash of the input, the prompt and policy version, and the spend so far. One SQS detail: for standard queues the expiry clock keeps running from the original enqueue time, so a dead-letter queue with the same retention as its source can delete a message that has already spent most of its life there. Set the dead-letter retention longer.
A budget instead of a count
Write each job type a small spec before you write its consumer: a deadline (how long can anyone wait), a cost ceiling (the most this job may spend across all attempts, the per-task ceiling argued for in the cost-control article applied to a queued job instead of an agent run), retry rules by failure class, and a lane.
Then replace the attempt counter with a spend budget. It is the same idea as the AWS SDK's token bucket, denominated in money. Keep one budget per job type, and let the failure class decide whether a retry may draw on it at all. Every first attempt deposits a small fraction of its cost. Every retry withdraws its estimated cost. When the bucket cannot cover a retry, the job goes to the decision lane instead.
class RetryBudget: # one per job type; add a lock if workers share it
def __init__(self, ratio, cap):
self.ratio, self.cap, self.balance = ratio, cap, cap
def on_first_attempt(self, cost):
self.balance = min(self.cap, self.balance + cost * self.ratio)
def try_retry(self, estimated_cost):
if self.balance < estimated_cost:
return False # fail fast: route to the decision lane
self.balance -= estimated_cost
return TrueThe ratio is the policy in one number. At 0.1, retries can consume about a tenth of what first attempts spend over time. I would choose it from measured yield, then check it against the invoice. During an incident the budget empties by itself and the system fails fast and cheaply, which is the property the AWS SDK gets from its own bucket, now in units the finance team can read.
Separate the lanes by whether anyone is waiting.
- Interactive. A person is waiting. Short deadline, live API, a small budget.
- Deferrable. Nobody is waiting. Send it through the provider's batch interface. Anthropic's Message Batches charges 50 per cent of standard prices, says most batches finish within an hour, expires them after 24, and does not bill requests that errored, were cancelled or expired. Read other providers' terms.
- Decision. Bad generations and exhausted budgets, waiting for a person or a changed prompt.
The batch lane is the cheapest change on this list. For the nightly job above, a batch interface halves the first pass ($3,900 to $1,950 at the worked example's prices) before any tuning of the retry curve, and transport errors stop costing anything. The price is latency you were not using.
The decision for Monday
Six changes, in the order I would make them:
- Count the layers that can retry one job and leave exactly one of them switched on.
- Log retry yield for every failure class. Start with bad generations, because that is where the money is.
- Compare the queue's visibility timeout with the p99 generation time. If the timeout is shorter, you are paying twice today.
- Split dead letters from decisions.
- Move work nobody is waiting for onto a batch interface.
- Put a deadline and a cost ceiling in each job's spec, and have the person who owns the product set them.
The last one is the one I would argue for hardest. The cost ceiling is not an infrastructure parameter. It is the answer to the question "what is a failed job worth to us?", and engineering cannot answer that on its own.
Where this is weakest
If your monthly model bill is smaller than an engineer's day, set the attempts to three and move on. A spend budget is overhead at that scale.
Yield needs enough failures to measure. At 300 retried jobs, a measured 25 per cent yield carries roughly plus or minus 5 points at 95 per cent confidence; below a few hundred per failure class, treat it as a hint.
The provider details come from public documentation read on 9 October 2026. Limits, defaults and billing terms change, so treat the table as a classification to adapt, not a specification to copy.
What would change my mind: measured yield on bad generations, at your settings, that is high and stable, say above half. Then a plain retry is the cheapest fix and my objection becomes a tuning exercise. The same goes if providers stop charging for failed and abandoned calls, or if an attempt costs less than the effort of budgeting it.
The short version
Retry defaults were written to protect servers from their clients. With an LLM downstream, every attempt is also a purchase. Let one layer own retries, budget them in money and time per failure class, and measure yield before paying for more. Set the lease for your slowest generation. Give bad generations a lane with an owner. Send work nobody is waiting for through a batch interface. Have the person who owns the product set the cost ceiling.
None of this makes a model more reliable. It makes the cost of its unreliability a number you chose.
Illustrations generated with AI.

