AI agents are prone to a specific failure mode that plagues traditional distributed systems: the duplicate side effect. When a model calls a tool, the request might time out, the framework might retry, or the model itself might decide to call the tool again because it isn't sure the first attempt succeeded. For read-only operations, this is harmless noise. For tools that send emails, create invoices, or charge credit cards, it means the customer gets billed twice. The solution isn't some new AI-specific magic; it's the old, boring, and absolutely necessary practice of idempotency keys.

The Problem With Agent Retries

Consider a common scenario: an agent uses a send_invoice tool. The server processes the invoice, but the response is slow. The agent's HTTP client times out, interpreting the delay as a failure. The agent retries, and the customer receives two identical invoices. You cannot fully prevent retries because networks are flaky and LLMs are probabilistic. What you can do, however, is make those repeated calls safe. The core idea is to assign every side-effecting action a key that identifies the intent, not the specific attempt. If the same key appears again, the system returns the stored result instead of executing the work a second time.

Building Keys From Intent, Not Model Output

A critical implementation detail is how you generate these keys. Do not let the LLM invent the key. Instead, build the key in your own code using the tool's arguments. For an invoice, the key should be a hash of the customer ID, the invoice period, and the action type. This ensures that two retries of the same intent always produce the same key, regardless of how the model phrases its internal reasoning. A minimal implementation uses a database table to store the key, status, and result. When run_once is called, it attempts to insert the key. If the key already exists and is marked 'done', it returns the cached result with an already_done: True flag. This flag is crucial: it tells the model explicitly that the action occurred, preventing it from getting stuck in a retry loop trying to confirm a success it can't see.

Key Design and Edge Cases

Choosing which fields to include in the key requires careful thought. You must include everything that defines the intent (like customer ID and period) but exclude anything that varies between retries without changing the outcome (like timestamps or the model's specific wording). For example, leaving out the amount field in the key ensures that a retry with a slightly different calculated amount doesn't accidentally trigger a second invoice for the same period. You also need to handle the 'pending' state. If a process crashes halfway through, the key remains pending. The safest approach is to flag these for human review rather than guessing whether the external action succeeded. Additionally, pass your idempotency key through to third-party providers like Stripe or SendGrid when they support it, creating a double layer of protection.

Key Takeaways

  • Agents increase the frequency of retries, not decrease them; treat every side-effecting tool like a payment endpoint.
  • Generate idempotency keys from tool arguments in your backend code, never from model-generated text.
  • Use an already_done flag in responses to guide the model away from redundant retries.
  • Leverage provider-side idempotency keys in addition to your own for defense in depth.
  • Define key fields to capture intent stability, excluding volatile data like timestamps.

The Bottom Line

Idempotency isn't an AI problem; it's a distributed systems problem that agents expose at scale. Stop trying to make the model 'smarter' at not retrying. Make the system safe to retry. It's a few lines of code that saves you from a class of 'why did this happen twice' support tickets.