Retrying a failed request is an obvious safety net, and easy to build backwards. It hinges on one question: did the first attempt actually fail?
Imagine you send a message and get no confirmation. Do you send it again?
If the first one never went out, yes. If it did go out and only the reply got lost, the other person now gets your message twice. You cannot tell which one happened. That is the whole problem with retrying a failed request. It is why the most obvious safety measure is also one of the easiest to get backwards.
"Could not connect" means the work didn't happen. A timeout (you stopped waiting and gave up) tells you almost nothing. The request might never have arrived, or been refused, or run completely: saved, charged the card, sent the email, with only the reply lost.
A timeout only tells you that you stopped waiting, not what the other side did. So a naive retry claims something it can't prove, and sometimes you end up with two of something that should exist once.
Before sending, make a unique ID for the operation, not for each attempt. Every retry carries the same ID, and the server writes it down next to the result. This is called an idempotency key. Idempotent is a scary word for a simple thing: doing it twice has the same effect as doing it once, like pressing an elevator button (pressing it ten times doesn't call ten elevators).
def create_order(key: str, payload: dict) -> Order:
existing = store.get(key)
if existing:
return existing # this exact operation already ran
order = process(payload)
store.put(key, order) # same transaction as process()
return order
Saving the key and doing the work must succeed or fail together, in the same database and the same transaction (one all-or-nothing step). Otherwise there's a moment where the work is done but the key isn't saved, and you've rebuilt the bug. The caller makes the ID, because only the caller knows whether two attempts mean the same thing.
Reading is safe to repeat as far as correctness goes. But the read that gets hammered is usually the expensive one, like a big report or a heavy search. Retrying it three times during a slowdown triples the load on something that's already struggling.
Think of a slow web page. Everyone hits refresh, so it gets slower. The same thing happens here. A service answers at half speed. Every caller times out and retries, so twice the work hits half the capacity. It slows down more, and more retries fire. This is a retry storm, and the safety net is what stops the recovery.
Three things keep it in check, and you need all three.
All of this gets sharper with AI models, because calls are slow, expensive and often rate-limited (you only get so many per minute). A duplicate costs money. Queues can grow long before anyone notices. And "too many requests" is exactly what a naive retry loop turns into a self-made outage.
If a generated answer is worth saving, it deserves an idempotency key. Turn the question and settings into an ID, and store the answer under it, so a repeat costs just a lookup. You get retry safety and a cache from one mechanism.
Retrying is a way of adding more load. It only helps when three things are true.