Skip to content
An AI agent sends the same approved write twice; an idempotency guard runs it once and returns the saved result for the duplicate.

BlogIndustry News

Idempotency Key: Stop AI Agents From Running Writes Twice

An OpenAI bug ran one approved agent write twice. Here's how idempotency keys keep AI agents from creating duplicate orders, invoices and refunds.

A new OpenAI bug report makes the case for the idempotency key better than any textbook. On November 16, a developer showed that one approved write through the Responses API's native MCP support executed twice when the same response carried on with a hosted programmatic read. The test server logged write, read, write. The model's final answer still said it had called the write once. OpenAI support replied that it's looking into it.

That bug will get fixed. The lesson won't expire. If an AI agent can create orders, invoices or posts in your systems, every write it can trigger needs a key like this, because the agent layer can't promise "exactly once" for you. Here's how that works and how to add it without rebuilding anything.

TL;DR#

  • A reproducible OpenAI bug ran one human-approved MCP write twice, and the model's own summary hid the duplicate.
  • Duplicate tool calls also come from retries, reconnects and client bugs, so plan for every write arriving twice.
  • Fix it where state changes: an idempotency key tied to the approval or business action, plus a database constraint as a backstop.

What happened in the OpenAI duplicate write bug#

The reporter used a synthetic MCP server with a read tool and a write tool marked idempotentHint: false. Writes required approval, and the first request produced exactly one approval request.

The second request sent the approval and limited further tool use to a programmatic read. The server then received three calls: the write, the read, and the same write again. Both writes carried the same approval_request_id and identical arguments. It happened whether the write succeeded or returned an error.

The reporter is honest about scope. All three samples that combined approval resumption with a hosted programmatic read replayed the write; the three control runs without that combination didn't. That's a sample, not a rate across models.

It's not new, either. Developers previously documented ChatGPT's MCP connector running write tools twice, and one invoicing project rated duplicate execution a P0 because two invoices for one approval is a legal problem.

Why duplicate writes from AI agents hurt more than they look#

In a real business, a duplicated write means a second invoice, a double refund or the same customer email sent twice. Someone has to find and undo each one by hand.

Two details in this bug make it worse than a normal retry problem.

First, a human approved the action once. Many teams treat an approval gate as their safety net. Here the approval worked perfectly, and the system still acted twice. Approval controls whether something happens. It does nothing about how many times.

Second, the agent's summary was wrong. If your audit trail is the agent's own report ("called the write once, no retries"), you won't catch this. Execution counts have to come from the system that received the calls.

As AI Agent Store's weekly roundup noted, long multi-step runs are where this risk grows, and that's where useful automation lives.

What an idempotency key is, and what idempotent means in a REST API#

An operation is idempotent when doing it twice has the same effect as doing it once. In a REST API, GET, PUT and DELETE are idempotent by design. POST usually isn't: "create order" twice gives you two orders.

An idempotency key turns a POST into something safe to repeat. The caller sends a unique value with the request. The server stores the result of the first request under that key, and any repeat with the same key gets the stored result instead of a second write.

Stripe's docs are the reference most teams copy. Stripe saves the status code and body of the first request for a key, even when it fails, compares parameters on repeats and errors if they differ, and lets keys be pruned after 24 hours. The IETF is standardising the same idea as an Idempotency-Key HTTP header.

Where the key comes from matters with agents#

Key sourceStops this replay?Stops a model "retrying"?Trade-off
Model invents a key per callYes, if args are copiedUsually no, a retry gets a new keyEasy to add, weakest
Derived from the approval IDYesYes, for that approvalYour orchestration must pass it through
Derived from the business action (order 1042 + "invoice")YesYesStrongest, needs a natural key
Database unique constraintYesYesBackstop, fails loudly instead of replaying the result

Technical breakdown: where to put the guard#

Put the check in the tool server, right before it touches your real systems. Not in the prompt, and not in the agent framework.

Agent ── tool call (args + idempotency_key) ──▶ Tool / MCP server
                                                  │
                                    look up key in idempotency store
                                                  │
                     hit ◀────────────────────────┴──────────────▶ miss
          return stored result,                          claim key, run write,
          skip the write                                 save result, return it
                                                  │
                                 Shopify / Zoho / Stripe / your database

A minimal store is one table:

create table tool_idempotency (
  key        text primary key,
  tool       text not null,
  args_hash  text not null,
  result     jsonb,
  created_at timestamptz default now()
);

Insert the key first with on conflict do nothing. If the insert wins, run the write and save the result. If it loses, return the saved result, or a conflict error if args_hash doesn't match.

The trade-offs to decide up front#

  • Retention. A community pattern for MCP writes keeps keys 10 to 30 minutes. For agents, keep them as long as your longest approval.
  • Concurrency. Two copies can arrive at the same moment. Claiming the key before the write is what stops both from running.
  • Pass-through. If the downstream API accepts keys (Stripe does), forward yours so a retry is caught at both layers.

What this means for businesses running AI agents#

If an agent writes into your store, CRM, billing or ERP, ask one question of whoever built it: what happens if this write arrives twice? "The agent only calls it once" isn't an answer.

Tool annotations won't save you. MCP's idempotentHint describes a tool to the client; it doesn't enforce anything. In this bug the write was correctly marked non-idempotent and still ran twice.

If you're on OpenAI's Responses API today, a community reply in the bug thread suggests splitting the turns: resume the approval with tool_choice: "none", then send the read as its own request. The reporter's own control runs showed a single write in that case. It costs one extra round trip.

How to add idempotency keys to your agent's writes: a checklist#

  1. List every tool that creates, sends, charges or deletes something.
  2. For each, pick the key source: a business key where one exists, the approval ID otherwise.
  3. Add a required idempotency_key field to the tool's input schema.
  4. Build the key store with insert-first claiming and a retention window that covers your approvals.
  5. Add a unique constraint on the business key (order ID plus action) as a backstop.
  6. Log every incoming tool call with its key, and count executions from those logs.
  7. Test it: send the same call twice, in parallel, and check you get one write.

How MagicMakers Lab approaches this#

When we build agentic systems, every tool that changes state gets scoped permissions, an approval gate where it matters, and an idempotency guard at the server, with decisions logged so execution counts never depend on what the model says. It's the same discipline that let Framico ship 200+ orders a day with zero manual steps, and that keeps automated artist payouts in Poster & Art from paying anyone twice.

Key takeaways#

  • A human approval controls whether an agent acts, not how many times.
  • An AI agent's summary of what it did is not an audit log; your server logs are.
  • Idempotency keys tied to the business action or approval ID stop replays that model-generated keys miss.
  • Put the guard in the tool server, with a database constraint underneath it.
  • Test duplicate and parallel calls on purpose before an agent touches billing or fulfilment.

FAQ#

What is an idempotency key?#

It's a unique value sent with a request that changes data, such as creating an order or charge. The server stores the result of the first request under that key. If the same key arrives again, from a retry or a replay, the server returns the stored result instead of repeating the action, so you get one order rather than two.

What does idempotent mean in a REST API?#

An idempotent operation has the same effect whether it runs once or several times. GET, PUT and DELETE are idempotent by design, because repeating them doesn't change the outcome. POST usually isn't, since each call can create a new record. Idempotency keys make POST requests safe to repeat by letting the server recognise duplicates.

Why would an AI agent call the same tool twice?#

Causes include network retries, client reconnects, framework bugs and platform bugs like the one reported in OpenAI's Responses API, where an approved write replayed after a programmatic read. Models can also decide to retry on their own after an error. Because the causes sit in several layers, the safest assumption is that any write can arrive twice.

Should the AI model generate the idempotency key?#

It's better than nothing, but it's the weakest option. A model that decides to retry will often invent a fresh key, which defeats the check. Derive keys from something stable instead: the approval ID, or a business key such as the order number plus the action. Keep a database unique constraint as a final backstop.

If your AI agents move money, stock or customer messages, check how they behave when a call arrives twice before your customers do. We'll walk through your setup and show where duplicates could slip in. Book a free audit.

Sources#