Skip to content
mentoor

Topic 4 of 5 · 5 min read

Idempotent retries for tool calls and side effects

Goal: Find a retry path that would repeat a side effect and fix it with an idempotency key.

Retrying a model call is safe because it only produces text; retrying a tool call that sends, charges or deletes is not safe unless it carries an idempotency key. An operation is idempotent when doing it twice has the same effect as doing it once.

Check yourself · 1 of 3

A tool call times out. Why can retrying it be dangerous?

Choose one answer

Why it matters

A timeout does not tell you the operation failed. The email may have been sent and only the response was lost. If your retry logic, your agent loop or your queue runs the tool again, the customer gets two emails or two charges. Agents make this worse: a model that is told "that call timed out" may decide to call the tool again by itself.

How to do it

  1. List side effects. Reading data is safe to retry. Sending email, creating orders, charging cards, posting to Slack and writing rows are not.
  2. Generate the key once, before the first attempt. Derive it from the business action (order id plus action name), or create a UUID and store it with the pending action. Never create a new key per retry.
  3. Send the key with the request. Stripe-style APIs accept an Idempotency-Key header and return the original result for a repeated key. For your own tools, store the key in a table with a unique constraint and check it first.
  4. Record the result. Save what the first call returned, so a retry returns the same answer without redoing the work.
  5. Make unsafe tools say so. Give the agent tool definition a clear note that it must not be called twice for the same request, and enforce this in code, not in the prompt alone.
 async function refundOrder(orderId: string, amount: number) {
-  return payments.refunds.create({ charge: orderId, amount });
+  return payments.refunds.create(
+    { charge: orderId, amount },
+    { idempotencyKey: `refund:${orderId}` }
+  );
 }

Before and after

Weak: an agent calls send_receipt, the connection drops, the retry wrapper calls it again, and the customer gets two receipts. With a refund the cost is real money.

Fixed: the wrapper creates receipt:order-8841 once, the mail service stores it, and the second call returns "already sent" with the original message id.

What goes wrong

  • A fresh key per attempt, which makes the key useless.
  • Dedup in memory only. A restart or a second server forgets the key. Store it durably.
  • Same key, different payload. Good APIs reject it; yours should too, instead of returning a wrong cached result.
  • Prompt-only protection. Telling the model "do not repeat actions" is not a guarantee. The check belongs in the tool.

Examples

Key derived from the action

Tool: create_invoice(customerId, month). Key: invoice:cus_204:2026-11. The invoices table has a unique index on the key. A retry hits the index, finds the row and returns the existing invoice instead of creating a second.

The agent that re-sent an email

The tool result is a timeout, and the agent decides to try again. The mail tool checks dedupe_key = thread-77:reminder-1 in the database, finds it, and answers already_sent. The agent learns the true state and moves on.

Key points

  • A timeout does not prove the action failed, so a retry can repeat a side effect that already happened.
  • Create the idempotency key once, from the business action, before the first attempt, and reuse it on every retry.
  • Store the key and the first result durably, with a unique constraint, so retries return the original outcome.
  • Reads can be retried freely; sends, charges, creates and deletes need keys or at-most-once handling.
  • Enforce safety in the tool code, not only in the agent's prompt.

Common mistakes

  • Generating a new UUID inside the retry loop.
  • Keeping idempotency keys in process memory.
  • Assuming the model will not repeat a call because the prompt says so.
  • Treating every error as safe to retry, including ones where the effect may have happened.

Check yourself · 2 of 3

Where should the idempotency key for a refund be created?

Choose one answer

Check yourself · 3 of 3

Which operation can usually be retried without an idempotency key?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic