New Tool Radar

AI Agents & Automation

How to Use Idempotency Keys for Safe Agent Retries

Give agent retries idempotency keys so a repeated write lands once instead of twice.

Schematic with three steps: Generate one key per logical write; Send the same key on every retry; Test by retrying a write that already.

A retry is only safe if repeating the write does not repeat the effect. That is what an idempotency key does: the first call carries a unique key, and if the same key arrives again, the receiving system returns the first result instead of doing the work twice. This matters most where a run can pause and resume, because LangGraph restarts the interrupted node from the top on resume, so a write placed before an interrupt can run again. Generate one key per logical write, store it with the run, and send the same key on every retry of that write.

What you need

  • A write the agent makes that must not happen twice, such as a payment or a record creation
  • A receiving system that accepts an idempotency key, or a check that the record already exists
  • One key per logical write, generated when the write is first planned

Step 1: Generate one key per logical write

When the agent plans a write, generate a unique key for that write and store it with the run record. The key belongs to the write, not to the attempt, so retries reuse it.

Step 2: Send the same key on every retry

Pass the stored key with the write on the first attempt and on every retry. A new key per attempt defeats the purpose, because the receiver sees a new write each time.

Step 3: Test by retrying a write that already landed

Run the write once, then send it again with the same key and confirm the receiver returns the first result without creating a second record. If a duplicate appears, the key is not being honoured.

Worked example: synthetic idempotency check

The table below is a synthetic example. It shows the expected result of idempotency keys; no write was made.

Check Expected Result
First write lands 1 record Pass
Retry with the same key 0 new records Pass
Retry returns the first result Yes Pass
New key makes a new write Yes Pass

Expected result (UNTESTED). the first write lands once, the retry with the same key creates nothing new, and a new key makes a new write. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • The receiving system must honour the key; a key nobody checks is just a label.
  • Keys must be unique per logical write, or two different writes will collapse into one.
  • A key stored only in memory is lost on a restart, so store it with the run record.
  • Idempotency protects the write, not the read: a retry can still return stale data.

Sources

See all