Agents are increasingly asked to complete whole business tasks rather than single actions. Onboarding a supplier might mean creating a vendor, raising a purchase order and paying a deposit, often across different systems.
Those systems share no transaction. When the payment fails, the vendor and the order already exist, and other people may already be able to see them. No database rollback can reach across them.
The established answer to this problem is the saga pattern. It predates AI agents by decades, and agents make it more relevant.
The pattern in brief
A saga splits one business transaction into a sequence of local steps. Each step commits on its own. For each step that can be reversed, the design also defines a compensating action: a separate business operation that semantically undoes it.
If a step fails, the saga runs the compensations for the completed steps in reverse order.
| Step | Forward action | Compensation |
|---|---|---|
| First | Create vendor | Deactivate vendor |
| Second | Raise purchase order | Cancel purchase order |
| Third | Pay deposit | None: irreversible |
If the third step is refused, the order is cancelled and then the vendor is deactivated. The reverse order matters because later steps often depend on earlier ones. You would not want to deactivate a vendor while an open order still points at it.
Compensation is not rollback
This distinction causes the most confusion, so it is worth being exact.
A database rollback makes it as if the change never happened. A compensation is a new action, recorded in the system’s history. After the example above, the vendor still exists as an inactive record and the order as a cancelled one. In a finance system this is usually what you want, since the trail of what happened stays intact.
It also means a saga gives no isolation. While it runs, other users and processes can see the intermediate state. Someone could read the new vendor before it is deactivated. A compensation also cannot reach side effects that the system triggered on its own, such as an email it sent when the order was created.
What changes when an agent writes the plan
In a conventional saga, a developer designs the sequence and its compensations together, and they ship as code. An agent assembles its sequence at run time. That creates three problems.
The agent cannot be trusted to know the way back. If compensation is improvised after a failure, the failure handling depends on the model reasoning correctly at the worst moment, about a state it may not fully understand. So the undo has to be declared in advance, by someone who knows the system.
The plan has to be checked before it starts. An agent can compose steps in an order that could never be unwound. That should be caught before the first write.
The agent should not run the recovery. Its process can crash, its context can be lost, and its session can end. Recovery has to live in something durable that carries on regardless.
How Sefra applies it
In Sefra, a plan is a fixed, ordered list of typed intents, and each write declares its way back or says that it has none.
compensate:
connector: erp
method: POST
path: "/vendors/${output.vendor_id}/deactivate"
A write with neither a compensate block nor irreversible: true fails validation, so no agent ever sees it as a tool.
The plan rules follow from that. A plan may hold at most one irreversible step, and that step must be last. Everything before it can then still be undone if it fails. A plan that breaks this rule is refused with PLAN_INVALID before any call is made.
The workflow runs durably on Temporal, outside the agent. If a worker is killed partway through, the execution resumes when workers return. The agent submits a plan and can poll for the outcome, and its own failures do not affect the recovery.
The compensation can fail too
Designs often stop at the happy path of the unhappy path, assuming every undo works. In practice the system you are compensating against may be the one having problems.
So compensations need the same care as forward steps.
They must be idempotent. A compensation that is retried must not do further damage. Cancelling a cancelled order should change nothing.
They are retried. A brief failure of the cancel call should not stop the rollback. A plan whose cancel fails once still ends rolled_back, with the vendor inactive, the order cancelled and the budget restored.
They need a success condition. An answer can look successful while the effect did not happen. By default, Sefra counts a compensation as done on its declared HTTP status. A manifest can also declare success_when, so that the response body must hold specific values. If it does not, the undo is retried and then handed to an operator.
They need an exit. If a compensation keeps failing, the execution enters needs_intervention, and, if the alert webhook is configured, an operator is alerted. Once the cause is fixed, the operator can retry the compensation and the execution ends rolled_back. If the systems were repaired by hand, the operator can close it with a note, and it ends resolved_manually. At that point Sefra guarantees nothing about the systems’ state, and the record says who made that call.
The case a saga cannot decide
Compensation assumes you know a step failed. Sometimes you do not. A connection can drop after the system committed the change, and running compensations then could cancel an order that was in fact paid.
A correct design treats that as a separate state. The workflow stops without compensating, and a person establishes what happened. Our article on unknown outcomes covers this in detail.
Before you adopt the pattern
Sagas are not free. Each compensation is an operation someone has to design and test, and some actions have no real reverse.
If your agent performs one write in one system, you do not need any of this. If it performs several writes that depend on each other, write out the table above for your workflow before you build anything. Fill in the compensation for every step. The rows you cannot fill in are the risks you are actually taking, and it is better to find them on paper than during an incident.