Retry, Resume, Roll Back, or Compensate? A Recovery Guide for AI Agents
When an AI agent fails halfway through a task, “try again” is not a recovery plan. It is one possible transition, and often the most dangerous one.
The right action depends on two facts: what durable state survived, and whether the failed step may already have changed the outside world. A parser timeout, a lost response after creating a ticket, and a partially completed deployment are all failures. They do not have the same recovery semantics.
I use five distinct verbs: retry, resume, roll back, compensate, and reconcile. Treating them as synonyms hides the exact decision a runtime must make.
Start with the effect, not the exception
Exception classes describe what software observed. Recovery must describe what reality permits.
| Observed state | What it means | Default transition |
|---|---|---|
| No side effect started; failure is transient | The same bounded operation can run again | Retry |
| Durable checkpoint proves earlier steps completed | Continue from the next uncommitted step | Resume |
| Local state has a known restorable snapshot | Restore that state and verify it | Roll back |
| Committed external effects need domain-specific reversal | Execute new compensating actions | Compensate |
| The effect may have happened, but evidence is incomplete | Query the target or stop for review | Reconcile |
This table is deliberately conservative. A model can help classify the failure, but it should not grant itself permission to repeat a write. The runtime needs evidence strong enough for the transition.
That is the practical extension to my comparison of agents, harnesses, workflows, and runtimes: a runtime is not defined by persistence alone. It owns the recovery decision after persistence and external reality disagree.
Retry: repeat an attempt, not an intention
Retry fits a transient failure when repeating the operation is safe. A read request may qualify. An idempotent upsert under a stable key may qualify. A model call may qualify if its output has not crossed an effect boundary and the retry budget is explicit.
A retry needs at least:
retry:
operation: fetch_build_status
error_class: transient_transport
attempt: 2
max_attempts: 4
backoff: exponential
side_effect: none
Backoff and attempt limits control load. They do not make an unsafe operation safe. Microsoft’s Retry pattern warns that a service may process a request successfully and then fail to return the response. Retrying a non-idempotent operation can therefore repeat the effect.
Temporal makes another useful separation: Activities are failure-prone operations and have retry policies by default, while Workflow code must remain deterministic for replay. The architectural lesson is broader than one platform. Put uncertainty at a boundary whose retry behavior is explicit; do not wrap the entire agent in an undifferentiated loop.
Resume: continue from proven progress
Resume does not repeat the failed run from the beginning. It reconstructs durable execution state and continues from the next valid transition.
A checkpoint is useful only if it answers more than “the process reached step 4.” It must identify the input and code version, completed artifacts, pending operations, and receipts for committed effects. Otherwise the runtime may skip work that never happened or repeat work whose response was lost.
{
"run_id": "release-1842",
"checkpoint": "after-tests",
"artifact_digest": "sha256:…",
"completed": ["build", "test"],
"pending": ["publish"],
"effects": []
}
Resume is the normal choice after a clean interruption before the next effect. If the checkpoint and current environment no longer match, the state is not resumable merely because it is readable. It needs migration, replay validation, or a fresh run.
Rollback: restore state you actually control
Rollback restores a known state inside a boundary that supports restoration: a Git worktree, a database transaction, a deployment revision, or a filesystem snapshot.
It does not erase external observation. Reverting a published configuration does not mean nobody used it. Restoring a database snapshot can overwrite concurrent work. Rolling back application code does not reverse messages, invoices, or API calls already emitted by that code.
A valid rollback contract names four things:
restore target -> protected boundary -> verification probes -> invalidation conditions
If any of them is missing, “rollback” is an aspiration. This is why an AI engineering runtime needs checkpoints and verification together. A restore command exiting with zero is evidence about the command, not proof that the protected system is healthy.
Compensation: create a new effect that repairs an old one
Compensation is not rollback. It is a forward action with business meaning.
Canceling a reservation compensates for creating it. Issuing a refund compensates for a charge. Posting a correction compensates for a message that cannot be unsent. The original effect remains part of history, and the compensation can fail too.
Microsoft’s Compensating Transaction pattern makes the boundary explicit: compensation may need application-specific rules, may run in a different order from the original actions, and must record progress because the compensating workflow itself can be interrupted. The Saga pattern similarly coordinates local transactions and invokes compensating transactions when a later step fails.
For an agent runtime, every compensating step needs its own identity and receipt:
compensation:
for_effect: invoice-send-1842
effect_id: invoice-void-1842
action: void_invoice
precondition: invoice.status == "issued"
terminal_states: [voided, not_voidable, manual_review]
The precondition matters. Compensation cannot blindly replay an imagined past. It acts against current state, including concurrent changes and points of no return.
Reconcile: resolve the unknown before choosing
The hardest state is not failure. It is ambiguity.
Suppose an agent calls create_release, the destination commits the release, and the connection dies before the response reaches the runtime. The step is neither safely complete nor safely retryable. The honest state is EFFECT_UNKNOWN.
Reconciliation asks the destination whether the intended effect exists. It uses a stable business key, destination ID, payload hash, or target-state query. If the destination can return the prior result, the runtime records the receipt and continues. If it proves absence, the runtime may retry under the same effect identity. If neither can be proven, automatic execution stops.
This builds directly on stable effect IDs. Effect identity prevents attempts from becoming new intentions. Reconciliation connects that identity to external truth.
if receipt(effect_id): confirm previous outcome
elif target_matches(intent): adopt and record outcome
elif target_proves_absence: retry same effect_id
else: require review
The last branch is a feature. A runtime that refuses to guess preserves more control than an agent that confidently duplicates reality.
Design each step with a recovery row
Before allowing an agent workflow to mutate state, I would require one compact row per effect:
| Step | Effect class | Proof of success | Safe retry | Restore/compensation | Unknown-state query |
|---|---|---|---|---|---|
| Generate patch | local artifact | digest + diff | yes, new attempt | delete candidate | file/digest lookup |
| Update database | conditional write | row version | same key only | transaction or domain action | read by business key |
| Publish release | external create | release ID | idempotency key only | archive/delete if supported | lookup tag/release |
| Send message | irreversible delivery | provider message ID | usually no | correction message | provider status/search |
This is not ceremony for every model response. Apply it where a workflow crosses durable or external state. The higher the consequence and the weaker the destination’s idempotency, the stronger the receipt and reconciliation path must be.
AWS’s durable execution guidance states the core constraint plainly: replay and retry may each execute an operation more than once. Choosing at-most-once handling can avoid automatic re-execution, but it also leaves interrupted work that the application must resolve. There is no universal switch that produces exactly-once reality across arbitrary systems.
The recovery decision
I reduce the choice to a short sequence:
- Can I prove no effect started? Retry if the error is transient and the budget allows it.
- Can I prove a durable checkpoint and completed effects? Resume from the next valid transition.
- Is all changed state inside a restorable boundary? Roll back, then verify.
- Did committed effects escape that boundary? Compensate with new identified actions.
- Can I not prove what happened? Reconcile before doing anything else.
The language matters because each verb grants different authority. Retry repeats an attempt. Resume advances execution. Rollback restores controlled state. Compensation changes reality again. Reconciliation withholds action until reality is known.
An AI agent may propose any of these. The runtime must decide which one is allowed.