Why retries make AI outages more expensive
Your AI provider has a slow ten minutes. Your retry code turns that into a bigger bill, a backed-up queue and a customer who got the same email twice.
The business case
When an AI provider slows down, badly set retries multiply your usage, keep customers waiting and can repeat actions like refunds. You end up paying more during the hours you're getting less. Most of the fixes below are small code changes. The rest of this post is for whoever will make them.
Jump to: the implementation / questions for your team
Retries feel like the responsible thing to add. A call failed, so try again. For one call, that's right. Across a busy system during a slowdown, the same code can do more damage than the original failure.
Three things go wrong, and AI workflows are unusually good at hitting all of them.
The math nobody does
Say a user request goes through your API, then a workflow step, then a client library that calls the model. Each one was given retries by someone being careful, and each tries up to 4 times.
When the model is down, those attempts multiply: 4 × 4 × 4 = 64 calls for one user request. Nobody chose 64. Three reasonable settings produced it, and it only shows up when things are already going badly.
That's the retry storm. The provider slows down, your calls time out, and your system answers by sending more traffic to the one thing that's struggling.
Why AI makes it worse
A normal API call takes milliseconds and costs almost nothing. A model call can take 30 seconds and cost real money. That changes the failure in two ways.
First, timeouts lie. When your client gives up at 30 seconds, the provider may still be working, and you may still be charged. The retry is then a second paid run of the same job.
Second, rate limits are shared. Most providers limit you per account, often by tokens as well as requests. A nightly batch job that's retrying hard can use up the quota your customer-facing chat depends on. Two features that have nothing to do with each other go down together.
Fix the amplification first
- Retry in one layer. Pick the layer closest to the model and turn retries off everywhere else. Check your SDK while you're at it, because many retry by default.
- Only retry what can succeed. A 429 or a 503 might work later. A 400 won't, and neither will a prompt that's too long.
- Wait, with randomness. If 500 workers back off for exactly 2 seconds, they all come back together. Add jitter, and honor
Retry-Afterwhen the provider sends it. - Set a retry budget. Cap retries as a share of normal traffic, say 10%. When the budget is gone, fail fast. A budget is what stops a retry policy from becoming a storm, because it notices the whole system is failing, which a single call can't.
public async Task<T> CallWithRetryAsync<T>(
Func<CancellationToken, Task<T>> call,
RetryBudget budget,
CancellationToken ct)
{
for (var attempt = 0; ; attempt++)
{
try
{
return await call(ct);
}
catch (ModelApiException ex) when (
ex.IsRetryable && attempt < 3 && budget.TrySpend())
{
var cap = Math.Min(30_000, 1_000 * Math.Pow(2, attempt));
var jitter = Random.Shared.NextDouble() * cap;
var delay = ex.RetryAfter
?? TimeSpan.FromMilliseconds(jitter);
await Task.Delay(delay, ct);
}
}
}
Then push back on incoming work
Retries are half of it. The other half is what you do with new work while the provider is slow.
An unbounded queue feels safe because nothing gets rejected. It really just hides the problem until the items at the front are hours old and nobody wants the answers anymore. You're now paying to generate replies to customers who left.
Limit how many model calls run at once, and give each kind of work its own limit so batch jobs can't starve live users. Keep the queue bounded. When it fills, decide on purpose: reject, defer, or fall back to a cheaper model. Stale work should be dropped, not processed.
If failures keep coming, a circuit breaker stops calls for a short period and then lets a few through to test recovery. The provider gets room to recover, and your users get a fast error instead of a 30-second wait for the same error.
The duplicate email
This is the one that reaches customers. An agent calls a tool to send an email. The email goes out, but the response is lost. The retry sends it again. Swap "email" for "refund" and it stops being funny.
The fix is an idempotency key: an ID that stays the same across every retry of one action, built from something like the workflow ID and step name. If you generate it inside the retry loop, it does nothing.
var key = $"{workflowId}:{stepName}";
await payments.RefundAsync(
order,
new RefundOptions { IdempotencyKey = key });
Some APIs handle this for you. Stripe does. When the system on the other side doesn't, you can record the key in your own database, but there's still a gap: if you crash after the send and before the write, you can't know whether it happened. At that point you have to choose between maybe sending twice and maybe not sending. For a refund, I'd stop and ask a person.
Keep in mind that an LLM can also repeat itself. An agent that decides to call the same tool twice isn't a retry at all, so the key has to be tied to the action, not to the network call.
Test it before it tests you
None of this means much until you've watched it fail. Put a fake provider in staging that returns 429s, hangs for 60 seconds, and drops the response after doing the work. Then count: how many calls went out per request, how old the queue got, and whether any action happened twice.
That last number should be zero. If you've never measured the first one, it's probably higher than you think.
Questions to ask your team
- How many model calls can one customer request trigger in the worst case?
- Do we have a hard spending cap, and what happens when we hit it?
- If our AI provider goes down for an hour, what do customers see?
- Can an automated action, like a refund or an email, happen twice? How would we know?
- Can a background job use up the capacity our customers depend on?
If nobody can answer those, that's where I'd start. I build and run AI systems that keep working under heavy load. Send me a message on LinkedIn with what yours does, and we can work out where to look first.