Topic 5 of 6 · 5 min read
Side effects, idempotency and dry runs
Goal: Add idempotency, a dry-run mode and a clear side-effect note to a tool that sends emails or payments.
Design every write tool on the assumption that the agent will call it twice, call it with the wrong arguments, or call it when it should not. Reads are safe to repeat. Sending an email or charging a card is not.
Check yourself · 1 of 3
An agent's call to a payment tool times out, so it calls again. What prevents a double charge?
Why it matters
Agent runs retry. A timeout, a rate limit or a model that is unsure whether a call worked all lead to a second call. Without protection that is two emails to a customer or two charges. Agents also misread the task, so a refund tool will occasionally be called with the wrong amount. The tool is the last place a mistake can be stopped cheaply.
How to do it
- State the side effect in the description. "Sends an email to the customer immediately. Cannot be undone." The model weighs actions it knows are final.
- Accept an idempotency key. The caller supplies a unique key; the tool stores it with the result. A repeated call with the same key returns the first result and does nothing new. Stripe's API documents this pattern for payments.
- Offer a dry run. A
dry_run: trueoption returns what would happen ("Would send to 3 recipients, subject 'Invoice 1042'") without doing it. Agents use it to check their own plan. - Prefer reversible actions. Archive instead of delete, draft instead of send, hold instead of capture. Return an undo handle where you can.
- Add a confirmation step for high-risk actions. Split into
propose_refundandconfirm_refund, where confirmation needs a token from the first call or a human approval in the harness. - Limit the blast radius in code. Maximum amount, allowed recipients, rate per run. Do not rely on the prompt for limits.
- Report what happened. Return the id, the time and whether the call was new or a repeat.
Before and after
// before: every call sends
async function sendInvoice(args: { customerId: string }) {
return mailer.send(buildInvoice(args.customerId));
}
// after
async function sendInvoice(args: { customerId: string; idempotencyKey: string; dryRun?: boolean }) {
const prior = await store.get(args.idempotencyKey);
if (prior) return { ...prior, repeated: true };
const mail = buildInvoice(args.customerId);
if (args.dryRun) return { wouldSendTo: mail.to, subject: mail.subject };
const result = await mailer.send(mail);
await store.set(args.idempotencyKey, result);
return { ...result, repeated: false };
}
What goes wrong
- Key per attempt. If the agent invents a new key on every retry, nothing is deduplicated. Tell it to reuse one key per intended action.
- Prompt-only safety. "Never refund over 100" in the system prompt is a request, not a control.
- Confirmation fatigue. Confirming every action trains people to approve without reading.
Examples
Propose, then confirm
refund_propose(order_id, amount) checks the order, returns Refund of 42.50 USD to the original card. Token: rf_91ac, valid 10 minutes. and changes nothing.
refund_confirm(token) performs it. In the harness, refund_confirm is set to require human approval above a threshold. The agent can plan freely; only the confirm step moves money.
A repeat that does nothing
{ "tool": "send_invoice", "args": { "customerId": "c_77", "idempotencyKey": "inv-1042-send" } }
The first call returns {"messageId": "m_5", "repeated": false}. After a network timeout the agent calls again with the same key and gets {"messageId": "m_5", "repeated": true}. The customer got one email.
Key points
- Retries, misread tasks and repeated calls are normal in agent runs, so write tools must survive them.
- Say in the description what the tool changes and whether it can be undone.
- An idempotency key makes a repeated call return the first result instead of acting again.
- A dry-run mode lets the agent preview an action, and reversible actions limit the damage of a wrong call.
- Enforce limits (amounts, recipients, rates) in code, not only in the prompt.
Common mistakes
- Assuming a write tool is called exactly once per intent.
- Describing a destructive tool in the same neutral words as a read-only one.
- Putting spending or recipient limits only in the system prompt instead of enforcing them in the tool.
- Skipping a dry run or preview, so the first sign of a wrong plan is the real side effect.
Check yourself · 2 of 3
What does a dry_run option on a send-email tool let the agent do?
Check yourself · 3 of 3
Which limit on a refund tool is most reliable?
Practice items, not counted towards your mastery.
Start here
- Writing effective tools for AI agents (Anthropic) (opens in a new tab)ArticleFree
Naming, namespacing, response design and evaluation loops for agent tools, from a team that builds them.
- Introducing advanced tool use (Anthropic) (opens in a new tab)ArticleFree
Tool search, programmatic calling and tool use examples for large toolsets.
- Define tools (Claude API docs) (opens in a new tab)DocsFree
How a tool definition is structured: name, description, input schema and examples.
More for this topic
- Idempotent requests (Stripe API reference) (opens in a new tab)DocsFree
A widely copied design for idempotency keys on write endpoints.