Rollback is not an automatic property of an agent. If a tool has already reserved a resource or changed a record and a later step fails, restarting the whole workflow may repeat the first effect. The runtime needs to know what was confirmed and choose recovery from that state.

The practical rule is to classify each effect as retryable, compensable, irreversible, or unknown. Retry only what has not happened yet or what has an idempotent contract. For a confirmed, compensable effect, run a recorded business action. For an unknown or irreversible outcome, reconcile or stop for human review. The durable execution guide for AI agents explains which layer persists the flow. This article focuses on what to do after a partial failure.

The example uses a small Node.js fixture. It records two completed steps, simulates a third-step failure, and applies compensations in reverse order. It is not a workflow engine, does not call external APIs, and does not prove that a distributed transaction is atomic.

Diagram shows an AI agent workflow recording two commits, failing at a tool, and choosing compensation or human review.

Short answer

  • Do not retry the whole workflow when an earlier step has already produced a confirmed effect.
  • Record the tool, validated arguments, operation key, observed result, and the way to verify the effect.
  • Use an explicit business compensation to correct a confirmed effect. Do not ask the model to invent an undo.
  • Treat unknown, irreversible effects, and compensation failures as states that may require reconciliation or human review.

Compensation is not time travel

An ACID transaction in one database can undo changes before commit. An agent workflow often crosses databases, queues, third-party APIs, and systems that do not participate in the same transaction. The Saga pattern coordinates local transactions and defines compensating transactions when a later step fails, but it does not create global isolation or rollback by itself (Microsoft, Saga distributed transactions pattern).

In an agent, compensation is a new operation with business meaning. Canceling a reservation, voiding an authorization, or restoring a configuration can be useful compensation. An email cannot be removed from the recipient's inbox. The response may be a correction message, a support task, or human review. The final state should say what was repaired, not merely that a code loop finished.

The Saga pattern also separates compensable transactions, a pivot or point of no return, and later steps that must be idempotent. That distinction is more useful for an agent than the generic word rollback: it shows which steps still have a known recovery action (Microsoft, Saga design).

Define a contract for every write tool

The agent may choose the next action, but the runtime should know what the tool changes. Before allowing a write, record a contract that answers these questions:

Contract part Question it must answer
effect What resource or state changes?
verify Which read or receipt proves that the change happened?
idempotency How is the same intent recognized if it is repeated?
compensation Which action corrects or neutralizes the confirmed effect?
approval Does the forward action or the compensation require a person?
impact Is the effect local, customer-visible, financial, destructive, or irreversible?

A tool may declare compensation: not_supported. That is not a contract failure. It is a decision that forces the workflow to block, ask for approval, or run a documented procedure when the step cannot be undone. The problem is hiding the limitation behind a generic function named undo.

AWS guidance for agentic systems uses Saga to describe local transactions with compensations across agent boundaries. The connection is useful, but it does not mean that an LLM can decide by itself that an operation is safe. The contract and authorization belong to the system that executes the tool.

Record the effect before choosing recovery

The journal must survive the process that called the model. For each step, keep at least:

  1. runId, stepId, and the workflow version.
  2. Tool name, validated arguments, and idempotency key.
  3. Step state: planned, started, committed, failed, unknown, compensating, or compensated.
  4. Receipt, version, or read that can verify the external resource.
  5. The recorded compensation, its derived arguments, and the dependency it must respect.

Write the intent before the external call, but mark it committed only when there is enough evidence. If the connection drops after the request was sent and before the receipt arrives, the correct state is unknown. The post about resuming an AI agent after a crash explains why a checkpoint proves what the runtime recorded, not necessarily what the external system did.

This record is different from a transactional outbox. The outbox protects the obligation to publish a message with a local change. The effects journal answers another question: which workflow actions actually reached their target systems, and what recovery is allowed now?

Choose between retry, reconciliation, and compensation

A small matrix keeps every failure from becoming an automatic retry:

Observed state Next action Why
planned or failed before the call Retry with the same intent No external effect was requested, if the contract confirms that.
unknown result Reconcile by receipt, ID, or a safe read A repeat could duplicate an operation that was already accepted.
Confirmed, compensable effect Run the recorded compensation The effect existed and a business action can correct it.
Confirmed, irreversible effect Open a correction or human-review path There is no reliable undo to call automatically.
Compensation failed Stop, preserve state, and escalate Continuing blindly can break the premise of the next compensation.

AWS's Saga guidance separates forward recovery, which continues or retries a local transaction, from backward recovery, which emits a compensating transaction (AWS, Saga patterns). Applied to tools, the important point is not to confuse a transport failure with an effect failure. Find the state first. Then choose the direction.

Node.js fixture: compensate only what was confirmed

The fixture below simulates inventory reservation, payment authorization, and shipping creation. The third step fails. Because the first two are committed, recovery walks their compensations in reverse order.

import assert from "node:assert/strict";

const steps = [
  { id: "reserve_inventory", compensation: "release_inventory" },
  { id: "authorize_payment", compensation: "void_payment" },
  { id: "create_shipping", compensation: null },
];

function runWorkflow(failAt) {
  const journal = [];

  for (const step of steps) {
    const entry = { ...step, status: "started" };
    if (step.id === failAt) {
      entry.status = "failed";
      journal.push(entry);
      break;
    }

    entry.status = "committed";
    journal.push(entry);
  }

  return journal;
}

function compensateCommitted(journal) {
  const actions = [];
  for (const entry of [...journal].reverse()) {
    if (entry.status !== "committed") continue;
    if (!entry.compensation) {
      return { status: "manual_review", actions };
    }
    actions.push(entry.compensation);
  }
  return { status: "compensated", actions };
}

const journal = runWorkflow("create_shipping");
const recovery = compensateCommitted(journal);

assert.deepEqual(
  journal.map(({ id, status }) => ({ id, status })),
  [
    { id: "reserve_inventory", status: "committed" },
    { id: "authorize_payment", status: "committed" },
    { id: "create_shipping", status: "failed" },
  ],
);
assert.deepEqual(recovery, {
  status: "compensated",
  actions: ["void_payment", "release_inventory"],
});

console.log("partial effect recovery passed");

Save it as partial-effects.mjs and run:

node partial-effects.mjs

The test proves only two local properties: recovery uses the journal to select confirmed effects and applies actions in reverse order. It does not call inventory, payment, or shipping. In production, journal transitions need to be durable, concurrent, and idempotent in the storage the team chooses.

Compensation order follows dependencies

Walking steps in reverse order is a useful default when each step depends on the previous one. It is not a universal law. If two reservations are independent, their compensations can run in parallel. If releasing payment depends on canceling shipping, the graph must express that dependency instead of relying only on timestamps.

AWS's Saga orchestration guidance describes a coordinator that controls sequencing, branches, retries, timeouts, and failures in agentic workflows. In a custom runtime, the same idea means that the orchestrator persists the transition and knows which actions remain allowed. The model may explain a failure, but it should not reorder a high-impact compensation because that sequence looks plausible in context.

A compensation has an effect too. It needs an idempotency key, verification, and its own state. If void_payment fails after the provider accepts the request, do not continue to release_inventory as if payment were resolved. Preserve unknown or compensation_failed, query the provider, and escalate when the next action depends on that result.

When to stop for human review

Stop automation when the effect cannot be verified, has no recorded compensation, requires authority the tool does not have, or could cause more harm than the original failure. The reviewer should receive the journal, available receipts, effective arguments, and authorized options. Do not give them only a model-generated summary.

The human approval guide for agents defines when an action needs a person. This question is different: after part of the workflow has happened, which correction can be authorized? If compensation changes the recipient, amount, or scope of the original effect, treat it as a new action and apply the approval policy again.

Common failures that make this design unsafe:

  • restarting the whole plan and letting the agent choose already confirmed steps again;
  • marking every timeout as failed, inviting the worker to repeat an effect that may be in progress;
  • keeping compensation only in the prompt or in an unpersisted message;
  • calling undo without checking that the resource is still at the expected version;
  • marking the workflow rolled_back because a compensation function returned, without checking business state;
  • continuing after a compensation failed and changed the assumption behind the next action.

Method and limits

Samuel Fajreldines is responsible for source selection, the fixture, and the final wording. The research combined public Microsoft and AWS documentation, current-result inspection in English, Portuguese, and Spanish, recent public discussions, and a search for overlap in the local cluster.

The fixture ran locally with Node.js and uses only node:assert/strict. It proves an in-memory journal and reverse compensation for two confirmed steps. It does not test PostgreSQL, queues, process concurrency, payment APIs, an agent SDK, or a distributed transaction. No production experience or benchmark is claimed.

I used AI assistance to organize research, review the prose, and prepare the localizations. It did not run a real workflow or provide operational experience. When I need continuity across long Codex and Claude Code sessions, I use RemoteCode, a tool I own. That does not replace runtime validation or authorization by the target system.

Conclusion

A partial failure calls for a state decision, not a reflexive retry. Record what was actually confirmed, separate unknown effects from compensable effects, and make every tool declare how its result can be verified or corrected. If there is no safe recovery, stop and request human review. A reliable workflow does not pretend that the world returned to its previous state just because the agent started again.

Sources consulted