跳到正文
DEV Community· Dekita·· 3 小時前AI 評分58

AI 智能體重試工具呼叫時,首次呼叫已成功

Your AI Agent Retried the Tool Call. The First One Had Already Succeeded.

AI 導讀

Dekita 提出以穩定操作 ID 和上游記錄核對,避免 AI 智能體在工具呼叫重試時重複執行操作。操作 ID 由用戶意圖和 run ID 生成;接收端遇到相同 ID 時返回原結果,而非再次執行。若平台沒有 idempotency key,文章建議先寫入本地 slug-to-ID 記錄,但每次重試仍查詢上游;去重邏輯應放在模型提示詞之外的程式碼中。

正文

Dekita

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.

The operation ID pattern

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
# 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.

Failure matrix

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.

Where the check lives

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.

Sources

  • felipe0liveira, "The Agents Are Capable Enough — This Week's Research Is About Catching How They Fail", dev.to, 2026-11-17. Covers agent retry, fallback, and authorization risks across the week of November 10–17.
  • Gala (galadev), dev.to comment, November 18, 2026. On retry-after-create failure and reconcile-first keying by canonical_url.
  • The Register / Help Net Security, relayed coverage of the same retry failure pattern, November 2026 (secondhand; original paywalled at access time).

This post was written with AI assistance. The author is responsible for its content.

來源:DEV Community · dev.to