Idempotency in Payment Systems
An idempotency key identifies duplicate operations, and the body hash determines whether to replay or reject them.
The network will deliver the same message twice, so every write carries an idempotency key. The key identifies duplicate operations. The body hash determines whether to replay or reject. When the key and body hash both match, replay the stored outcome. When the key matches but the body differs, reject the request. Without the hash, a different request using the same key is silently treated as a successful retry.
What the key identifies
Delivery dedup is a different thing. A webhook delivery_id identifies one HTTP attempt at telling you; an idempotency key identifies the operation. Collapse the two and a retried webhook looks like a business event. (Verify per vendor that a resend reuses the id, since many don’t document it.)
Two refunds against one charge can be distinct authorized operations. Stripe permits partial refunds up to the remaining amount. Each new operation needs its own identity and a check against that amount. Counting refund identifiers confuses a legitimate second refund with a replay.
Where the key lives
Keep the key in the event log. Most accepted operations write no row to the transactions table. Examples include opening an account, changing a limit, rejecting a posting and closing a statement. The event log tracks which keys have already been used, provides the audit trail and functions as both inbox and outbox. The reasons for keeping it immutable also apply to the journal.
How the body is compared
- Hash a versioned, canonical byte form (timestamps normalized to UTC first). Put the hash in the
WHEREof the replay lookup so a mismatch returns no row. Returning the hash in a column lets a caller that forgets to compare it read the wrong stored result. - The hash must cover every semantic field. Strip
statusfrom it and a retry that changed only its status silently replays. Money can move twice behind a 200.
How long the claim lasts and what happens mid-flight
- Key expiry converts a retry into a double post. Stripe-style 24-hour expiry does this silently.
- Don’t invent an “in-flight” state (409) if the claim and its work commit in one database transaction. The concurrent duplicate blocks on the key claim and finds a durable result. Stripe needs a 409 only because its work isn’t one transaction.
The same lost reply in an agent’s tool call
An agent that writes through a tool meets the lost reply without the key. In Girish Konda’s illustration a sub-agent calls a tool that opens a pull request, the reply is lost, the orchestrator retries the whole task, and the write happens again [13:21 to 13:45]. “A lost reply stays unknown until you read it back” [20:23].
- Stopping and retrying both fail. Not retrying can leave the work undone. Retrying can do it twice. Only the system that performs the write can tell a repeat of the same intended effect, and it needs an identity for that effect to do so.
- The tool layers do not supply one. “In MCP, idempotent hint is only a hint and it defaults to false. If you need it, you build it” [14:30].
- Confirmed means observed. “Submitted is not confirmed. Confirmed means you observe the effect” [15:28]. Without a key, the agent’s version of a replay lookup is a read-back: find the effect by its identity before writing again.
Carry one effect identity across the approval. In a payment workflow with a human gate, approval identifies a version of the intended payment. Keep the same provider idempotency key when retrying that version after an uncertain acknowledgement. If terms change, the new effect needs its own approval.
Measured: the duplicate belongs to the endpoint
Tanmay Sah and Dolly Sah’s UndoBench gives the lost reply a measured rate. In its main study every faulted run had the remote write commit and the reply dropped. The talk reports duplicates in 54% of naive retries [3:27]. The paper gives the denominator: 512 of all 960 naive-retry trials (53.33%), counted over every trial.
- The rate follows the receiving side. Five workflows with no idempotency support and no unique constraint duplicated in 400 of 400 naive retries. A schema migration duplicated in none, because SQLite’s unique constraint blocked the second attempt.
- Recovery after a lost reply. Across two hosted models and twelve test workflows, client-side keys alone gave a recovery rate of 33.20% among tasks the agent could normally complete. Adding server-side idempotency raised recovery to 97.24%, or 741 of 762 such tasks. With server-side idempotency, the study recorded 0 duplicates in 960 trials.
- Keys do not repair a half-done write. When the fault hit in the middle of a multi-step write, on four workflows and the same two hosted models, client-side keys recovered none of the 315 pairs whose normal run succeeded, out of 320 pairs tested with that policy. A key identifies a repeat of the whole operation. It cannot finish or undo the part that already happened.
- Limits. Mocked services (SQLite, local Git, mocked Stripe and AWS), one fault per run, twelve workflows, and the authors’ own benchmark.
Sources
- Girish Konda (Microsoft), “Your Sub-Agents Are a Distributed System. Treat Them Like One.”, AI Engineer, YouTube.
- Tanmay Sah and Dolly Sah (independent researchers), “Your Coding Agent Can’t Undo Its Own Mistakes”, AI Engineer, YouTube; UndoBench, arXiv 2610.05622v1.