AI Agent Retries: How to Prevent Duplicate Actions
Learn how to make AI agent retries safe with idempotency keys, execution layers, and failure testing to prevent duplicate actions in production.
An AI agent receives a request to issue a customer refund. It calls a payment API, but the request times out before the agent receives a response.
The agent does not know whether the refund succeeded. It calls the API again. If the payment service treats both requests as separate operations, the customer may receive two refunds.
From the agent’s point of view, retrying seems reasonable. From the system’s point of view, it creates a serious business problem.
This is why production AI agents need more than good prompts and reliable models. They also need a safe execution layer.
Why Retries Become Dangerous
Retries are a common way to handle temporary network failures. However, a failed response does not always mean a failed operation.
A server may complete an operation before the connection is lost. The client sees a timeout, but the business action has already happened.
This problem is especially important when an agent uses tools that change external systems, such as issuing refunds, creating orders, or sending emails.
The HTTP specification, RFC 9110, advises against automatic retries of non-idempotent requests unless the client knows that retrying is safe.
Idempotency means that repeating the same operation has the same intended effect as performing it once.
An agent cannot reliably solve this problem through reasoning alone. It needs a system that can recognize repeated operations.
Retry safety should be enforced by the execution layer, not left to the language model.
Build an Idempotent Execution Layer
Instead of allowing the agent to call the payment API directly, place a controlled execution layer between the agent and the external service.
The execution layer manages operation identity and safe retries independently of the agent.
Each approved refund should receive a unique operation ID before the first API call. Store this ID in a database and reuse it whenever the same operation is retried.
The application must link repeated tool calls to the same approved refund. Do not generate a new ID for each model response or use the payment ID alone, since separate partial refunds may be valid.
The following example shows a single execution attempt using a payment service that supports idempotency keys. It assumes the application has already checked authorization and stored the approved operation and its parameters.
import requests
def execute_refund(operation, api_key):
response = requests.post(
"https://api.stripe.com/v1/refunds",
auth=(api_key, ""),
headers={"Idempotency-Key": operation.id},
data={
"payment_intent": operation.payment_id,
"amount": operation.amount,
},
timeout=10,
)
response.raise_for_status()
return response.json()
The important part is not the HTTP request itself. It is the identity of the operation.
If the request times out, the execution layer can retry using the same operation ID and parameters, subject to the provider’s retry rules.
Stripe’s API v1 documentation explains that once endpoint execution begins, the service records the response for that key, including server errors. This behavior was checked on December 2, 2026.
However, this protection has limits. Providers may expire stored keys. They may also reject requests that reuse a key with different parameters.
A production system must understand these limits before retrying an old operation. Use bounded retries with backoff, and reconcile uncertain outcomes rather than retrying indefinitely.
It must also store the operation ID before making the first external request. Otherwise, a process crash could cause the system to lose the original ID and create a new one during recovery.
AWS’s engineering guide on safe retries describes a similar approach using unique client request identifiers.
Test the Failure, Not Just the Response
A normal integration test might check whether the refund API returns a successful response.
That is not enough.
To evaluate retry safety, simulate failures at different points in the operation. Check the final business state rather than relying only on HTTP status codes.
| Test scenario | Expected behaviour |
|---|---|
| Timeout before execution | Retry safely |
| Timeout after execution | No duplicate refund |
| Agent repeats a tool call | Reuse the existing operation |
| Worker crashes and restarts | Recover the stored operation ID |
| Same key, different parameters | Reject the request |
| Idempotency key expires | Reconcile before retrying |
The most important metric is the duplicate side-effect rate: the number of tested operations with duplicate effects divided by the total number of tested operations.
For financial operations, the target should be zero duplicates across the defined test scenarios. This does not prove that duplicates are impossible, but it provides a clear regression test.
There is also a trade-off. An execution layer introduces storage, state management, and additional failure modes.
For read-only tools, this complexity may not be necessary. For tools that move money or change important business data, it is often justified.
Key Takeaways
AI agents should not have unrestricted control over retries for operations that change external systems.
Use stable operation IDs, persist execution state, and rely on provider-supported idempotency where possible. When the outcome remains uncertain, reconcile the external state before attempting another operation.
A reliable agent is not simply one that knows when to retry. It is one whose system makes retrying safe.