
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.
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:
- A text message API has no way to recognise a repeated send, so a retried send is a second, billed text.
- A chat bot library raised a timeout although the message was delivered, so the retry posted it twice.
- A notification system retried with a fresh idempotency key on every attempt, so the provider could not tell it was the same notification.
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
- Derive the key from the business action, not the attempt.
charge-order-1001is the same on every retry; a random UUID per attempt is not. - Where the provider has no keys, remember what you sent. Store the message or order ID before sending, and check it before a retry.
- Treat a timeout as unknown, not failed. Look the result up, or retry with the same key, before reporting failure to the customer.
- 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.