Topic 5 of 5 · 5 min read
Undo, rollback and audit trails for agents
Goal: Specify the audit log fields and the undo path for an agent that edits records in a shared system.
Assume the agent will sometimes do the wrong thing, and make sure every action can be traced to a cause and, where possible, reversed. Approval gates lower the odds of a bad action. Undo and audit limit the damage when one gets through.
Check yourself · 1 of 3
Which field is most important for an undo of an edit to a record?
Why it matters
A shared system, such as a CRM, a database or a document store, has other users. If an agent edits 300 customer records wrongly, you need to answer three questions fast: what changed, who or what caused it, and how do we restore it. Without a log and an undo path, the answers are guesswork and a backup restore.
Make actions reversible
- Prefer soft changes. Mark a record as archived instead of deleting it. Write a new version instead of overwriting.
- Store the before-state. For each edit, save the old values next to the new ones. Undo is then a write of the old values.
- Stage first. Let the agent create drafts, branches or pull requests, and let a person merge.
- Use compensating actions where true undo is impossible: a refund for a charge, a correction email for a wrong email. Say in the policy which actions have none, and gate those hardest.
- Keep an undo window. Hard-delete only after a retention period.
What the audit log records
One row per tool call, written by the harness, not by the model:
{
"event_id": "evt_90211",
"run_id": "run_7f3a",
"timestamp": "2026-12-02T14:03:11Z",
"agent": "support-agent@1.4.2",
"model": "model-name-and-version",
"on_behalf_of": "user_318",
"tool": "update_record",
"args": {"table": "customers", "id": 4417, "field": "plan", "to": "pro"},
"before": {"plan": "free"},
"approval": {"by": "user_102", "at": "2026-12-02T14:02:40Z", "decision": "approve"},
"result": "ok",
"request_id": "req_55a1"
}
The key fields are who asked, who approved, what exactly ran, the before-state and the result. Leave secrets and full personal data out of the log; store ids. Make the log append-only, and give the agent no tool that edits it. The OWASP logging guidance covers which event data to record and how to protect it.
Recovering after a bad action
- Stop: pause the agent or revoke its credentials.
- Scope: query the log by run id or time window to list every affected record.
- Restore: replay the before-states, or run the compensating actions.
- Fix the cause: a missing gate, a vague tool, a prompt hole.
- Record it with the run id and a note on what changed.
Before and after
Weak: the agent has direct write access, and the only record is the chat transcript.
Better: writes go through a tool that stores before and after values, tags the run id and approver, and exposes undo_run(run_id) to a person.
What goes wrong
- Logging only the prompt. You can see what the model said, not what changed.
- A log the agent can edit. It then cannot be trusted after an incident.
- Undo that was never tested. Restore from the log in a drill before you need it.
Examples
Undoing a run
def undo_run(run_id: str, actor: str):
events = audit.find(run_id=run_id, result="ok", reverted=False)
for e in reversed(events):
db.update(e["args"]["table"], e["args"]["id"], e["before"])
audit.append(event="undo", ref=e["event_id"], by=actor)
It walks the run's events newest first, restores each before-state and logs the undo itself.
Finding the blast radius
A customer reports a wrong plan. A query on the audit log for tool = update_record and run_id = run_7f3a returns 212 rows across two hours. Each row has the old value, so the fix is a scripted restore, and the approver on the batch is known.
Key points
- Prefer reversible actions: soft deletes, versioned writes, drafts and staged changes that a person merges.
- Store the before-state with every edit so that undo is a write of the old values.
- The harness writes one audit row per tool call: who asked, who approved, exact arguments, before-state and result.
- Keep the log append-only and out of the agent's reach, and keep secrets and full personal data out of it.
- After a bad action, stop, scope by run id, restore, fix the cause and record it; drill the undo path in advance.
Common mistakes
- Giving the agent direct database write access with no wrapper that records before and after values.
- Treating the chat transcript as the audit log.
- Letting the agent's own credentials modify or delete log entries.
- Building an undo tool and never testing it until an incident.
Check yourself · 2 of 3
Who should write the audit log row for a tool call?
Check yourself · 3 of 3
An agent edited 212 records wrongly. After stopping the agent, what is the best next step?
Practice items, not counted towards your mastery.
Start here
- Interrupts (LangGraph docs) (opens in a new tab)DocsFree
How an interrupt pauses a graph, stores state and resumes with the human's answer.
- Persistence (LangGraph docs) (opens in a new tab)DocsFree
Checkpoints and threads: the saved state that makes pause and resume possible.
- Configure permissions (Claude Code) (opens in a new tab)DocsFree
A working example of allow, ask and deny rules for an agent's tools.
More for this topic
- Logging Cheat Sheet (OWASP) (opens in a new tab)ArticleFree
Which event fields to record and how to protect logs; applies to agent audit trails.
- AI Risk Management Framework (NIST) (opens in a new tab)StandardFree
A governance frame for human oversight roles and incident response around AI systems.