← All articles
Practical guideWorkflow testing

The timeout that charges your customer twice

A saved payment, a lost reply and a retry without the original key: the most common way apps charge customers twice and send duplicate emails, and how to test for it.

In this article
Conceptual illustration · AgentTwin journal

The most common way an app charges a customer twice

Your app asks the payment provider to charge a card. The provider charges it. The reply is slow, so your app's timeout fires first. The app never sees the success, decides the charge failed, and tries again. Now the customer has two charges.

Stripe's own engineering blog describes this exact sequence, and one team's postmortem describes customers being charged twice for 47 minutes because a retry went out without the original idempotency key.

Your app asks the provider to charge a card and the provider saves charge 1, but the reply arrives after the app's timeout. The app retries without a key and the provider creates charge 2. With the same idempotency key, the retry returns charge 1.
A timeout is not a failure. Illustrative test design; this diagram is not a run result.

It is not only payments

The same sequence sends duplicate email, texts and chat messages, and most messaging APIs have no idempotency key at all:

Client libraries retry for you

You may not have written a retry at all. Many official SDKs retry failed requests on their own. That is helpful for reads and dangerous for writes when the library cannot attach a stable key. Two things to check in every client you use:

Question Why it matters
Does it retry on timeouts or 5xx by default? A retried write without a key is a second write
Does it send an idempotency key automatically, and is the key stable across attempts? A new key per attempt makes each attempt look new

How to make a retry safe

  1. Derive the key from the business action, not the attempt. charge-order-1001 is the same on every retry; a random UUID per attempt is not.
  2. Where the provider has no keys, remember what you sent. Store the message or order ID before sending, and check it before a retry.
  3. Treat a timeout as unknown, not failed. Look the result up, or retry with the same key, before reporting failure to the customer.
  4. Never announce success you did not see. If the charge's outcome is unknown, do not email "your order is confirmed".

How to test it

The bug only appears when the provider saves the write and the reply is lost. A mock that returns success or an error never produces that state, so unit tests pass.

AgentTwin produces it on purpose. It runs your real workflow against working copies of the services it calls, then injects the faults that cause duplicates: the write is saved and the reply arrives after your client's timeout, the first request fails with a 500, the service stays down, or the triggering webhook is delivered twice. Your code's own timeouts fire exactly as they would against the real service. When a second charge, email or message appears, the run reports it with its cost and the fix:

✗ Your agent charged cus_at_1001 twice
  Stripe /v1/payment_intents: write saved, reply lost (timeout after write)

Frequently asked questions

Why do customers get charged twice?

Usually because a payment request succeeded but the reply was lost or slow, the app treated the timeout as a failure, and it retried without the original idempotency key. The provider saw two different requests and charged twice.

Does an idempotency key stop duplicate charges?

Yes, if the key identifies the business action and stays the same on every retry. A new random key per attempt defeats it, because each attempt looks like a new action.

What about services with no idempotency keys, like SMS or chat?

Record what you sent, keyed by the business action, before sending, and check that record before any retry. Treat a timeout as unknown and look the result up instead of resending.

Can I test this without charging real cards?

Yes. AgentTwin runs your workflow against local copies of the services it calls and injects lost replies, errors and duplicate webhooks. Nothing reaches the real provider.

Continue readingMy AI agent refunded a customer twice after a timeout →
Evaluate one workflow

Can your workflow pass the same check after a failure?

Bring the entry point, dependency operations and result you need to preserve. Discuss whether AgentTwin supports the test, what preparation is needed and the scope and costs before proceeding.

Discuss your workflow Try the sample first →← Back to all articles