All patterns · 11 of 13 · Structure
Agentic patterns: Retry and compensation
The bank executes a payment and the answer is lost. Watch a retry that cannot pay twice, and an undo when a later step fails.
The bank did not answer. Five payments go out. The bank executes the third and the confirmation never arrives. Paid, or not?
Demo: The bank did not answer. Recorded on 26 November 2026 with Claude Opus 5.5 and Claude Haiku 5.5: the model’s answers are real, replayed here. The first run contains no model call at all. The bank and the ledger are a small simulation.
What you just saw
What the retry and compensation demo shows
The principle
What is retry and compensation?
Retry and compensation is a workflow pattern in which every action carries an idempotency key, so that repeating it has no further effect, and every completed step has an undo that runs when a later step fails.
Processes fail in the middle. The bank executes a payment and the confirmation is lost; a later step is refused after earlier ones have already changed the books. Neither problem involves a model, and both get worse when an agent is in charge, because an agent that sees an error will often try again.
Two old techniques cover it. An idempotency key travels with each request, so the receiver recognises a repeat and returns the first result. Compensation means every step that changes something registers how to undo it, and a failure runs those undos in reverse order. In the demo the first run is all code: one timeout, one retry, nothing paid twice. An agent given the same batch did not retry and reported the gap, which is careful and still leaves the batch open until someone phones the bank.
Where it applies
- Payments, orders, bookings: any action against a system that can time out
- Processes that span several systems with no shared transaction
- Agents that call tools with side effects
- Long-running processes that must survive a restart
Where it does not
- Read-only steps, which can be repeated freely
- Actions that cannot be undone at all: those need a gate beforehand
- Systems that offer a real transaction across the steps
Fill the boxes
One shape, three ways to build it
The pattern exists with or without AI. The design decision is which boxes deserve a model, and what that costs in money, speed and auditability. In the first run above, twelve steps were done: 12 by code.
All code
The workflow engine retries with a key and runs the undo steps. Failures become non-events, and nobody is asked to think.
- Cost
- Free
- Speed
- One retry, about a second
- What the auditor gets
- Each request, its key, and the bank’s answer
Code, with a model at the edge shown above
The same, with a model drafting the message a person has to send afterwards. The model touches no money.
- Cost
- A fraction of a cent for the note
- Speed
- The same
- What the auditor gets
- The same, plus the draft a person approved
An agent in charge
The agent sees the error and chooses what to do. In the demo it chose well and stopped. Next time it may retry.
- Cost
- $0.02 for this batch
- Speed
- Stalled until a person checks
- What the auditor gets
- The agent’s account of what it thinks happened
codemodelagentperson
What it costs
The price of each run
The first run contains no model call, and the third contains one, to draft a note. Making failure safe is engineering, and it costs nothing per case once it is built.
Measured from the runs recorded with Claude Haiku 5.5 and Claude Opus 5.5 on 26 November 2026. Model calls only: people’s time and infrastructure come on top.
| Run | Model calls | Model cost | |
|---|---|---|---|
| Retry with an idempotency key | 0 | $0 | |
| An agent runs the batch | 2 | $0.019 | |
| A later step fails | 1 | $0.0001 |
In a regulated process
What the auditor gets
For every payment the log holds the key, each attempt and the bank’s reference, so the question whether something was paid twice is answered by a lookup. For a failed run it holds each undo and the entry that reversed the original. The books show a posting and its reversal, never a silent deletion.
Lineage
Where the shape comes from
Enterprise Integration Patterns calls the receiving side an Idempotent Receiver. The undo chain is a saga, described by Garcia-Molina and Salem in 1987 for long transactions and now standard in service architectures; the workflow patterns catalogue has it as compensation. None of it is specific to AI. An agent that calls tools needs it more, because it retries of its own accord.
Also known as Idempotent Receiver (Enterprise Integration Patterns) · Compensating Transaction (Azure Architecture Center) · Saga (microservices.io) · Cancel activity; compensation (Workflow Patterns, Van der Aalst)
Advice
Questions I ask before using it
- Which calls change something, and does each carry a key the receiver honours?
- If the answer to a call is lost, how do we learn what happened?
- For every step that changes something: what is the undo, and who wrote it?
- Can an agent in this process retry a tool call on its own, and should it?
Asked often
Retry and compensation: short answers
What is retry and compensation?
Retry and compensation is a workflow pattern in which every action carries an idempotency key, so that repeating it has no further effect, and every completed step has an undo that runs when a later step fails.
When should you use retry and compensation?
Typical uses: payments, orders, bookings: any action against a system that can time out; processes that span several systems with no shared transaction; agents that call tools with side effects. It is a poor fit for read-only steps, which can be repeated freely.
What is an idempotency key?
A unique value sent with a request. The receiver stores the result under that value, and when the same value arrives again it returns the stored result without acting a second time.
Should an AI agent retry a failed tool call?
Only when the call is safe to repeat. In the demo the agent did not retry a payment that timed out, which was the careful choice. A design should not depend on the agent choosing well.
What is compensation in a workflow?
Undoing completed steps after a later step fails, by running a counter-action for each one in reverse order: a reversal entry for a posting, a release for a reservation.
What does retry and compensation cost to run?
In the demo, the model calls of a run cost between $0.0001 and $0.019. The first run contains no model call, and the third contains one, to draft a note. Making failure safe is engineering, and it costs nothing per case once it is built. Measured from the runs recorded with Claude Haiku 5.5 and Claude Opus 5.5 on 26 November 2026.
The demo in detail
The run in words “Retry with an idempotency key”, step by step
Five payments go to the bank. The bank executes the third and the answer is lost on the way back. Paid or not? A workflow retries with an idempotency key. An agent gets the same batch and the same timeout. In the third run the bank rejects a payment after the ledger was already posted, and the earlier steps have to be undone.
- INV-01 to Noordzee Logistics: executed.
- INV-03 to Brouwer Packaging: executed.
- INV-05: the bank did not answer. Paid or not? The workflow cannot tell.
- The retry carries the same key, so the bank returns the first result and pays nothing twice.
- INV-05 to Polder IT Services: executed.
- INV-07 to Hanze Cleaning: executed.
- INV-12 to Lumen Office Supplies: executed.
- The batch is closed: five payments sent, five executed, none twice.
- Done. The timeout cost one retry and nothing else. No model was involved.
Building it This demo runs on plain Python and the Claude API: when I would pick it, and when not
This demo uses no agent framework: the retry, the idempotency key and the undo steps are plain Python. Other frameworks can carry the same pattern; this is when I would pick this one.
Reach for it when
- The process is yours and you want to read every line that touches money
- A decision table, a loop and a few API calls cover the job
- You need the recording and the control points more than a framework’s features
Skip it when
- Runs must survive a crash or wait days for a person: take a durable workflow engine
- Several teams build agents and need one shared way of working
- You would end up rebuilding checkpoints, retries and tracing by hand
In the framework’s own terms. Each payment is sent with a key derived from the invoice. The bank stores the result under that key, so a retry returns the first result. Every step that changes something registers its own undo, and a failure runs the registered undos in reverse order.
- Framework
- Plain Python and the Claude API (anthropic SDK 2.10)
- Licence
- MIT
- Model
- claude-opus-5-5, claude-haiku-5-5
- Watch time
- ~2 min
Contact
Questions, ideas or a conversation?
I’m always glad to hear from people working on AI, product strategy, transformation or software.