§ Engineering

AI Agent Idempotency: The Engineering Pattern That Prevents Duplicate Actions in Production

— Luke Needham··8 min read
AI Agent Idempotency: The Engineering Pattern That Prevents Duplicate Actions in Production

In November 2026, a Manchester recruitment consultancy discovered their AI agent had sent the same candidate briefing email to seventeen clients — twice. The agent had run successfully, but a webhook retry fired it a second time 90 seconds later. The briefing went out again. No error was thrown. No alert fired. The clients noticed. The firm spent two days recovering the relationship damage. The root cause was not a model failure, a prompt error, or a system crash. It was a missing idempotency layer — the engineering pattern that prevents an AI agent from performing the same action more than once, no matter how many times it is triggered.

AI agent idempotency pattern — dark tech diagram showing idempotency keys preventing duplicate actions in production AI agent workflows for UK service businesses

Why AI Agents Are Unusually Prone to Duplicate Actions

AI agent duplicate actions problem — workflow showing how webhook retries, timeout restarts and parallel triggers cause AI agents to fire the same invoice, email or CRM update multiple times in production

Traditional software has predictable failure modes. When a function runs twice, the second run either succeeds again or throws a conflict error. AI agents are different. They are event-driven, retriable, and often triggered by external systems — webhooks, Zapier, n8n schedules, CRM automations — that make no guarantees about firing exactly once.

Consider the triggers that commonly run UK service business agents:

  • A webhook from your CRM fires when a new lead is created — and fires again if the receiving server returns a 5xx error.
  • A scheduled n8n workflow runs at 9am, times out at the LLM layer, and the retry mechanism fires the full workflow again 60 seconds later.
  • A Slack message triggers a client briefing agent — and the same message is delivered twice because Slack's at-least-once delivery guarantee does not mean exactly-once.
  • A human clicks "Run" twice on a UI that does not disable the button after the first click.

None of these are bugs in your agent. They are expected behaviours of distributed systems. The agent has no way to know it has already run — unless you build a layer that tells it.

Idempotency is not a nice-to-have for production AI agents. It is the engineering contract that makes them safe to run on real business operations.

The consequences of missing it scale with the stakes of the action. Reading data twice is harmless. Sending an invoice twice is a client relationship problem. Posting a contract twice is a legal exposure. Triggering a payment twice is a financial error. As UK service business agents take on more consequential actions — which is the point — idempotency protection becomes non-negotiable. This is distinct from broader fault tolerance engineering, which handles failures. Idempotency handles successful re-execution.

The Four Idempotency Patterns

Four AI agent idempotency patterns illustrated as an engineering framework — idempotency keys, deduplication windows, state machine guards and outbox pattern for UK production AI agent systems

There are four patterns used in production AI agent systems. Each applies to a different failure mode. Most agents need at least two.

Pattern 1: Idempotency Keys

An idempotency key is a unique identifier attached to every execution of an action. Before the agent performs an action, it checks a store — a Redis hash, a Postgres table, a simple n8n variable store — to see whether that key has been seen before. If it has, the action is skipped. If it has not, the action runs and the key is recorded.

The key is generated from the inputs that define the action — not a random UUID. For an invoice-sending action, the key might be a hash of client_id + invoice_number + date. For an email action, a hash of recipient + subject + date_window. This means the same logical action triggered twice by different events produces the same key and is deduplicated automatically.

This pattern handles the most common duplicate trigger scenario: a webhook that fires twice because the first delivery was not acknowledged. It is the simplest pattern to implement and covers the widest range of agents.

Pattern 2: Deduplication Windows

Idempotency keys work well when actions have a natural unique identifier. Some agent actions do not. A daily client summary email does not have an invoice number — but you still only want it sent once per day per client.

Deduplication windows solve this by recording that an action was performed within a time window, and refusing to perform it again until the window expires. The check is: "Has this action been performed for this client in the last 24 hours?" If yes, skip. If no, run and record.

The window length should match the action's natural frequency. Daily digests use a 24-hour window. Onboarding sequences use a 48-hour window. Proposal follow-ups use a 7-day window. The window is not a retry delay — it is a business-logic guard that reflects how often an action should actually happen.

Pattern 3: State Machine Guards

Idempotency keys and deduplication windows prevent duplicate actions at the trigger level. State machine guards prevent duplicate actions at the workflow level — particularly in multi-step processes where an agent might reach the same action from two different execution paths.

The pattern is to treat each major action as a state transition. Before executing, the agent checks the current state of the entity it is acting on. If the state already indicates the action was completed, the agent skips it and moves forward.

For example: a client onboarding agent that sends a welcome pack, creates a CRM record, and schedules an intro call. If the agent runs twice, the state machine checks whether the CRM record already exists before creating a new one. The action is guarded by the state of the system, not just the agent's execution history. This integrates naturally with the state machine patterns that production agent systems already use for reliability.

Pattern 4: Outbox Pattern

The first three patterns protect against duplicate execution. The outbox pattern protects against a subtler failure: an agent that performs an action and crashes before recording that it did, so on restart it performs the action again.

The outbox pattern writes the action to an outbox table — a pending record — before performing it. A separate process reads from the outbox, performs the action, and marks the record as completed. If the agent crashes between writing the outbox record and completing the action, the outbox record is still there. The separate process performs the action exactly once and marks it done.

This pattern is particularly important for agents handling payments, contract sends, or any action where "did it happen?" must be auditable. It is more complex to build than the first three patterns but provides the strongest guarantee.

Building an Idempotency Layer in n8n

n8n workflow showing AI agent idempotency layer implementation — idempotency key check node, Redis deduplication store, and state guard before email, invoice and CRM action nodes in production UK AI agent workflow

For UK service businesses running agents in n8n, the most practical starting point is a two-pattern combination: idempotency keys for discrete actions, and deduplication windows for recurring ones.

The implementation has three parts:

  1. A key generation function node that runs before any consequential action node. It takes the action's inputs and produces a deterministic hash using a SHA-256 function node.
  2. A key check node that queries a Postgres table (or n8n's built-in static data) for the generated key. If the key exists, the workflow branches to a "skip" path that logs the deduplication event and exits cleanly. If the key does not exist, execution continues to the action.
  3. A key record node that runs immediately after the action succeeds, writing the key and a timestamp to the store. The key expires after a configurable window.

This pattern wraps around your existing action nodes — it does not replace them. A workflow that sends a proposal, creates a CRM record, and books a discovery call adds three key check/record pairs, one per consequential action. The overhead is minimal: two database reads and one write per action, adding less than 100ms to typical workflow execution.

The deduplication window for each action is set as a configurable constant — 24 hours for daily digests, 7 days for follow-up emails, 30 days for onboarding sequences. When an agent fires twice within the window, the second run is logged, not silently dropped. That log matters: a surge of deduplication events tells you your trigger system has a problem that needs fixing upstream, not just at the idempotency layer.

For agents that also need output guardrails — where the concern is not just duplicate execution but malformed outputs reaching clients — idempotency and guardrails are complementary layers, not substitutes. Build both.

Stress-Testing Your Idempotency Layer

Idempotency layers that are not tested fail in production at the worst possible moment. The test cases are straightforward:

  • Double trigger test: Fire the same webhook twice within five seconds. Verify the action runs once and the second trigger is logged as a deduplication event.
  • Restart test: Trigger the workflow and kill it partway through execution. Restart it. Verify that actions already completed are not repeated and actions not yet completed run correctly.
  • Window expiry test: Trigger an action, wait for the deduplication window to expire, then trigger again. Verify the action runs correctly on the second trigger after the window.
  • Key collision audit: Confirm that two different actions cannot produce the same key hash. If they can, refine the key generation logic before promoting to production.

These four tests take less than an hour to run against a staging environment. Run them before promoting any agent to production. Run them again whenever the trigger system changes — a new CRM integration, a changed webhook provider, a new n8n schedule — because new triggers introduce new duplication risk.

The agents that compound in value over time are not the ones that never fail. They are the ones where failures are visible, logged, and contained.

The goal is not a perfect system on the first build. It is a system where duplication failures are auditable, recoverable, and do not reach clients. That is a significantly higher bar than "no guardrails" — and a significantly lower bar than "perfect".

The Business Case Is Simple

Production-grade AI operating system with idempotency protection — shield symbol overlaid on AI agent workflow network, representing reliability and trust for UK service businesses deploying agents on consequential operations

Idempotency engineering is not glamorous. It does not make your agents faster, smarter, or more capable. It makes them safe — safe enough to run on the operations that matter: client communications, financial documents, CRM data, project updates. Without it, every agent you run on a consequential workflow is a risk that scales with usage. The more it runs, the more chances for a duplicate action to reach a client.

UK service businesses deploying agents seriously — not as pilots, but as production infrastructure — are the ones adding idempotency layers to their workflows. It is the engineering discipline that separates an AI operating system built for the long term from a collection of automations that require constant monitoring and manual correction.

The pattern connects directly to tool calling reliability: idempotency protects the outer execution loop, while tool call hardening protects the individual action calls within it. Both layers are needed. Neither is a substitute for the other.

If you are building AI agents for your service business and want to make sure the engineering foundations are right, talk to us. We help UK service businesses design and deploy production-grade AI operating systems that run reliably on the operations that matter.

L

Written by Luke Needham

Founder at Quantum Flow Automation — building AI systems that work.

§ 99Subscribe

More field notes, in your inbox.

One email per week. What we shipped, what broke, what's worth paying attention to in AI.

BOOK CALL