AI Agents: Why Duplicate Charges Happen and How to Prevent Them
Most AI agent benchmarks only answer one question: can the agent finish the task? This article looks at the question almost nobody asks—can it clean up when the task goes wrong halfway through? Using duplicate charges as the running example, it explains how lost acknowledgments in distributed systems leave an agent unable to tell whether a write operation succeeded, why LLM-driven agents are especially prone to blind retries, and how idempotency keys, query-before-retry, compensating transactions, and reconciliation turn an unreliable agent into a production-trustworthy one. It closes with a set of concrete engineering rules, including measuring error recovery and side-effect rates in agent evaluations rather than task success alone.
You have probably seen this: you click a button once, the page hangs with no response, and unsure whether it worked, you click again—and the same charge lands on your account twice. That small bit of human clumsiness has now been carried almost verbatim into the world of AI agents, where it happens more quietly and more often. This article breaks a seemingly absurd scenario down into a serious engineering problem: when an agent that can autonomously call payment APIs, write to databases, and invoke system interfaces hits a network failure, can it know what it has actually done—and can it put things back cleanly?
The Neglected Question: Finishing the Task vs. Cleaning Up the Mess
Most agent evaluations answer only one question: can it get the job done? Given a goal, can the agent call tools step by step, plan a path, and reach the end state? Metrics for that are already fairly mature. Almost nobody asks the follow-up: when execution breaks halfway, can it roll back safely or resolve the state correctly?
These are genuinely different capabilities. An agent can post a high task success rate—say, completing more than 80% of scenarios without trouble—yet if you deliberately sever the network just before or just after a critical write operation, its error-recovery success rate can fall below half. Worse, it may not fail loudly at all; it may quietly double-charge your account while you have no idea it happened. “The task finished” and “the outcome is correct” are two separate capability lines that need to be validated separately.
The Technical Root Cause: Lost Acknowledgments
One counterintuitive point needs clarifying first. Many people assume that a smart enough model will simply notice an error and adapt. But the problem is not how smart the agent is. The problem is a structural one: information asymmetry.
A distributed system is an architecture in which multiple machines cooperate over a network to get work done. Most enterprise cloud services, payment gateways, and database clusters run on exactly this kind of architecture, which means network latency, packet loss, and timeouts are built into normal operation. When an agent talks to those systems, the central difficulty is the lost acknowledgment: the server-side operation actually succeeded, but the message saying “success” was lost in transit, and the client—the agent—has no way to tell whether the operation happened.
Unpack what that means. A remote call fails midway because the network drops, and all the agent has in hand is something like “connection timed out.” That error tells it nothing about the one fact that matters: did the operation actually land on the server and only the receipt go missing, or did it never execute at all? Those two situations demand opposite handling—the first must absolutely not be repeated, the second may need to be redone. Unfortunately the error itself carries no such information, so the agent can only guess. And when context is missing, blind retry is precisely the action with the highest error rate.
Why LLM-Driven Agents Are Especially Fragile
A conventional program handles a timeout with a predefined retry policy: how many attempts, what interval, when to trip the circuit breaker. All of it lives in code, and the behavior is deterministic. An LLM-driven agent is different: its next action is reasoned, not hard-coded.
When it perceives that “the last call failed,” the model naturally reaches for the human experience it absorbed from a huge corpus: if the page hangs, click again; if the API errors out, send the request again. That anthropomorphic instinct is harmless for read-only, freely repeatable operations. But the moment it lands on a write operation—charging a card, sending a message, placing an order—the “try once more” reflex produces side effects directly. The agent has no way to learn the true landing state of the previous attempt, and no innate respect for the engineering rule that non-idempotent operations must not be blindly retried. The fragility is amplified.
Fix 1: Use Idempotency to Make Repeated Requests Harmless
The most fundamental solution is to make repetition itself produce no additional effect. In engineering this is called idempotency. If an operation has the same effect on system state whether it runs once or five times, the agent can retry as often as it likes without double-charging.
The most common mechanism is an idempotency key: before issuing the write operation, generate a globally unique identifier for that business action and send it along with the request. Before committing the operation, the server checks whether it has already seen that identifier. If it has, it returns the previous result instead of executing again.
# Pseudocode: a charge call with an idempotency keydef charge(order_id, amount):idem_key = f"charge:{order_id}" # derived from a business unique identifierexisting = query_by_idem_key(idem_key) # check whether it has already been processedif existing is not None:return existing # already charged, return the historical resultresult = payment_service.execute(amount=amount,idempotency_key=idem_key # let the server deduplicate too)save_by_idem_key(idem_key, result)return result
Now, even if the agent reissues the request because of a lost acknowledgment, the second call hits the same idempotency key, gets an identical result, and does not charge an extra cent. The design point is that the key must map uniquely and stably to the same business intent—typically derived from an order ID, a request sequence number, or similar business identifier—rather than being regenerated on every retry.
Fix 2: Query Before You Retry
Beyond making the write itself idempotent, an equally important idea is this: when you are unsure, do not retry blindly. Issue a query first to reconstruct the real state.
Back to the timeout. The correct reaction is not to immediately resend the charge, but to call a read-only status endpoint and ask, “has this order actually been charged?” Then branch on the answer: if it has, treat it as a success and continue; only if it has not is it safe to retry.
# Pseudocode: query after a timeout, then decide whether to retrydef safe_pay(order_id, amount):try:return payment_service.execute(order_id, amount)except TimeoutError:status = query_order_status(order_id) # key step: query the real state firstif status == PAID:return success(status) # it actually succeeded, do not retryelif status == UNPAID:return payment_service.execute(order_id, amount)else:return pending(status) # unknown state, hand off to a human or reconciliation
The value of this principle is that it grounds the agent’s decision in an observation of the real world rather than speculation about an error message. For an agent, every side-effecting operation should come with a queryable state, so it has the chance to confirm instead of assume.
Fix 3: Compensating Transactions and Reconciliation
Some business operations cannot be made strictly idempotent, or the flow is long and spans multiple systems. There you can borrow the idea of compensating transactions: decompose a complex flow into steps that can be rolled back, and define a reversal action for each one. If an unrecoverable error occurs, run the compensations in reverse order to cancel out the side effects already produced.
This follows the same lineage as database transactions, but suits long cross-system flows better: record a reversible checkpoint after each successful step, so that if a later step fails, the system can walk the recorded steps backward. A companion mechanism is reconciliation—periodically comparing “what the system believes it did” with “what actually happened downstream” so that inconsistencies left behind by lost acknowledgments are detected and corrected. Reconciliation is the last line of defense beyond idempotency and query, built on the assumption that some inconsistency will always slip through, and using periodic checks as a safety net.
Key Principles for Implementation
Condensed into executable engineering discipline, the above reduces to a handful of points:
- Separate reads from writes. Read and pure-compute operations can be retried freely; writes and other side-effecting operations must be handled carefully, with real state confirmed before any retry.
- Give side-effecting operations an idempotency key. For every charge, order, or notification, derive a stable idempotency key from the business unique identifier, and have the server participate in deduplication.
- Make timeouts trigger a query, not a retry. Read “connection timed out” as “outcome unknown”: check first, then act—not redo first and ask questions later.
- Design compensation and rollback for long flows. For multi-step operations across systems, every step needs a reversible path reserved in advance.
- Use reconciliation as the safety net. Accept that inconsistency cannot be fully eliminated, and use periodic checks to catch and repair what slips through.
- Put recovery capability into evaluation. Measuring task success alone is nowhere near enough; you must deliberately inject network faults at critical points and measure the agent’s error-recovery rate and side-effect occurrence rate.
Conclusion
Stories of AI support agents issuing refunds in a loop, or charging the same customer twice, look like model recklessness on the surface. The root, though, is an old and stubborn distributed-systems problem—the information asymmetry created by lost acknowledgments. It is a reminder that building a reliable agent cannot stop at asking “can it finish the job”; it must also answer “when things go wrong halfway, can it exit safely?” Idempotency, query-before-action, compensating transactions, and reconciliation are nothing new, yet they are the necessary path from an agent that “looks clever in a demo” to one that is “trustworthy in production.” Real agent reliability shows up exactly in that restraint when facing uncertainty: confirm first, then act.
