Why Does Your AI Agent Send the Same Email Twice?
I once built an agent whose tools included sending quote emails to customers. One day, right after the agent sent an email but before it could record "sent," the system restarted. After the restart, the agent had no idea the email had already gone out, so it sent it again. The customer received two identical emails. Worse, if the price had changed during the restart, the customer would have received two quotes with different prices. That raises the question: how do you make an agent remember that a tool has already been called?
The problem isn't the LLM. An LLM is stateless; it can only decide based on its messages. The crash happened exactly between "the email was sent" and "the result was written back to messages." So after the restart, the LLM saw exactly the same context it saw before sending, naturally concluded the email hadn't been sent, and sent it again. The real problem is that the side effect had already happened, but the system never recorded it.
Payment systems ran into this long ago: "the charge went through, but we crashed before recording it." Their solution: before calling the payment provider, generate an idempotency key (such as a payment_intent_id) and write it, together with a PENDING status, into a payment intents table. Then call the payment API with that key, and on success update the status to SUCCEEDED. If the process crashes midway, on restart it sees PENDING and queries or retries with the same key. The payment provider recognizes the key and won't charge twice.
Applying this to agents, the first question is what to use as the idempotency key. You can't use tool_call_id, because it's freshly generated on every LLM inference. After a restart, the LLM runs inference again and produces a new ID. The ledger finds no matching record, and the email goes out again. So we use run_id:step:index instead. run_id is generated and persisted when the task starts, step is the inference round, and index is the position of the tool call within that round. All three are determined by the program, not the LLM, so when a restart replays to the same point, it computes the same key.
Anything that must stay the same across restarts cannot be generated by the LLM.
With a stable key, we can give the agent a ledger, ToolLedger (like reconciliation in payment systems). Before every tool call with side effects, look up the key in the ledger:
No record found: this is the first execution. Write PENDING first, call the tool with the key, then on success update to SUCCEEDED and store the result.
SUCCEEDED found: this already happened. Don't call the tool; return the stored result to the LLM. The LLM gets exactly the context it would have had without the crash and never notices there was a restart.
PENDING found: the last run crashed mid-call, so the email may or may not have been sent. If you can query the email service, reconcile against it. If you can't, retry with the same key and rely on downstream idempotency as the safety net: the email service recognizes the key, sends normally if it wasn't sent last time, or returns the previous result if it was. If neither is possible, flag it for human review.
I looked at how pi, the popular harness, handles this. Its experimental pi-durable records the intent before executing a tool. On recovery, it does not re-run by default and explicitly tells the LLM that the tool was interrupted and may have only partially executed. Its docs also acknowledge that tools with external side effects still need their own idempotency. This shows that persisting the conversation history isn't enough: the idempotency key must be generated by the program, written to disk before the call, and handed downstream as the safety net. Agent reliability doesn't live in the model. It lives in the harness.