Spot the idempotency violation in this agent retry logic
Click any words you think contain an error. Click again to unmark.
The loop retries every tool blindly, so a non-idempotent side effect like charge or send fires once per attempt. The fix is an idempotency key giving at most once semantics.
Imagine you press the elevator button and nothing seems to happen. So you press it again, and again. With an elevator that is fine, because the button is idempotent: ten presses summon one elevator. But now imagine the button is a vending machine that charges your card on every push. The first push worked, the screen just froze before it could tell you. Press again and you pay twice. This code is the second machine. It retries the tool three times whenever the result does not say success, including when the action already happened but the reply got lost. For a charge or an email, that means duplicate charges and duplicate emails. The safe fix is to give each request a unique ticket number so the machine recognises the repeat and refuses to charge you twice.
Detailed answer & concept explanation~7 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Name the bug as a missing idempotency guard, explain why a lost response makes a succeeded call look failed, separate transient from permanent error classification, describe how an idempotency key yields at most once effects over at least once delivery, and close with the compounding agent and tool retry interaction plus circuit breakers.
Real products, models, and research that use this idea.
- Stripe's API requires an Idempotency-Key header on charge requests so a retried payment after a timeout returns the original charge instead of billing the card twice.
- AWS SQS standard queues guarantee at least once delivery, so a message can be redelivered after a visibility timeout lapses, forcing consumers to be idempotent.
- Temporal models activities as at least once and pushes developers to make activity handlers idempotent, because the workflow engine replays and retries on worker failure.
- The Anthropic and OpenAI SDKs auto-retry transient HTTP failures, which is safe for completions but would be unsafe for a custom side-effecting tool lacking an idempotency key.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you make a tool idempotent when the underlying API does not support an idempotency key?
QWhere should the idempotency key be generated, and why not let the server mint it?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Assuming a failed result means the side effect did not happen. A timeout or lost response can hide a tool call that already charged the card or sent the email.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.