Skip to content
mentoor

Topic 2 of 5 · 4 min read

Safe retries with idempotent tool calls

Goal: Mark which steps of an agent job can be retried safely and add idempotency keys to the steps that have side effects.

Assume every step of an agent job can run more than once, and make each step safe to repeat: reads and model calls are retried freely, steps with side effects carry an idempotency key. Queues and workflow engines deliver work at least once. A worker can finish a step, crash before it records the result, and the step runs again.

Check yourself · 1 of 3

Which agent step most clearly needs an idempotency key?

Choose one answer

Why it matters

An agent's tools do real things: send an email, create a ticket, charge a card, post to Slack. If the worker crashes after the email is sent but before the job is marked done, the retry sends it again. The customer gets two refund confirmations, or two charges. The retry logic did what it was told. The step was not safe to repeat.

Sort every step into one of three groups

  1. Pure or read-only. Reading a database, calling a model, searching the web. Repeating costs tokens or time, nothing else. Retry freely.
  2. Naturally idempotent writes. PUT /tickets/42 {status: "closed"} gives the same result however many times it runs.
  3. Non-idempotent side effects. Send an email, create a record, charge a card. These need an idempotency key.

An idempotency key is a unique string you attach to the request. The receiving service stores the key with the result. A second request with the same key returns the stored result and does nothing new. Stripe's API works this way with an Idempotency-Key header.

Where the key comes from

The key must be the same on every retry of the same logical action, and different for different actions. Derive it from the job and the step, not from a random value created at call time.

// Weak: a new key on every retry, so the dedupe never triggers
await stripe.refunds.create(
  { charge: chargeId },
  { idempotencyKey: crypto.randomUUID() },
);

// Better: stable per job and step
await stripe.refunds.create(
  { charge: chargeId },
  { idempotencyKey: `${job.id}:refund:${chargeId}` },
);

If a tool has no key support, add your own guard: write a row (job_id, step) with a unique constraint before the call, and skip the call if the insert fails.

The model is not deterministic

If the agent decides what to send, a retry may decide something different: another email subject, another amount. Persist the decision (the tool name and arguments) before executing it, and on retry execute the saved decision rather than asking the model again.

What goes wrong

  • Random keys. A key generated inside the retried code defeats the purpose.
  • Keys that expire too soon. Providers keep keys for a limited window; a retry after the window creates a second charge. Check the provider's retention.
  • Retrying a non-retryable error. A 400 invalid argument will fail again. Retry timeouts, 429 and 5xx; fail fast on the rest.
  • Trusting the tool description. A tool called create_order may not be idempotent. Read the API docs.

Examples

Unique constraint as a guard

INSERT INTO sent_emails (job_id, step, to_addr)
VALUES ('j_81', 'notify', 'dana@example.com')
ON CONFLICT (job_id, step) DO NOTHING;

If the insert affects 0 rows, the email was already sent on an earlier attempt, so the step returns without sending again.

Classifying a research agent's tools

web_search and read_file: read-only, retry freely. update_ticket_status: idempotent write. send_email and issue_refund: side effects, key required.

Key points

  • Every step may run more than once, so design each step to be safe to repeat.
  • Sort steps into read-only, naturally idempotent, and side-effecting; only the last group needs an idempotency key.
  • The key must be identical on every retry of one action, so derive it from the job id and step, never from a random value at call time.
  • Save the model's chosen tool call before running it, so a retry replays the same decision.
  • Retry timeouts, 429 and 5xx responses; do not retry client errors that will fail again.

Common mistakes

  • Generating the idempotency key inside the retried function with a random value.
  • Retrying every error the same way, including validation failures.
  • Assuming a tool is idempotent because its name sounds like a lookup.
  • Asking the model to redo a decision on retry and getting a different email or amount.

Check yourself · 2 of 3

A refund tool call uses idempotencyKey: crypto.randomUUID() inside the retried function. After a timeout the job retries. What happens?

Choose one answer

Check yourself · 3 of 3

Which key design makes a retried send_invoice step safe and still allows two different invoices in one job?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic