Skip to content
Apixo
Blog
news· 4 min read· via Towards AI

Why AI Agent Retries Trigger Double Payments and How Idempotency Solves It

A simulation of AI agent tool execution reveals how network timeouts cause duplicate transactions, and why status checks fail where database-level idempotency keys succeed.

Why AI Agent Retries Trigger Double Payments and How Idempotency Solves It

When autonomous AI agents interact with the physical or financial world, they rely on external tool calls to look up records, send messages, or issue payments. However, because these calls travel over unreliable networks where packets can drop or delay, typical timeout handling often involves retrying the request. For read-only queries, repeating an action carries no downside. But when handling money or mutating state, a blind retry can duplicate transactions even when the underlying language model made no mistake at all.

A developer experiment using an in-memory SQLite service in Python evaluated what happens when an automated agent attempts to issue a $25 refund across a faulty connection. In this setup, the language model was intentionally excluded to isolate post-decision execution. The test modeled three standard network failures: a lost request that never arrives, a lost response where the write completes but the agent receives no confirmation, and a slow commit where the refund writes to the database only after the agent gives up waiting.

Why naive retry strategies fail

The benchmark compared four common approaches to handling failed tool attempts:

  1. Blind retry: Dispatching the exact same refund request again.
  2. New key on every try: Generating a fresh unique identifier on each attempt.
  3. Check status, then retry: Querying whether a refund exists for the order before sending another request.
  4. One key per refund: Generating a single operation key upfront, passing it on every attempt, and using a UNIQUE database constraint to reject duplicate entries.

Generating a new key on every attempt performed identically to sending no key at all, as the receiving service had no way to identify repeats. More surprisingly, the "check status, then retry" pattern proved vulnerable to race conditions caused by slow commits. When a query asked if a refund had occurred, the database truthfully returned "no refund yet." Immediately afterward, the original delayed write completed, followed quickly by the agent's retry—resulting in a second refund being written in the gap between the check and the write.

The only approach that held up was enforcing idempotency inside the database transaction itself. By declaring an op_key column as UNIQUE, SQLite raised an IntegrityError if a repeated identifier arrived. The service caught the error, verified that the order and amount matched, and safely returned an "already done" response. When tested with 20 simultaneous insert operations sharing one key across separate connections, only one row was written.

The financial cost at scale

To observe the financial implications across volume, the author ran a simulation of 10,000 fictional orders facing an equal distribution of the three failure modes across a 20% failure rate.

The results demonstrated a stark divide in reliability:

  • Blind retries: Generated 1,654 duplicate refunds across 1,432 orders, causing $41,350 in erroneous payouts.
  • New key per try: Produced the exact same loss of $41,350 across 1,432 orders.
  • Check status first: Double-refunded 650 orders, losing $16,250 due to late-arriving requests.
  • One key per refund: Resulted in zero duplicate refunds.

Even when the simulated failure rate was lowered to 1%, blind retries still triggered 68 double-refunds across 10,000 orders.

However, idempotency keys do not guarantee that an operation succeeds on the first try. In the 20% failure test, 15 orders exhausted their four attempts without receiving a confirmation. At a 30% failure rate, 83 orders remained unconfirmed, and two received no refund at all. A key prevents duplicate actions, but it cannot fix broken connections on its own.

What it means for developers

For engineering teams deploying autonomous agents into production, tool execution logic requires the same transactional discipline as payment gateways. While developers can test and orchestrate agent prompts across models cheaply through one API at https://apixoai.online, model reliability does not eliminate downstream network failures.

To build safe tool-calling architectures, developers should adhere to a clear integration checklist:

  • Keep ID generation in application code: The surrounding application must generate the operation key before calling the model or the tool, ensuring restarts reuse the same identifier.
  • Enforce idempotency at the database level: Rely on unique constraints inside the transaction that performs the state change, mirroring patterns established by Stripe and Amazon's Builders' Library.
  • Reject parameter mismatches: If an existing key arrives with different parameters (such as an altered amount), the service must return an explicit error.
  • Handle timeouts as unknown states: When an agent times out, the interface should report that the action could not be confirmed, rather than claiming it failed. These unconfirmed operations must be routed to automated reconciliation or human review.

If an external tool or third-party API cannot support idempotency keys natively, automatic retries should be disabled entirely to prevent costly operational errors.


Source: The Refund Worked. The Agent Sent It Again. — Towards AI. Written by the Apixo team from that report.

#ai-news#ai-agents#idempotency#software-engineering#api-design#databases
Try it with your own tools

One key for Claude, GPT, GLM, DeepSeek and more. Pay per token with crypto.

Get your API key

Keep reading