Skip to main content

Undoing AI agent actions: design the compensating step first

An agent that writes to real systems needs a planned way back. Pair each action with a compensating step, record what it changed and know what cannot be undone.

Share -
A data centre engineer in a white shirt holds his head in his hand while looking at monitors showing code and a glowing blue AI brain, with server racks behind him.

Plan the undo before the agent can act. For every action an AI agent can take in a real system, define a compensating step that reverses its effect, record the state it needs at the moment of the action, and mark actions that cannot be undone so they need approval first. Then test the undo path before launch.

Why rolling back the agent is not enough

When an agent misbehaves, the first instinct is to roll back its prompt, model or tools to the last good version. That stops new mistakes, but it does nothing about the ones already made. The refunds it issued, the records it updated and the emails it sent are still out there.

So an agent that writes to real systems has two kinds of rollback. One is reverting the agent itself, which our guide to versioning an AI agent for rollback covers. The other is reversing what it did in the world. Most teams build the first and discover they need the second during an incident.

Pair every action with a compensating step

Business systems rarely offer a single transaction that spans an agent's whole task. A task that creates an order, reserves stock and books a delivery touches three systems, and any step can fail after the earlier ones have succeeded.

The established answer is the compensating transaction pattern, also known as the saga pattern. Each forward step has a matching step that undoes its business effect, and if the task fails partway, the completed steps are compensated in reverse order. For an agent this means:

  • Create is paired with cancel or archive, not a hard delete that loses history.
  • Update is paired with restoring the previous value, which must be captured before the change.
  • Reserve or hold is paired with release.
  • Send or notify usually has no true reverse, only a follow-up correction.

Compensation is not the same as erasing. A cancelled order still exists with a cancelled status, and that is usually what finance and auditors want.

Record what the undo will need at the moment of action

An undo can only restore what was saved. If the agent changes a credit limit and logs only the new value, nobody can put the old one back with confidence. Capture the prior state, the identifiers of everything created and the context of the request when each action runs, not afterwards.

This record overlaps with a good AI agent audit trail, but it has a different job. The audit trail explains what happened. The undo record must contain enough to reverse it, and it must survive a restart of the agent, so keep it in durable storage alongside the task, not in the agent's memory.

Make compensating steps safe to repeat

Undo runs at the worst moment: during an outage, after a timeout, or when someone presses the button twice. Each compensating step should be idempotent, meaning running it again has no extra effect. Releasing a hold that is already released should succeed quietly, not raise an error or release someone else's hold.

Compensation can also fail. If one reverse step cannot complete, stop and report exactly which steps were undone and which remain, rather than retrying blindly. The same discipline applies to forward actions, as our guide to what to do when an AI agent tool call fails explains.

Check for changes made since the agent acted

Between the agent's action and the undo, people and other systems keep working. A customer may have used the credit the agent granted, or a colleague may have edited the record the agent changed. Blindly restoring the old value would overwrite legitimate work.

Before compensating, compare the current state with what the agent left behind. If they match, undo automatically. If they differ, hand the case to a person with both versions in front of them. That handover should feel routine, in the way our guide to human in the loop without friction describes.

Treat irreversible actions differently

Some actions cannot be compensated: a payment that has left the business, a message a customer has read, data shared with a third party. List these for each agent and treat them as a separate class. They are good candidates for approval before execution, for a delay window that allows cancellation, or for removal from the agent's toolset altogether. Our guide to stopping an agent taking a wrong action covers the controls that sit in front of them.

Test the undo path before launch

Compensating steps that are never exercised tend to break quietly. Run them in a sandbox with realistic data, including partial failures and concurrent edits, alongside the forward actions, as described in our guide to sandbox testing write actions. Include an undo drill in the launch checklist so the team knows who triggers it and what they will see.

If you are giving an agent write access to core systems and want the way back designed in from the start, talk to our team.

Frequently asked questions

What is a compensating action for an AI agent?

A compensating action is a step that reverses the business effect of something an AI agent did, such as cancelling an order it created or restoring a value it changed. It does not erase history. The original action and its reversal both stay on record, which keeps the audit trail intact and lets finance, support and compliance teams see exactly what happened and when it was corrected.

Is rolling back the agent's version the same as undoing its actions?

No. Rolling back the version changes the prompt, model or tools so the agent stops making new mistakes. It does not touch records, payments or messages the agent already produced. Undoing those needs compensating steps that act on the business systems themselves. A complete rollback plan covers both, and the second part is usually the one that is missing when an incident begins.

Which agent actions should never be automated without approval?

Actions that cannot be reversed or compensated are the main candidates: payments leaving the business, messages to customers, deletion of records and sharing data with third parties. For each agent, list these actions and decide whether they need a person to approve, a delay window that allows cancellation, or removal from the agent's tools. The answer depends on the cost of a mistake, not the agent's accuracy.

What should happen if someone changes a record after the agent acted?

The undo process should notice and stop. Before compensating, compare the current state of the record with the state the agent left. If they match, the undo can run automatically. If someone has edited it since, send the case to a person with both versions visible, so they can decide what to keep. Restoring the old value blindly would overwrite legitimate work done in the meantime.

Share this post

Contact us to build the right product

Talk to our engineers about your application, the systems it connects to, and what you want to build next.

Get in touch
Two people discussing work with a laptop