By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Retries are the most common source of duplicate side effects in any distributed system, and LLM features add a second source: the model may be called again after a partial tool execution. The rule is not "retry on failure"; it is "retry only calls that are safe to repeat, inside the retry budget, with jitter, and only for retryable failures."
Reads are safe to retry. A chargeable write is safe only if it carries an idempotency key and the executor can prove the key has not already produced an action. When that proof is impossible — because the downstream system has no idempotent API — the right answer is not a retry; it is reconciliation, a dead-letter queue and a human.
from dataclasses import dataclass, field
@dataclass
class ActionRecord:
key: str
status: str
action_id: str
@dataclass
class IdempotentExecutor:
"""Executes a chargeable action at most once per idempotency key."""
records: dict[str, ActionRecord] = field(default_factory=dict)
def execute(self, key: str, action: str) -> tuple[str, bool]:
if key in self.records:
return self.records[key].action_id, True
action_id = f"act_{action}_01"
self.records[key] = ActionRecord(
key=key, status="executed", action_id=action_id
)
return action_id, False
executor = IdempotentExecutor()
first_id, first_replay = executor.execute("idem-77c1", "issue_credit")
second_id, second_replay = executor.execute("idem-77c1", "issue_credit")
print("first replay:", first_replay)
print("second replay:", second_replay)
print("same action id:", first_id == second_id)
print("records:", len(executor.records))| Failure | Retry automatically? | Idempotency needed? | Handling |
|---|---|---|---|
| Timeout before the provider accepted the request | Yes, once, with jitter | Yes for writes | Safe only because the key can prove it did not execute |
| 5xx after the provider may have accepted | No; reconcile first | Yes | Look up the key or downstream status before retrying |
| 4xx validation or auth error | No | No | Fix the request; a retry repeats the same mistake |
| Duplicate webhook or queue delivery | N/A | Yes | Deduplicate on the event id before any effect |
| Abandoned message after max attempts | No | Yes | Dead-letter queue with the payload and attempt history |
| Partial tool execution (read succeeded, write uncertain) | No | Yes | Reconcile by key; never guess whether money moved |
Key takeaways
Retry only safe calls, inside a retry budget, with jitter, and only for retryable failures.
Every chargeable write carries an idempotency key; the executor proves the key has not already acted.
5xx after possible acceptance needs reconciliation, not blind retry; 4xx needs a fix.
Dead-letter queues need context, alerting and reviewed replay; exactly-once across HTTP does not exist.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.