← All articles
Practical guideWorkflow testing

My AI agent refunded a customer twice after a timeout

The usual cause is a timeout after a successful write: the provider saved the refund, the reply never arrived, and a retry did it again. Catch it by replaying that moment in a test and counting refunds.

In this article
Conceptual illustration · AgentTwin journal

Illustrative test design

Illustrative test design: one required record for the request, one required notification for that record, and unrelated state left unchanged.
Check the effects, not only the reply. Illustrative test design; this image is not a run result.

The usual cause is a timeout after a successful write: the payment provider saved the refund, the reply never reached your code, and your agent or its framework retried without knowing the first call had worked. You catch it by replaying exactly that moment in a test and counting refunds, not by reading logs.

What happened

  1. The agent calls the refund API.
  2. The provider saves the refund and starts to reply.
  3. The reply is lost, or arrives after your client's timeout.
  4. Your code, or the agent's own loop, sees an error and calls the refund API again.
  5. There are now two refunds and no error anywhere.

The HTTP call "failed", so nothing crashed. The only evidence is in the provider's final state, which is why logs look clean.

Fix the cause

  • Send an idempotency key with every refund, derived from the business action (for example refund:{order_id}:{request_id}), not a random value created inside the retry loop. A new key on every attempt makes each retry look like a new action.
  • Retry a timeout with the same key and the same parameters. Stripe documents this. Its keys expire after 24 hours, so reconcile anything older by looking the refund up instead of retrying.
  • Keep retries in the tool wrapper, not in the model's loop. A model that rephrases the call defeats exact-match deduplication.
  • Check your own ledger before refunding when a provider has no idempotency support.

Catch it before it ships

Run your real code against a twin of the payment API, make the twin save the write and then drop the reply, and count the refunds afterwards.

npx agenttwin

AgentTwin finds the workflow that moves money, runs it clean, then reruns it under each failure, including a timeout after a write, a lost reply and a duplicate delivery. On a demo support agent (an app written for AgentTwin's own product films, not a customer) it reported:

$48.00 per incident  Your agent refunded ch_at_1001 twice
Evidence: stripe write /v1/refunds (server.js:51) → stripe write /v1/refunds (server.js:51)

The cost is an estimate, and the report states its assumptions. npx agenttwin --fix proposes a patch (here, passing the caller's idempotency key to stripe.refunds.create), applies it, reruns the same failures and keeps it only if they all pass. Nothing reaches the real service.

Limits

The lost-reply fault is applied to Stripe by default. A twin covers the operations agents use, not the whole vendor API; a call outside it answers 501 and marks the run incomplete instead of passed.

Related: Idempotency keys, The timeout that charges your customer twice and How to simulate timeouts and duplicate event delivery.

Continue readingChoose your first AI-agent workflow test →
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