Retry-Safe Order Creation: How Agent Tool Calls Avoid Double Orders
Make AI agent tool calls idempotent with request keys, stored outcomes and lookup-before-write checks, so a retry never creates a second order or invoice.

An idempotent tool call produces the same result no matter how many times the same request reaches your system. For an AI agent, that means a retry after a timeout, a repeated instruction or a restarted workflow reuses the outcome of the first order, invoice or booking instead of writing a second one. The practical fix has three parts: a stable key created before the first attempt, a record of what that key produced, and a check against that record before any write happens.
Key takeaways
- Create the idempotency key once, before the first attempt, and reuse it for every retry of the same business intent.
- Store the key next to the outcome in your own system, so a repeated call returns the original result instead of writing again.
- Look up existing records before creating anything, because a provider's key window may be shorter than your workflow.
- Treat a timeout as an unknown outcome, not a failure. Check the state before retrying.
- Derive keys from the business action, not from the model's wording, so two phrasings of one order collapse into one write.
Why retries create duplicate orders
Duplicates rarely come from a single bad decision. They come from ambiguity. An agent sends a create_order call, the network drops the response, and the agent's harness treats the missing reply as a failure. The order may already exist on the other side. The harness then retries, and the second call creates a second order with a new identifier. Nothing in the logs looks wrong, because each request was valid.
There is a second path. The model may decide again, in a new turn, that the customer wants the order placed. If the new request contains slightly different text, a different timestamp or reordered line items, a naive system sees a new request. Retries and re-derivations are normal behavior for agents, so the write tool itself has to be built to absorb them.
The same pattern appears with invoices that get sent twice, bookings that land in the calendar twice and refunds that are issued twice. The write endpoint is not the only place to fix this. The fix belongs in the tool contract, in the stored state and in the rules the agent follows when a result is unclear.
What an idempotency key should identify
A key should identify the business action, not the attempt. A random identifier generated for each call defeats the purpose, because every retry then looks new. A key built from the model's free text is just as weak, because wording changes between turns.
A good key combines the stable facts that define the intent. For an order, that usually means the customer identifier, the quote or cart identifier, a fingerprint of the line items and the action type. For example, a key such as order, customer, quote and a version can be built from fields that the agent receives from your system, not from the conversation. If the same quote is accepted twice, the key is the same, and the second call returns the first order.
The key should also be stored with the request payload. If the same key arrives with a different payload, the tool should reject the call and report a conflict, because someone or something has changed the intent. Silently returning the old result in that case hides a real problem.
A step-by-step plan for making a write tool idempotent
Use this sequence for any tool that creates an order, invoice, booking or record. Each step is small, and each one can be reviewed by the person who owns the process.
- Step 1: List every write action the agent can take, and name the business object each one creates or changes.
- Step 2: For each action, define the fields that make two requests the same intent, and write them down as the key recipe.
- Step 3: Add a table or store, owned by your system and not by the model, that holds the key, a hash of the payload, the status and the resulting identifier.
- Step 4: Before any write, look up the key. If a succeeded record exists, return it. If a pending record exists, return a pending status and do not create anything.
- Step 5: Write the pending record first, call the external system with the key where the provider supports one, then update the record to succeeded or failed with the external identifier.
- Step 6: Return the same structured response on a repeat call, so the agent can continue without inventing a new action.
- Step 7: Log every duplicate attempt and every conflict, and review them in the recurring evaluation.
The order of steps matters. Writing a pending record before the external call means that a crash in the middle leaves evidence behind. Without it, the next retry has nothing to compare against.
How to handle timeouts and unknown outcomes
A timeout is the case where idempotency earns its place. The agent does not know whether the write happened. The rule should be simple: never retry a write blindly. First read the state. Ask your own record for the key, and if the record is pending, ask the external system for an object tied to that key or to the quote identifier.
Provider-defined idempotency is useful but not sufficient alone. Stripe documents idempotency keys for its API, so that a repeated request with the same key returns the result of the original request rather than creating a new charge. That behavior is a strong building block. However, keys are retained for a limited period, the provider only knows about its own endpoint, and your workflow may span several systems. Your own stored record is what lets the agent reason about the whole business action.
When the lookup itself fails, the agent should stop and escalate rather than guess. A human approver can then confirm whether the order exists before anything else is written. That single rule prevents most of the expensive duplicates.
An illustrative scenario
The following scenario is illustrative and does not describe a specific company. A small distributor lets an agent convert accepted quotes into sales orders. A customer confirms quote 1042 by email. The agent calls create_order. The call times out after the order was written, so the harness reports an error and the agent retries.
Without a key, the second call creates a second order, and the warehouse prepares two shipments. With the key built from the customer, the quote and a fingerprint of the lines, the first call left a succeeded record. The retry finds that record and returns the original order identifier. The agent then tells the customer the order is confirmed, and the warehouse sees one shipment. Had the first call stayed pending, the tool would have returned a pending status, and the agent would have asked a person to check before any further write.
How Be Human handles this in managed operations
Be Human designs these rules during the first engagement, usually with one useful role and one write action at a time. The key recipe, the stored record and the lookup-before-write sequence are written into the tool contract, so the agent cannot skip them. Approval boundaries define which writes need a person to confirm a pending or conflicting state. In managed operations, Be Human monitors retries, duplicate attempts and conflicts, reviews them in recurring evaluations, and updates the key recipe when a business process changes. The goal is that a retry is a routine event, not an incident.
How do you test that retries are safe?
Testing should try to create duplicates on purpose. A test that only runs the happy path proves nothing about retries. The checks below are simple enough to run before any agent gets write access, and to repeat after each change to the tool.
- Send the same create request twice in sequence, and confirm that only one business object exists.
- Send two identical requests at nearly the same time, and confirm that the second one waits for or joins the first.
- Simulate a timeout after the external write succeeds, and confirm that the retry returns the original identifier.
- Send the same key with a changed payload, and confirm that the tool returns a conflict instead of a silent success.
- Change the wording of the customer request but not the quote, and confirm that the key stays the same.
- Confirm that two different quotes for the same customer produce two different keys.
Keep the results in a shared log that the operations lead can read. A clear pass or fail is more useful than a long technical report. If you use an evaluation framework, the guidance on evals for AI agents in the sources below describes how to keep these checks repeatable as the tool changes.
Related work is worth reading before you change production tools. The article on idempotency for AI agents covers the wider pattern, and the guide on rate limit handling explains how retry timing and queues should interact with keys. For the schema itself, the notes on tool schema design show how to name and type the key fields so the agent cannot send ambiguous input.
If you are not sure which of your write actions should get this protection first, the free Be Human AI diagnostic can help you find the first task worth handing to an agent.
Frequently asked questions
Does an idempotency key stop an agent from creating the same order twice if it phrases the request differently? It does, provided the key is built from business facts such as the customer, the quote and the line items, rather than from the model's wording. The agent's text is an input to the decision, not the identity of the action. Your tool should derive the key from the structured fields it receives and reject any call that tries to reuse a key with different content.
What should an AI agent do if a tool call times out? The agent should not retry the write immediately. It should first read the stored record for the key, and if that record is pending, query the external system for an object linked to the key or to the source document. Only when the system confirms that nothing was created should the write run again. If the state remains unclear, the agent should stop and escalate to a named person.
Can I rely on the idempotency feature of a payment or ERP provider alone? Provider features are valuable, but they usually cover only their own endpoint and only for a limited window. Your workflow may involve an email, a quote, a warehouse record and an accounting entry, and the provider cannot see all of them. Keep your own stored record of the key and the outcome, and use the provider's mechanism as an additional layer rather than the only one.
Is idempotency the same as rate limiting? No. Rate limiting controls how fast requests are sent and how the system responds when it is busy. Idempotency controls what happens when the same intent is sent more than once. A well-designed agent needs both: rate limits and retry timing to avoid overload, and idempotent writes so that a retry is harmless when it does occur.
Your next step
See where an AI agent fits in your company first.
Take the free Be Human AI diagnostic. A few questions about your work show which task to hand to an agent first, what it needs to know and where a person approves.
Be Human editorial guidance. Examples are illustrative unless explicitly attributed. Sources provide background; implementation choices depend on your workflow.


