跳到正文
DEV Community· Rausal Bahtiar Fadhli·· 2 小時前AI 評分58

為盲目重試的智能體工具呼叫設計冪等性帳本

Idempotency Ledger for Agent Tool Calls That Retry Blindly

AI 導讀

Rausal Bahtiar Fadhli提出以冪等性帳本處理智能體工具呼叫重試,使用確定性意圖雜湊關聯同一操作的重試,以避免重複副作用。帳本狀態分為 reserved、executed 和 settled,重試時分別等待、返回已儲存的參照或純重播。作者建議在上線前測試逾時、程序中止,以及帳本記錄與下游物件數量是否完全相符。

正文

An agent retry is not a request repeat. It is a new request that looks identical. When a model re issues a tool call after a timeout, the downstream system sees two valid writes, two charged payments, or two created tickets. The failure is structural: the caller carries state in its prompt, the callee carries state in its database, and no key ties the two together.

Why prompt level retry logic breaks at scale

Most agent frameworks implement retry as a loop around the tool function. The loop lives in memory. A process restart, a context compaction, or a supervisor failover erases the attempt counter while the side effect survives. The result is at least once execution with no deduplication, which the operations team discovers through a duplicate invoice rather than a stack trace.

Measurement matters here. A 2026 study titled "Do LLM Agents Act Exactly Once?" measures idempotency violations under retries, which indicates that test suites never forcing a retry cannot surface this class of failure (doi 10.5281/zenodo.24166026).

The write ahead ledger contract

Before the tool executes, the agent writes a row keyed on a deterministic intent hash. The hash covers tool name, normalized arguments, tenant, and a caller supplied window id. The row stores three fields that matter: key, state, and result reference. State moves through reserved, executed, and settled. A retry with the same key reads the existing row instead of calling the tool again.

Stage aware retry plus idempotency plus exactly once semantics is not a novel pattern, it is a well documented pipeline property (doi 10.52783/jisem.v11i2s.14608). Agents fail it because the retry decision is made by a language model rather than by the pipeline.

Three states that prevent duplicate side effects

Reserved means the intent is claimed but the tool has not returned. Executed means the tool returned a reference and the row is no longer eligible for a fresh call. Settled means the downstream effect is confirmed by reconciliation. A retry that lands on reserved must wait, not re issue. A retry that lands on executed must return the stored reference. A retry that lands on settled is a pure replay.

Dead letter handling belongs in the same table. When a reserved row exceeds its lease without reaching executed, the row is released and the intent is parked for human review rather than retried forever, a strategy already standard in payment pipelines that combine idempotency with dead letter queues (doi 10.32628/ijsrst52310495).

What breaks when the key is derived by the model

Keys must be computed by code, never generated by the model. A model that writes its own key will produce a new key on retry because the prompt changed by one token, which defeats the entire mechanism. The safest design derives the key in the tool wrapper from the normalized payload, and treats any model supplied identifier as metadata only.

Research on counterfactual repair of LLM agent failures (doi 10.48550/arxiv.2605.25338) addresses attributing failures to specific actions, which presupposes that a failing action keeps a stable identity across attempts.

Verification checklist before production

Force a timeout after the tool returns but before the agent records success. Confirm the second attempt reads executed and returns the original reference. Kill the process mid flight and confirm the lease expiry releases the row without duplicating the downstream object. Count rows in the ledger against objects in the downstream system and require an exact match. Any mismatch is a duplicate, not a rounding error.

Canonical: https://rausalbahtiar.dev/blog/agent-tool-call-idempotency-write-ahead-ledger

來源:DEV Community · dev.to