Note
How do retries avoid repeating an external effect?
Idempotency Is an AI Agent Feature
Retries, concurrent webhooks and provider timeouts can duplicate real-world effects even when the model behaves correctly.
Note
How do retries avoid repeating an external effect?
Retries, concurrent webhooks and provider timeouts can duplicate real-world effects even when the model behaves correctly.
An AI agent can reason correctly and still send the same message twice.
The model may choose the right tool with the right arguments. The duplicate happens later: a webhook arrives twice, a worker times out after the provider accepted the request, or two processes observe the same incomplete state and both decide to continue.
This is why idempotency is not infrastructure trivia around an AI product. It is part of the agent's behavior.
The model produces intent. The surrounding system turns that intent into an effect: sending a message, creating a payment arrangement, updating a record or calling an external service.
Between those events, failure is ambiguous.
Suppose a worker asks a messaging provider to send a reply and the request times out. There are at least two possible realities:
A blind retry is correct in the first case and a duplicate in the second. The model cannot solve that ambiguity from the prompt. The delivery architecture must solve it.
A common implementation looks safe:
Under concurrency, two workers can both complete step one before either reaches step three. Both see "not sent" and both produce the external effect.
The protection must be atomic. Depending on the workflow, that can mean:
An idempotency key should describe the business operation, not merely the HTTP request.
For example, a generated random key per retry prevents nothing because every attempt looks new. A stronger key might combine the conversation, intended action, recipient and workflow step. The right scope depends on what "exactly once" means for the product.
The key also needs a lifecycle. Some operations are unique forever. Others may repeat after a day, a state transition or a new campaign. Encoding the wrong scope can either allow duplicates or suppress legitimate work.
External effects rarely fit inside the same transaction as local persistence. That creates an unavoidable half-done state.
The system should represent it explicitly:
This state model gives recovery code something concrete to inspect. It also helps operators distinguish a slow provider from a dead worker or a permanently invalid destination.
Messaging providers commonly report several stages: queued, sent, delivered, failed, blocked or expired. Those callbacks may arrive late, more than once or out of order.
Treating every callback as a command is dangerous. It is safer to treat it as
evidence and apply a monotonic transition policy: an old queued event should
not move a terminally delivered message backward, and two concurrent failed
events should not create two fallback sends.
The internal workflow needs authority over its own state while preserving the provider's detail for diagnosis.
Users do not experience the model and the queue as separate products. A duplicate message looks like the agent repeated itself. A delayed callback that reopens a completed conversation looks like the agent forgot the agreement.
From the user's perspective, reliability is part of intelligence.
That is why production AI engineering includes unique constraints, leases, outboxes, state machines and provider semantics. The model decides what should happen. Idempotency is what makes sure it happens the intended number of times.