{"slug": "your-ai-agent-retried-the-tool-call-the-first-one-had-already-succeeded", "title": "Your AI Agent Retried the Tool Call. The First One Had Already Succeeded.", "summary": "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.", "body_md": "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.\n\nThis 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.\n\nThe 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.\n\nHere is the pattern, and a check you can paste into your own agent runner.\n\nBefore 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.\n\n``` bash\n#!/usr/bin/env python3\n# idempotent_retry.py\n# Stable operation ID + reconcile-before-create. Exit: 0=allow, 1=duplicate, 2=unknown.\nimport hashlib, json, sys\n\ndef operation_id(user_intent: str, run_id: str) -> str:\n    base = f\"{run_id}:{user_intent}\".encode()\n    return hashlib.sha256(base).hexdigest()[:24]\n\ndef gate(existing_keys: set, op_id: str) -> tuple:\n    if op_id in existing_keys:\n        return False, \"duplicate operation id\", op_id\n    return True, \"proceed\", op_id\n\nif __name__ == \"__main__\":\n    # Example: user wants to \"create order #442\" in run \"run-773\"\n    op = operation_id(\"create order #442\", \"run-773\")\n    known = {\"abc123def456\"}  # normally loaded from your durable store\n    allowed, reason, key = gate(known, op)\n    print(json.dumps({\"allowed\": allowed, \"reason\": reason, \"operation_id\": key}))\n    sys.exit(0 if allowed else 1)\n```\n\nThe 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.\n\n| Scenario | First call | Retry | Without idempotency | With idempotency | \n|---|---|---|---|---|\n| Network timeout after success | succeeded | retried | duplicate record / double charge | returns original result | \n| Local write failed after remote success | succeeded | retried | duplicate | reconcile-first: skip create | \n| Transient API 5xx before any write | failed | retried | single record | single record | \n| Genuine second user request | created | new intent, new ID | two records (correct) | two records (correct) | \n\nThe 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.\n\nDo 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.\n\n*This post was written with AI assistance. The author is responsible for its content.*", "url": "https://wpnews.pro/news/your-ai-agent-retried-the-tool-call-the-first-one-had-already-succeeded", "canonical_source": "https://dev.to/dekita/your-ai-agent-retried-the-tool-call-the-first-one-had-already-succeeded-b3c", "published_at": "2026-11-22 12:09:28+00:00", "updated_at": "2026-11-22 12:12:26.748705+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "large-language-models"], "entities": [], "also_reported_by": [], "takeaway": "Compute a stable operation ID from the user intent plus run ID before every state-changing tool call, have the receiver return the original result on a repeat ID, and query upstream for an existing record before creating rather than trusting a local dedup record.", "recommendation": {"advice": "Gate state-changing tool calls in code, not in the prompt: derive an operation ID from intent plus run ID, deduplicate on it at the receiver, and reconcile upstream before any create.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/your-ai-agent-retried-the-tool-call-the-first-one-had-already-succeeded", "markdown": "https://wpnews.pro/news/your-ai-agent-retried-the-tool-call-the-first-one-had-already-succeeded.md", "text": "https://wpnews.pro/news/your-ai-agent-retried-the-tool-call-the-first-one-had-already-succeeded.txt", "jsonld": "https://wpnews.pro/news/your-ai-agent-retried-the-tool-call-the-first-one-had-already-succeeded.jsonld"}}