Sub topic 12
Recording decisions for audit
This chapter assumes you can read short Python programs, work with JSON, and understand the approval gate, the decision types (approve, edit, reject), and action binding from earlier chapters. Here you will record each approval decision so you can answer "who allowed this, and what exactly did they see?" later.
Why an audit trail
An audit trail is an append-only record of events that you can inspect after the fact. For approvals, it serves three purposes:
- Incident review: when an agent sends the wrong email, you can trace who approved it and what the reviewer was shown.
- Accountability: reviewers know their decisions are recorded.
- Debugging: rejection reasons and edits show you where the agent's proposals are weak.
Logging only "approved" is not enough. A useful record ties the decision to the exact action, using the fingerprint from the previous chapter.
What each record should contain
| Field | Purpose |
|---|---|
| `approval_id` | Links the record to the pending request |
| `run_id` | Links it to the workflow run |
| `fingerprint` | Hash of the tool name and arguments that were decided on |
| `action` | `approve`, `edit`, `reject`, `expired` |
| `reviewer` | Identity of the human, taken from your login system |
| `timestamp` | When the decision was made (UTC) |
| `reason` | Reviewer's note, required for reject |
| `original_args` and `final_args` | For edits, both versions, so the change is visible |
Record the reviewer from an authenticated session. Do not accept a reviewer name from the request body, because anyone could type another person's name.
Writing the records
The code below appends one JSON object per line to a file. This format is called JSON Lines. It is simple to write and easy to search.
import json
import time
import hashlib
def fingerprint(name: str, args: dict) -> str:
payload = json.dumps({"name": name, "args": args}, sort_keys=True)
return hashlib.sha256(payload.encode()).hexdigest()
def record_decision(path, approval_id, run_id, call_name, original_args,
decision, reviewer):
action = decision["action"]
final_args = decision.get("args", original_args) if action == "edit" else original_args
entry = {
"approval_id": approval_id,
"run_id": run_id,
"tool": call_name,
"action": action,
"reviewer": reviewer,
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"reason": decision.get("reason", ""),
"original_args": original_args,
"final_args": final_args,
"fingerprint": fingerprint(call_name, final_args),
}
with open(path, "a", encoding="utf-8") as f:
f.write(json.dumps(entry) + "\n")
return entry
entry = record_decision(
"approvals.jsonl", "ap-1", "run-42", "send_email",
{"to": "[email protected]", "body": "Hello"},
{"action": "edit", "args": {"to": "[email protected]", "body": "Hello, Ana"}},
reviewer="[email protected]",
)
print(entry["action"], entry["fingerprint"][:12])For an edit, the fingerprint covers the final arguments. That is what actually runs, so it is what the audit record must match.
Where the write belongs
Write the record before the tool runs, in the same step that resolves the approval. If you log after execution and the process crashes in between, the action may have happened with no record. If you log first and the tool then fails, you can add a second event for the outcome.
Use the diagram below to see the order.
Common errors
- Logging secrets. Arguments may contain tokens or personal data. Redact sensitive fields before writing, or store a reference instead of the value.
- Mutable logs. A file that anyone can edit proves little. Use append-only storage, restrict write access, and consider a hash chain, where each entry includes the hash of the previous one.
- Missing rejections and expirations. These matter as much as approvals. A pattern of rejections shows where the agent needs better instructions.
- No link between the decision and the outcome. Without an outcome record, you cannot tell whether an approved action succeeded.
Exercise
A reviewer edits the body of an email before approving it. Which two fields in the audit record show what changed, and which arguments should the fingerprint be computed from?
Show answer
original_args and final_args show the change. The fingerprint should be computed from final_args, because those are the arguments that actually run.
Exercise
Why should the reviewer identity come from the authenticated session instead of the decision payload?
Show answer
A payload field can be set to any value by whoever sends the request, so the record could name someone who never made the decision. The session identity is verified by your login system.