cd /news/ai-agents/your-ai-agent-retried-the-tool-call-… · home › topics › ai-agents › article
[ARTICLE · art-174158] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=· neutral

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.

recommendation

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.

more in playbook: Designing agent systems
by read3 min views1 publishedNov 22, 2026

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.

#!/usr/bin/env python3
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__":
    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.

── more in #ai-agents 4 stories · sorted by recency
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/your-ai-agent-retrie…] indexed:0 read:3min 2026-11-22 · —