Guide

Why your AI agent sent the same email twice, and how to stop it

The assistant understood the request both times. It still did the job twice, because it could not tell the first one worked.

Short answer

Give every action that changes something (an email, a refund, an order) one idempotency key, made from what the action is for, and reuse the same key on every retry. Keep a small ledger of keys already done and skip any action whose key is in it.

Reading data twice is harmless. Sending an email, refunding a payment or placing an order twice is not. When an agent repeats an action that changes something in the real world, a customer sees it.

Why agents repeat actions

  • The reply was lost. The action worked, but the confirmation timed out. From the agent's side the result is unknown, so it retries as if it were a new request.
  • A restart. The process crashes after the action and starts again from the top of the task, with no memory of what it already did.
  • The same trigger twice. Two webhooks or two queue messages for one event start two runs that do the same work.
  • The model forgets. After a long run the earlier step is out of its context, and it decides the job is still to do.

The fix: give every action one key and reuse it

A retry is only safe if the other side can recognise it as the same request. Payment services do this with an idempotency key: send the same key twice and the second call returns the first result instead of charging again.

The rule that matters: build the key from what the action is, and reuse it on every retry.

def refund_with_retry(service, order_id, amount, tries=3):
    # ONE key for this action. Build it from what the action is,
    # never from a random value generated on each attempt.
    key = f"refund:{order_id}:{amount}"

    for _ in range(tries):
        try:
            return service.refund(order_id, amount, idempotency_key=key)
        except TimeoutError:
            continue          # the first attempt may have worked, so reuse the same key
    raise RuntimeError("gave up")

We tested this on 20 November 2026 with a simulated payment service that makes the refund but loses the reply. A retry with a fresh random key made 2 refunds. A retry with the same key made 1.

When the other side has no idempotency key

Email services, chat tools and many internal APIs do not offer one. Then keep your own ledger: write down what was done, and check it before you act. Keep the ledger somewhere that survives a restart.

import json, os

class Ledger:
    """Remembers what the agent already did, across restarts."""
    def __init__(self, path):
        self.path = path
        self.done = set(json.load(open(path))) if os.path.exists(path) else set()

    def already_done(self, key):
        return key in self.done

    def mark_done(self, key):
        self.done.add(key)
        json.dump(sorted(self.done), open(self.path, "w"))

def send_once(ledger, send_email, to, subject):
    key = f"email:{to}:{subject}"
    if ledger.already_done(key):
        return "skipped, already sent"
    send_email(to, subject)
    ledger.mark_done(key)
    return "sent"

In our test the first call sent the email and a second call from a fresh process, reading the same file, skipped it. One email went out.

One rule for retries

Retry automatically only when the step is read-only or has an idempotency key. For anything else, look at what already happened before you try again.

What this does not cover

  • Two near-identical actions. If the key includes a changing part such as a timestamp, two attempts get two keys. Build keys from the stable facts only.
  • Races. Two runs checking the ledger at the same moment can both see "not done". A shared database with a unique constraint closes that gap.
  • Actions you cannot undo. A key stops a repeat. It does not stop a wrong first action.

Doing part of it with SteerPlane

SteerPlane does not add idempotency keys for you, and it does not remove duplicates. What it can do is hold the risky actions for a person, so a refund or a send never fires unseen. Install it with pip install steerplane:

from steerplane import SteerPlane

def ask_a_person(action, details):
    return input(f"Allow {action}? (y/n) ").strip().lower() == "y"

sp = SteerPlane(agent_id="support_agent")

with sp.run(
    max_cost_usd=5,
    require_approval=["send_*", "refund_*"],
    approval_callback=ask_a_person,
) as run:
    run.log_step("read_orders", tokens=100, cost=0.001)
    run.log_step("refund_payment", tokens=50, cost=0.0005)   # waits for a person

On version 1.1.3, a refused approval ends the run with a policy error, and your callback is asked before the action goes through. This is a good stopgap while you add keys, and a good permanent guard for actions that should never be automatic. More on this in how to stop an agent from deleting data.

A checklist before you ship an agent

  • Every action that changes something has one key, built from stable facts.
  • Retries reuse the same key.
  • Where there is no key, a ledger records what was done.
  • Only read-only or keyed steps retry automatically.
  • You have tested what happens when an action works but the confirmation never arrives.
Keep reading
Design partners

Shipping agents? Let's talk.

Our first 10 design partners get every paid feature free, plus direct help setting up.