AI
Why Your AI Agent Sends Duplicate Messages (and What to Require Before Launch)
AI agent duplicate actions come from retry logic, not the model. Here is what idempotency means in practice and what to require before an agent touches real customers.
TL;DR
Duplicate sends are a retry problem, not an LLM problem. Webhooks, queues, and workers all deliver at least once, so every external action an agent takes needs an idempotency key, a reservation step, and a dedupe table. Require these before any agent contacts a real customer.

Your AI agent sent the message twice because something underneath it retried, not because the model decided to. Webhooks, queues, workers, and LLM API clients all default to at-least-once delivery, and an agent that calls the outside world without an idempotency key will eventually double-send, double-book, or double-charge. The fix is architectural and cheap relative to the damage, so it belongs in the requirements before the build starts, not in a post-launch patch.
Retries are the default, not the exception
Founders tend to picture an agent as a single conversation: a customer writes, the model thinks, a reply goes out. In production it is a chain of hops, and nearly every hop will repeat itself under real load.
The inbound webhook fires first. Messaging platforms resend a notification when your endpoint is slow to respond or returns an error. If your handler had already started processing before the timeout, you now have two copies of the same event in flight.
The queue is next. Standard queues like Amazon SQS are explicitly documented as at-least-once: a message can be delivered more than once and your consumer is expected to cope (AWS docs). Most managed agent runtimes sit on something equivalent.
A crash, a deploy, or an out-of-memory kill mid-task means the job restarts from the beginning. Everything it already did, it does again. The model call adds another layer: every serious LLM SDK retries on rate limits and transient errors. If the first attempt succeeded server-side but the response was dropped in transit, you pay twice and your code may act on both. Then the tool call goes out to a third-party API with its own timeouts. A timeout is not a failure; it is an unknown.
None of this is exotic. It is the normal behaviour of normal infrastructure. An agent that looks "99% reliable" in a demo is being tested at low load where none of this fires. A few thousand conversations a day, and all of it fires.
Why the LLM makes this harder than a classic job
A conventional worker that retries at least behaves identically the second time. An agent does not. If the retry re-runs a planning step, the model may pick a slightly different action: the first attempt sent "Your order ships Tuesday," the retry sends "Your order ships tomorrow" because the context window now reflects a half-written state. The customer gets two confident, contradictory messages from the same business.
This is why prompt-level fixes do not work. Telling the model "never send a message twice" assumes it knows the first attempt happened. It often does not, because the failure occurred after the model returned and before your code recorded the result. Deduplication has to live below the model, at the boundary where your system touches the world. That is the same principle behind the deterministic guardrails we cover in reliability for AI features: the model proposes, the code decides whether that action is still allowed.
What exactly-once effect actually requires
You cannot get exactly-once delivery from the network. You can get exactly-once effect by making every external action idempotent. Three things do the work.
An idempotency key per intended action
Before the agent sends anything, your code derives a key from what the action is, not from which attempt this is. For a reply, that is typically the inbound message ID plus the conversation step. For an order, the cart ID plus the customer ID. Two retries of the same intent produce the same key; two genuinely different actions do not.
Stripe's API is the reference design: the client supplies the key, the server stores the first result and returns it on repeats (Stripe docs). Your agent's send function should behave the same way, whether the downstream API supports keys natively or you enforce it in your own layer.
Reserve, then act, then record
The failure that hurts most is "sent but not recorded." The agent calls the messaging API, the API accepts the message, your process dies before writing to the database, and the retry sees no record and sends again. The fix is to reserve first: insert the key into an actions table with status pending before calling out. A unique constraint on the key means a concurrent or repeated attempt fails at the insert and stops (PostgreSQL constraints). After the external call succeeds, update the row to sent with the provider's message ID.
If you find a pending row older than your timeout threshold, that is a reconciliation case, not a resend case. Query the provider by key, or surface it for a human. Automatic resend is exactly the wrong default.
Dedupe inbound events at the edge
Webhook handlers should acknowledge quickly and store the event ID in a dedupe table with the same unique-constraint pattern. A duplicate delivery hits the constraint and is dropped before any model is invoked. This also saves money: every duplicate you discard at the edge is an LLM call you never pay for, which matters at the volumes discussed in what a WhatsApp sales agent costs.
A note from our own outreach engine
Our cold-outreach system scrapes each prospect's site and drafts one tailored email per company. Early on we assumed writing quality was the main problem. The pattern that won on both cost and reliability was a single model call that extracts facts and drafts the email together, rather than a multi-step chain. An underrated benefit: a single call has one output to make idempotent, so the "one email per company" guarantee could be enforced with one reservation row keyed on the company domain. Each extra step in a chain is another place a retry can double-fire, so collapsing the chain simplified the reliability story as much as it simplified the bill. The drafting approach is in single call vs agent chains.
What to require before an agent touches a real customer
Put these in the statement of work or the vendor contract. Each is a yes-or-no question with a demonstrable answer.
- Every outbound action has a named idempotency key, and the derivation logic is documented per action type.
- Reservation before send. Ask to see the table, the unique constraint, and the state machine. Ask what happens to a row stuck in
pendingafter an hour. - Inbound dedupe at the webhook, with the retention window stated. Platforms can redeliver the same event hours later.
- Retry-safe model calls. SDK retries are fine, but the surrounding code must not treat "no response" as "nothing happened."
- A chaos test in acceptance criteria. Kill the worker mid-send. Replay the same webhook three times. Force a timeout on the messaging API. Zero duplicates should reach the test number. An agency that cannot run this test has not built for it.
- Reconciliation, not resend, as the default when state is unknown. A human-visible queue for unknowns beats an automatic retry.
- Observability on duplicates. A counter for constraint hits is an early warning that infrastructure is retrying more than expected, long before a customer complains. Pair it with the alerting described in monitoring missed agent runs.
The cost of getting this right vs. the cost of skipping it
For a typical sales or support agent with three or four action types, retry safety adds a few days of focused engineering, mostly in key design and the reservation flow, plus a day for chaos tests. That is the cheap version.
The expensive version is skipping it: refunded double charges, a messaging number flagged for spam-like behaviour, and the gradual erosion that happens when customers learn the agent is not quite trustworthy. If a proposal for an agent that sends real messages does not mention idempotency anywhere, that is the question to ask before signing.
If you are scoping an agent that will act on behalf of your business and want the reliability requirements written down before the build starts, let's talk.
Frequently asked questions
Why did my AI agent send the same WhatsApp message twice?
Almost always because a webhook, queue, or worker retried after a timeout and the agent had no idempotency key tied to the intended action. The model did its job correctly; the infrastructure ran it twice.
Can prompt engineering stop duplicate actions?
No. The prompt runs inside the retry. By the time the model sees context again, it often has no way to know the first attempt succeeded. Deduplication has to live at the boundary where the agent calls the outside world, in code and in the database.
How much does making an agent retry-safe add to a project?
Usually a few days of engineering per external action type, most of it in designing the key and the reservation step. That is far cheaper than the refunds, blocked numbers, and trust damage that duplicates cause.
Does this apply to managed agent platforms too?
Yes. Managed runtimes retry failed steps automatically, which is useful, but the idempotency of your own actions is still your responsibility. Ask the vendor exactly where deduplication happens and what they do with an unknown state.
How Pykero can help
Services related to this article.
Building something like this?
Pykero Agency designs and ships production web, mobile, SaaS, and AI products.
Talk to us


