DEV Community

Orvi Das
Orvi Das

Posted on Originally published at robatdasorvi.com

AI Agent Audit Trails for Compliance: Tamper-Evident Logs

Originally published on robatdasorvi.com

An AI agent audit trail for compliance is a record of every action the agent took, written so that neither the agent nor its operator can quietly edit it afterward. Store each event in a hash chain, keep the verification key outside the agent's process, and copy the chain head somewhere the agent cannot write. Once the EU AI Act's high-risk obligations apply, Article 19 requires providers of high-risk systems to keep automatically generated logs for at least six months.

You are the person who opens the log after the account is already gone. Maybe your agent posted, replied and followed for weeks, and now a platform says it broke a rule. Or a customer asks which model made a decision, and the only answer you have is a .log file on the machine that ran the agent. You are about to find out whether that file is evidence or a diary the agent was allowed to write in.

Is an Audit Trail Actually Required for My Agent?

It is required if your agent works in a regulated use case. It is practically required anyway if you ever need to explain what it did. The EU AI Act's record-keeping duties attach to high-risk systems, but a platform suspension, a customer dispute or an insurer's questionnaire will ask for the same thing: a sequence of actions with timestamps you can defend.

The Act is specific about the mechanism. Article 12 requires high-risk systems to technically allow automatic recording of events over their lifetime. Article 19 sets the retention floor for providers at six months, unless other law sets a longer one (EU AI Act, Article 19). Article 26(6) sets the same six-month floor for deployers, for logs under their control (EU AI Act, Article 26). The Annex III high-risk obligations apply from 2 December 2027, under the Digital Omnibus postponement (Gibson Dunn).

The practical test is simple. If you cannot reconstruct, from storage alone, which tool call preceded which external effect, you don't have an audit trail. You have output.

Why Does an Ordinary Application Log Fail a Compliance Review?

An ordinary application log fails because the process that writes it can also rewrite it. Standard logging libraries append to a file. That file sits on the same disk as the agent, and the agent's credentials often have write access to the datastores the log describes.

Look at what that looks like in practice. In July 2025, during a twelve-day experiment run by Jason Lemkin, the SaaStr founder, a Replit coding agent ran and deleted a production database, despite a code freeze instruction, according to reports collected by the AI Incident Database. The agent told Lemkin a rollback was impossible. He later reported that the rollback worked, so the agent's own account of what it had done was wrong as well. The same database served development and production. Had Lemkin relied on the agent's own report, he would have believed the data was unrecoverable. Lemkin said the agent "hid and lied about it." Replit's CEO called the outcome unacceptable (eWeek, 2025).

The lesson from Replit isn't that the agent was malicious. The only account of what happened came from the agent itself, and that account was wrong. Without an independent record of each action, written where the agent can't edit it, a reviewer has nothing to check the agent's story against.

In July 2025, version 1.84.0 of the Amazon Q Developer VS Code extension shipped with an injected prompt telling the agent to wipe local files and cloud resources. AWS removed that version and issued security bulletin AWS-2025-015. AWS stated that the injected code did not run successfully on customer environments (AWS-2025-015). When an agent's instructions can be altered upstream, a record of what it was told and what it actually did is the only way to reconstruct the blast radius.

My own bot's records failed differently. Nobody tampered with them. They were just wrong. Every post, reply, follow and like went through a Telegram approval I pressed by hand. For months the Skip button silently did nothing, so every skipped item was recorded as a timeout and the approval statistics were wrong. For about six weeks, four hardcoded target accounts that no longer existed absorbed 88% of daily comment attempts, and every run logged success. A log that records "success" for a no-op is an audit trail of a lie.

What Does Tamper-Evident Mean for an Agent Log?

Tamper-evident means any later edit, deletion or reordering of an entry breaks a check you can run. The log can still be damaged. The point is that the damage can't go unnoticed.

The idea is old. Schneier and Kelsey (1998) described a computationally cheap scheme in which log entries written before a machine's compromise can't be read by the attacker, and can't be modified or destroyed without detection (USENIX Security 1998, Schneier and Kelsey). The core mechanism is a chain. Each entry's hash covers the previous entry's hash, and each entry is authenticated under a key that evolves forward and is then deleted, so an attacker who seizes the machine holds only the current key, not the ones that protected earlier entries. Delete an entry and every later hash stops matching.

NIST's log management guidance treats integrity as a storage-stage requirement alongside confidentiality and availability (NIST SP 800-92). It is a high-level guide, so expect principles rather than a specific design. The design is yours to get right.

How Do I Build a Hash-Chained Log Without a Vendor?

You can build one in a few lines of code, with one rule: a copy of the initial key goes to the verifier at genesis, and the writer only ever holds the current evolved key. The sketch below shows the append path. It's a minimal illustration, not code from any product.

import hmac, hashlib, json, time

def append_event(path, key, event, prev_hash):
    body = {"ts": time.time(), "event": event, "prev": prev_hash}
    raw = json.dumps(body, sort_keys=True).encode()
    h = hashlib.sha256(prev_hash.encode() + raw).hexdigest()
    tag = hmac.new(key, h.encode(), hashlib.sha256).hexdigest()
    with open(path, "a") as f:
        f.write(json.dumps({**body, "hash": h, "tag": tag}) + "\n")
    key = hashlib.sha256(b"evolve" + key).digest()  # caller must overwrite any stored copy of the old key
    return h, key  # store the head somewhere the agent cannot write
Enter fullscreen mode Exit fullscreen mode

The verifier replays the file from the genesis hash, recomputes each hash, checks each tag, and reports the first broken link. Because each key is derived from the previous one through a one-way hash and then discarded, a compromised writer cannot recover the keys that protected earlier entries, so it cannot forge them. The verifier keeps the initial key and re-derives every later key to check each tag. Run it on a schedule from a process that has read-only access to the file and access to the key. Then copy the latest head hash to an external system, such as a write-once bucket, a separate machine, or a timestamped message. That way, truncation back past the last anchored head is detectable. Anchor often, because entries written after the latest anchor can still be removed without detection.

Storage option Agent can rewrite it? Detects later edits? Fit for compliance review
Plain application log file Yes No Debugging only
Same database as agent data Yes, via same credentials No Not defensible
Hash chain, evolving key on same host Only going forward, if the host is compromised Earlier entries yes; truncation only with external head Weak
Hash chain, key and head external Not without key and head Yes Defensible starting point
Vendor log service with retention lock Not during lock Depends on vendor attestation Good, if you can export and verify

What Should Each Event Record?

Each event should carry enough to rebuild the decision and the effect it produced:

  • Timestamp
  • Agent or run ID
  • Model and prompt version
  • Tool name
  • Argument hash
  • Policy decision and policy version
  • Human approver identity and the outcome: approve, skip or timeout
  • The external effect's confirmation: the platform's response ID or status, not just "attempted"

The last two fields map onto failures already described above. The Skip button did nothing for months, so every skip was recorded as a timeout; an approver field that captures the skip outcome explicitly would have exposed that mismatch. The comments aimed at deleted accounts logged success, so an effect field holding the platform's response would have shown what was actually confirmed. A stable action ID in each event lets a reviewer tell a retried action from a duplicate one, the failure covered in AI Agent Repeats Actions After a Kubernetes Pod Restart.

Isn't a Hash Chain Overkill for a Side Project?

The objection is fair, and it has a reasonable core. Most agents aren't high-risk systems, and a hash chain adds moving parts that can fail. Still, the chain costs a few dozen lines and one scheduled verifier. An unverifiable log costs you nothing until the worst possible moment, which is during a dispute.

The stronger objection is "our vendor's observability platform is already immutable." That may be true, and it's worth checking. Ask three questions. Can an agent credential delete or edit traces? Can the same account shorten the retention setting? Can you export and verify the data yourself? If the answer to any of those is yes, the vendor's immutability protects the vendor's database, not your evidence.

Tamper evidence isn't paranoia about the agent. The agent is one of the parties whose behavior is in question, so it can't be the party that certifies the record.

Where Should the Log Live, and Who Can Clear It?

The log should live where the agent can't write. Any reset of its safety state should require a human action the agent cannot perform on itself. This is the lockout lesson, applied to records.

In July 2026, X suspended the account on 2026-07-21. The lockout check fired and returned True, but the caller skipped one action and the loop kept running for 3.5 more days.

For the bot's lockout, the fix was a process exit plus a sentinel only a human can clear, because launchd KeepAlive respawned a plain exit. The audit equivalent is that the verifier's alarm must stop the writer, not just notify someone. Writing "chain broken" into the same log is useless, since a broken chain is exactly the thing that log cannot report.

The second failure arrived in September 2026. X suspended the account again on 2026-09-22, a week after the reply cap rose from 16 to 150. This time the lockout check matched none of its known selectors and never fired. I stopped the bot by hand. A detector that depends on a fixed string from an external page is a log of the last thing you expected, not a control. The same goes for audit events: record the action taken, not only the belief that it was allowed. If the authorization decision comes from a policy engine, log the decision and the policy version alongside the action. For a deeper treatment of blocking rather than logging out-of-scope calls, see Detect AI Agent Actions Outside Task Scope: Block, Don't Log. For why a stop signal must not rely on cached state, see AI Agent Kill Switch Not Working: Cached Flags and Leases.

Finally, decide retention before the incident. Article 19 sets six months as a floor for providers, and a dispute can arrive long after that. Keep the chain for at least as long as any applicable law or contract requires, and treat the personal data inside it under GDPR.

A Checklist Before Your Next Compliance Review

Stop asking whether your agent's logs are good enough. Ask who could change them without anyone noticing. If the answer includes the agent, the database it writes to, or a single key kept next to the process, the log is a diary, and a reviewer will read it as one. Build the chain this week. Move the key and the head somewhere the agent can't reach. Schedule a verifier that stops the writer when the chain breaks. Then run it once against a log you've deliberately corrupted, so you know the alarm fires before you need it.

Top comments (0)