DEV Community

Cover image for Idempotency keys for AI agents, v2: how to stop your agent from sending the same email twice after a crash

Idempotency keys for AI agents, v2: how to stop your agent from sending the same email twice after a crash

Anis Meziani on November 10, 2026

An agent is sending 100 emails. The pod dies after the 40th. Replay the run, and 40 customers get the email twice. Don't, and 60 never get it. Ou...
Collapse
 
mnemehq profile image
Theo Valmis •

The crash window I would test next is after the pending row commits but before the provider ever sees the request. It can look identical to a crash after the provider accepts it. For tools with a provider idempotency key, you can reconcile by resending that same key. For tools without one, I’d record how the operator decided it was safe to bump the epoch. That decision is worth keeping alongside the receipt.

Collapse
 
anis_meziani_52aab42304a8 profile image
Anis Meziani •

Good call, a pending row alone can't tell "never sent" from "sent, no receipt". So pending after a crash has to be treated as unknown, unless the provider takes an idempotency key: then resending the same key is the reconciliation.

And agreed on the operator's decision. Our ledger design records who bumped the epoch, not why. Want to take the ledger table as a PR (task 2 in the post), with a reason field next to the epoch? Comment on #473 to claim it: github.com/apowerb/apowerb/issues/473

Collapse
 
mnemehq profile image
Theo Valmis •

I'd make the reason required at bump time and store which pending rows the bump resolved. Then you can query later which resets rested on an idempotency key and which rested on an operator's judgment, and the second group is the one to audit after an incident.

Thread Thread
 
anis_meziani_52aab42304a8 profile image
Anis Meziani •

Agreed on both. A required reason, plus the pending rows the bump resolved, turns the ledger into an audit trail: resets settled by a provider key on one side, resets resting on someone's judgment on the other. That second list is exactly what you'd pull after an incident.

That's a clean spec for the ledger table. If you're up for writing it as a PR, comment on #473 and it's yours: github.com/apowerb/apowerb/issues/473

Collapse
 
delayrun profile image
delay.run •

The six-state ledger is the part I'd steal. Making unknown block the key and go to a human is the honest call, but it also means the size of that bucket quietly becomes your real reliability number. For providers that accept their own idempotency key, passing the effect key through could let a replay settle a lot of those rows automatically: resend with the same key and the provider either returns the original receipt or creates the thing once. Did you consider different ledger rules per provider depending on whether they support that?

Collapse
 
anis_meziani_52aab42304a8 profile image
Anis Meziani •

Yes, and you're right about the bucket: the size of "unknown" is the real reliability number, so it should be a metric, not a log line.

Per-provider rules make sense, with one catch from another comment here: some APIs accept the key but create a duplicate if the retry lands outside their window. So I'd declare it per tool: "honours idempotency keys for N hours". Inside that window, resend with the same key and settle automatically. Outside it, unknown goes to a human.

Want to sketch it on #466? github.com/apowerb/apowerb/issues/466

Collapse
 
sudokat_ profile image
Gordon Chase •

One catch with bumping v1: to v2:: a fresh key space doesn't stop a duplicate on replay, it invites one. If a pod crashes mid-deploy and retries under v2:, the unique index misses the v1: row and emails customer #1 twice. Would you pin canon_version on the trigger, or dual-read old prefixes during cutovers?

Collapse
 
anis_meziani_52aab42304a8 profile image
Anis Meziani •

Good catch, a fresh key space is exactly what lets the replay through. I'd pin it on the trigger: the canonicaliser version is recorded when the event is first seen, and every replay of that event derives its key with that version, never the current one. New events get v2, in-flight ones finish on v1. Dual-read is a decent safety net during the cutover, but pinning makes it unnecessary.

There's an RFC on exactly this, pinning the version per event: github.com/apowerb/apowerb/issues/481. Your crash-mid-deploy case would make a good test for it.

Collapse
 
alexcodebytes profile image
Oleksandr •

"Generated content stays out of the key by construction, not by convention" is the sentence I'll be quoting at people for the next year. A function that simply has no parameter for the body is worth more than any amount of prompt-level discipline.

The other thing worth saying: a design that got measurably better because readers found a serialisation bug and wrote an RFC is a much better argument for open review than any blog post about "building in public."

One failure case I'd add to the list: what happens when the provider itself is non-idempotent behind an idempotency key — some APIs accept the key but silently create a duplicate if the retry lands during a different billing window. Have you hit that?

Collapse
 
anis_meziani_52aab42304a8 profile image
Anis Meziani •

Thanks, that's exactly why the key function has no body parameter: discipline doesn't survive the next refactor.

We haven't hit that one, but it's the reason our own ledger decides, not the provider's key. A provider key is a reconciliation tool, not a guarantee: we only trust a receipt we can look up later. Your case also makes "resend the same key" risky as a recovery path: across a billing window, "unknown" should go to a human instead.

Which API was it? Worth adding to #476 with the provider named: github.com/apowerb/apowerb/issues/476

Collapse
 
alexcodebytes profile image
Oleksandr •

"Discipline doesn't survive the next refactor" is a great line — I'm stealing that. The API was SendGrid (v3 mail send); their idempotency keys are solid, but their error states on network timeouts are notoriously vague. I'll drop a comment on #476 with the details. And yeah, routing "unknown" to a human across a billing window is definitely the safest move.

Collapse
 
challan116ux profile image
challan116-ux •

The six-state ledger is what I would steal first, especially the rule that unknown never means did not happen — a provider 500 after a call that may have landed is exactly the case where guessing wrong creates a duplicate. Your canonicalisation example also lands well, because five representations of the same call producing five hashes is the kind of bug that looks like a model problem until someone measures it. The epoch rule that only a human can bump it is the guardrail I would keep, since an agent that can re-authorise its own retry has quietly removed the only protection the ledger offered.