Three layers each retry 3 times. How many calls reach the model?
Question
A support bot handles each message in three layers. The API gateway calls the chat service, the chat service calls an LLM client, and the LLM client calls the model provider. Every layer has retries: 3: after a failure, it tries again up to 3 more times.
The provider has an outage, and every call times out. 200 user messages arrive in one minute.
- How many calls reach the provider for one message? For the whole minute?
- What if the setting meant 3 attempts in total?
- How would you change the retry design?
Show hints (3)
- "ATTEMPTS, NOT RETRIES"
retries: 3means a first try plus 3 retries. How many attempts does each layer make? - "LAYERS MULTIPLY"
Each gateway attempt calls the chat service, which runs its whole retry loop again. The LLM client does the same below it. Multiply, don't add.
- "ONE LAYER RETRIES"
Only the layer right above the provider retries. The others pass the error up. How many calls does one message cause now?
Show checked explanation
64 calls for one message, because retries: 3 means 4 attempts and the layers multiply: 4 × 4 × 4. That is 12,800 calls in the minute instead of 200. With 3 attempts per layer it is 27. Retry in one layer only, right above the model, with backoff, jitter and a retry budget.
def send(layers, model): # layers: attempts per layer, top layer first if not layers: return model() attempts, *below = layers for _ in range(attempts): if send(below, model): return True # success: stop retrying return False # out of attempts: the failure goes up def calls_when_down(layers): calls = [] send(layers, lambda: calls.append(1)) # returns None: a failure return len(calls) print("retries: 3 on each layer:", calls_when_down([4, 4, 4])) print("max_attempts: 3 on each layer:", calls_when_down([3, 3, 3])) print("3 attempts in the LLM client only:", calls_when_down([1, 1, 3]))
retries: 3 on each layer: 64 max_attempts: 3 on each layer: 27 3 attempts in the LLM client only: 3
Code run on CPython 3.12 against the examples above. Checked by an editor other than the author.
Walkthrough
Count attempts, not retries. retries: 3 means a first try plus 3 retries: 4 attempts. A setting like max_attempts: 3 means 3 attempts in total.
Nested retries multiply. Each gateway attempt calls the chat service, which runs its full loop again, and so does the LLM client below it.
| design | attempts per layer | calls per message | calls in the minute |
|---|---|---|---|
retries: 3 everywhere | 4, 4, 4 | 64 | 12,800 |
max_attempts: 3 everywhere | 3, 3, 3 | 27 | 5,400 |
| 3 attempts in the LLM client only | 1, 1, 3 | 3 | 600 |
The first row is the example in the Google SRE book: three layers with 3 retries each make 64 attempts on the database. A provider that is already failing now gets 64 times the normal load, so it takes longer to recover.
A safer design:
- Retry in one layer only, the one right above the failing service. The other layers pass the error up.
- Retry only errors that can pass: a timeout, a 429 or a 5xx. A 400 or 401 will fail again.
- Wait before each retry, about twice as long each time, plus random jitter so clients don't retry together. If the reply has a
Retry-Afterheader, wait at least that long. - Keep a retry budget. The SRE book's rule: a client retries only while retries are under 10% of its requests. With 3 attempts per request alone, a full outage nearly triples the load. With the budget, it grows only about 1.1 times.
- Set one deadline for the whole message, and stop retrying when it has passed.
Common mistakes
- Reading
retries: 3as 3 calls. It is 4. Libraries differ: some count attempts, some count retries. - Adding the layers instead of multiplying: 4 + 4 + 4 = 12. Each outer attempt restarts the whole loop below it, so it is 4 × 4 × 4 = 64.
- Retrying at once, with no wait. Thousands of clients then retry together, just as the provider tries to recover.
- Retrying in two places without knowing it. Many client libraries retry on their own, so check yours before you add your own loop.
Follow-up questions
- "An agent's
issue_refundtool call timed out. Can you retry it?" Not blindly. A timeout doesn't tell you whether the refund happened, so a retry can pay twice. Send an idempotency key, likerefund-order-1001. The payment service returns the first result for a repeated key, so the retry is safe. - "The provider is down for ten minutes. Should the client keep retrying?" No. After enough failures, stop calling it for a while: fail fast or switch to a backup model. That is a circuit breaker. Later, let a few trial requests through to see if it has recovered.
What a strong answer covers
Separates attempts from retries and multiplies through the layers: 64, or 27 with 3 attempts. Retries in one layer only, only on retryable errors, with backoff, jitter, Retry-After and a retry budget. Uses an idempotency key before retrying a tool call with side effects.
Open in interactive practice deck →