date: 2026-11-08

title: "Idempotency Stops Duplicate Writes, Not Partial Side Effects: Saga Compensation in Agent Pipelines"

author: Brian Taylor


Idempotency Stops Duplicate Writes, Not Partial Side Effects: Saga Compensation in Agent Pipelines

Brian Taylor’s agent pipelines are built around idempotency, retries, scheduling hygiene, and reliable external writes. Those patterns stop duplicate writes. They do not stop a second, harder failure: a multi-step workflow that succeeds on steps 1 and 2, then fails on step 3, leaving real business state committed by services that your single local transaction cannot reach. Payment, inventory, email, CRM, audit log, and Google Drive upload are not one database. A retry will not undo them; an idempotency key will not unwind them. The missing layer is compensation, usually implemented as a coordinator-driven saga.

Why idempotency is not rollback

Idempotency is an entry precondition: it says that if the same request is replayed, the effect happens at most once. It is excellent when a retry repeats the whole action. It is silent about partial completion. Suppose execute_checkout reserves a payment, allocates inventory, and then fails before scheduling shipping. Re-running the same call will find payment reserved twice unless the coordinator remembers the first attempt; it still has a half-finished business outcome if step 2 remains allocated while step 3 is abandoned.

A saga separates forward execution from reversal. Each step declares an inverse or compensating action: reserve-payment ↔ cancel-payment, allocate-inventory ↔ release-inventory, schedule-shipping ↔ cancel-shipping. On failure, the coordinator runs the already completed steps’ compensators in reverse order. Reverse order matters: cancelling payment before releasing inventory can orphan a recorded reservation with no compensating ledger.

Choreography vs. orchestration

There are two classic forms. Choreography lets services react to events; coordination is decentralized. Orchestration uses one coordinator that records step state. For an agent workflow, orchestration is the safer choice. An agent already has a central reasoning loop; a coordinator composes immediately into that place, keeps decision context readable, gives a single source of step records, and removes “we both assumed the other did it” gaps. Choreography can be right for long-lived business processes spanning independent services, but it does not fit a model-driven tool call whose model must understand the result.

The coordinator contract

A minimal coordinator needs a persistent journal, step state machine, reverse-order compensation, and compensation failures treated as a distinct class. Concretely:

  • sagaId must be deterministic from the business operation, e.g. chk_${orderId}. That makes a retried tool call land on the same partial outcome and resume or no-op rather than create a second saga.
  • compensate should accept a possibly-null execute result. Compensation before execution and compensation after execution are different branches; burying this in the type avoids crashes from assumed results.
  • Journal entry per step: running, succeeded, failed, compensated (or compensation_failed).
  • On failure, call compensators from the latest successful step backward. Record any compensator failure separately and alert humans; do not fold it into generic failure.
  • Outer execution reads the journal first and should not restart automatically from a compensated state unless a human supplies a restart flag. A model-generated retry on a compensated saga creates a new mess.
  • Idempotency keys for every compensator must be persisted with the side effect they write. If the key and the side effect separate, a retry can double-compensate.

Agent-specific failure modes

Agents introduce a new replay source: the reasoning loop. After a saga returns compensated, the model may interpret that as a transient condition and call the same tool again with the same inputs. Without a compensating-state guard, re-executing can reserve inventory a second time. Three defensive moves follow:

  1. Tool result plumbing must be deliberately designed. compensated should mean “stop; explain alternatives,” not merely “something failed.”
  2. System prompt examples should map compensated to proposing an alternative path rather than retrying.
  3. Prompt behavior deserves its own test harness: feed repeated compensated results and assert the model gives a user-facing proposal rather than re-invoking the tool. This sounds excessive, but model-mediated retries are a clean incident class.

What compensation cannot undo

Several common side effects are not truly reversible. Irreversible actions (sending an email, publishing an article, delivering a notification), destructive writes, and third-party charges may only be cancelled through a business process. Physical shipment is not cancelled by a database flag. More subtly, some classes need accurate classification before any compensator runs; misclassifying an irreversible action as a bufferable one in a test agent caused compensation “success” while the real system retained leakage. Treat every outcome either as compensated (verified reversal) or compensation_failed (requires a human, alert, and replay control).

Grounded facts that justify the layer

  • Naive agent tool execution is effectively at-least-once; with 30% timeout probability, roughly a third of tasks experience double side effects. Idempotency-ledger plus saga discipline took one reported test architecture to near-zero duplicates with about 10–15% overhead.
  • Sagas pair each forward action with a semantically inverse compensator and recover common forward/retry paths. Without a refund step, a failed downstream step leaves a successful payment and a bad customer state.
  • Coordinator state must be persisted outside the process or a crash loses the journal and duplicates everything on recovery.
  • Decorator/order discipline is important: idempotency must wrap retry, not the reverse, so duplicate dispatches do not become duplicate charges.

Rollout plan

Start with one small two- or three-step saga and lock the compensation contract, deterministic saga ID, and compensation idempotency convention. Persist state in Redis or a durable key-value layer until long-lived operation demands Postgres. Tracing and compensation-failure alerting should come before step four. Split into sub-sagas or switch to multi-agent only past roughly ten steps. A concrete saga running for weeks in production is a better template than a premature shared library.

Sources used (original external sites):

Sources