Your AI Agent Retried the Tool Call. The First One Had Already Succeeded. A developer outlined a pattern for preventing duplicate side effects when LLM agents retry tool calls, arguing that missing idempotency causes double charges, duplicate records and repeated emails. The approach generates a stable operation ID from the user's intent plus the run ID before any state-changing call, has the receiver deduplicate on that key, and reconciles against upstream state before creating anything new. The author stresses that the deduplication gate belongs in code outside the model, since "prompts are probabilistic; a model can forget or misread its own instruction. Your agent calls a tool. The call succeeds. Then the agent retries the same call, and the second one succeeds too. Now you have two charges, two records, or two emails where one was intended. This is not a theoretical failure. It happened last week. A dev.to comment on November 18 described exactly this: a user creates a record on a platform they do not control, the create succeeds, but the local bookkeeping write fails. On retry, the platform creates a second record. The author concludes that "retry becomes recover, not create again" — and that the only durable fix is to query upstream for an existing record keyed by a stable identifier before creating anything new. The root cause is missing idempotency. An idempotent operation can be applied multiple times without changing the result beyond the first application. Most LLM agents do not have this property built in. They see a network error or a timeout, assume the call never landed, and send it again. The first call had already landed. Here is the pattern, and a check you can paste into your own agent runner. Before any state-changing call, generate a stable operation ID from the user's intent plus the run ID. Send it with the call. The receiving side stores it. If the same ID arrives again, the receiver returns the original result instead of executing twice. bash /usr/bin/env python3 idempotent retry.py Stable operation ID + reconcile-before-create. Exit: 0=allow, 1=duplicate, 2=unknown. import hashlib, json, sys def operation id user intent: str, run id: str - str: base = f"{run id}:{user intent}".encode return hashlib.sha256 base .hexdigest :24 def gate existing keys: set, op id: str - tuple: if op id in existing keys: return False, "duplicate operation id", op id return True, "proceed", op id if name == " main ": Example: user wants to "create order 442" in run "run-773" op = operation id "create order 442", "run-773" known = {"abc123def456"} normally loaded from your durable store allowed, reason, key = gate known, op print json.dumps {"allowed": allowed, "reason": reason, "operation id": key} sys.exit 0 if allowed else 1 The crucial part is not the hash. It is that the operation ID is computed before the call, and the receiver deduplicates on it. If your platform does not expose an idempotency key, keep your own local slug-to-ID record written before the first create, and refuse to create if the key already exists. The local record is an optimization, never the source of truth — query upstream on every retry. | Scenario | First call | Retry | Without idempotency | With idempotency | |---|---|---|---|---| | Network timeout after success | succeeded | retried | duplicate record / double charge | returns original result | | Local write failed after remote success | succeeded | retried | duplicate | reconcile-first: skip create | | Transient API 5xx before any write | failed | retried | single record | single record | | Genuine second user request | created | new intent, new ID | two records correct | two records correct | The last row matters: idempotency is scoped to the same intent within the same run. A real second request in a new conversation gets a new operation ID and proceeds normally. Do not put the deduplication logic inside the model prompt. Prompts are probabilistic; a model can forget or misread its own instruction. Put it in code, outside the model, where the agent cannot talk it out of checking. The agent proposes; the gate decides. This post was written with AI assistance. The author is responsible for its content.