Security → Auditability

LLM Audit Logging Explained: Audit Is Not Observability

What it is

Audit logging is distinct from observability (observability & tracing), though they share a source. Observability is for engineers debugging; audit is for accountability — proving after the fact what happened, to a regulator, an auditor, a customer, or a court.

The differences that matter:

ObservabilityAudit
PurposeDebug, optimiseProve what happened
SamplingSampled payloads acceptableComplete for auditable events
RetentionShort (days–weeks)Long (months–years), per regulation
MutabilityCan be pruned freelyAppend-only, tamper-evident
ContentTechnical detailActor, action, resource, decision, evidence, outcome

The audit record for an AI interaction:

{when, actor (user + tenant + roles + groups used),
 request (query, channel, purpose),
 authorisation (filter applied, permissions evaluated, denials),
 evidence (chunk ids + document ids + versions retrieved and shown),
 model (id, version, prompt version, parameters),
 output (answer, citations, confidence, abstained?),
 actions (tools called, arguments, approvals, results),
 governance (PII handling applied, guardrail decisions, region, retention class)}

The essential property for AI systems: the evidence chain. "The system said X" is not defensible; "the system said X based on document D version 3, page 12, retrieved under these permissions, using prompt v7 and model M" is.

How to implement

  1. Define auditable events: every user query and answer, every authorisation decision (especially denials), every tool action with side effects, every approval, every configuration or prompt change, every erasure and data-export operation.
  2. Append-only storage separate from application logs: immutable object storage with retention lock, or a database with insert-only permissions and hash chaining.
  3. Record the evidence chain: chunk IDs with document IDs and versions — so the exact text can be reproduced even after the document changes (document versioning).
  4. Record the authorisation context: the principals used in the filter (authentication & authorization), not just the user ID.
  5. Make it queryable: by actor, tenant, resource, time range, and action type — auditors ask "everything user X did with document D in Q3".
  6. Protect it: audit records contain sensitive content; apply access controls, encryption, and its own retention policy (data residency & retention).
  7. Test reconstruction: periodically pick a past interaction and prove you can reproduce exactly what was shown, from the audit record alone.

Small example

import hashlib, json
from datetime import datetime, timezone

class AuditLog:
    """Append-only with hash chaining — tamper-evident without a blockchain."""
    def __init__(self, store): self.store, self._last_hash = store, self._load_head()

    def record(self, event_type: str, actor: dict, resource: dict, decision: dict,
               evidence: dict | None = None, metadata: dict | None = None) -> str:
        entry = {
            "event_id": str(uuid.uuid4()),
            "ts": datetime.now(timezone.utc).isoformat(),
            "event_type": event_type,
            "actor": {"user_id": actor["id"], "tenant_id": actor["tenant_id"],
                      "roles": sorted(actor.get("roles", [])),
                      "principals_used": sorted(actor.get("principals", []))},   # what the ACL filter used
            "resource": resource,
            "decision": decision,
            "evidence": evidence or {},
            "metadata": metadata or {},
            "prev_hash": self._last_hash,
        }
        entry["hash"] = hashlib.sha256(
            json.dumps(entry, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
        self.store.append(entry)                      # immutable storage, retention-locked
        self._last_hash = entry["hash"]
        return entry["event_id"]

    def verify_chain(self, start=None, end=None) -> bool:
        prev = None
        for e in self.store.range(start, end):
            if prev is not None and e["prev_hash"] != prev:
                return False
            body = {k: v for k, v in e.items() if k != "hash"}
            if e["hash"] != hashlib.sha256(json.dumps(body, sort_keys=True, separators=(",", ":")).encode()).hexdigest():
                return False
            prev = e["hash"]
        return True

audit = AuditLog(immutable_store)

def audited_answer(query, caller, trace_id):
    principals = sorted(caller.principal_set())
    chunks = retrieve(query, caller)
    out = generate_grounded(query, chunks)

    audit.record(
        event_type="ai.query_answered",
        actor={"id": caller.id, "tenant_id": caller.tenant_id,
               "roles": caller.roles, "principals": principals},
        resource={"query": query[:2000], "channel": "web", "trace_id": trace_id},
        decision={"answered": not out.get("refused"), "abstained": out.get("refused", False),
                  "confidence": out.get("confidence"), "guardrails": out.get("guardrail_actions", [])},
        evidence={"retrieved": [{"chunk_id": c["chunk_id"], "doc_id": c["doc_id"],
                                 "version": c["version"], "page": c.get("page"),
                                 "score": round(c.get("rerank_score", 0), 4)} for c in chunks],
                  "cited": out.get("citations", []),
                  "answer_hash": hashlib.sha256((out.get("answer") or "").encode()).hexdigest(),
                  "answer": out.get("answer")},                       # full text where regulation requires it
        metadata={"model": MODEL_ID, "prompt_version": PROMPT_VERSION,
                  "index_version": INDEX_VERSION, "region": REGION,
                  "pii_action": POLICY.pii_action, "retention_class": "audit_7y"},
    )
    return out

def audited_action(caller, tool_name, args, approval, result, trace_id):
    audit.record("ai.action_executed",
                 actor={"id": caller.id, "tenant_id": caller.tenant_id, "roles": caller.roles},
                 resource={"tool": tool_name, "arguments": redact_sensitive(args), "trace_id": trace_id},
                 decision={"authorised_by": approval.get("approver"), "approval_id": approval.get("id"),
                           "shown_to_approver": approval.get("preview"),        # what they actually saw (human-in-the-loop)
                           "policy_checks": approval.get("checks", [])},
                 evidence={"result_status": result.get("status"), "external_id": result.get("id"),
                           "idempotency_key": result.get("idempotency_key")})

def audited_denial(caller, resource_id, reason):
    audit.record("ai.access_denied",
                 actor={"id": caller.id, "tenant_id": caller.tenant_id,
                        "principals": sorted(caller.principal_set())},
                 resource={"resource_id": resource_id},
                 decision={"denied": True, "reason": reason})           # denials matter as much as grants

def reconstruct(event_id: str) -> dict:
    """Prove what the user was shown, from the audit record alone."""
    e = audit.store.get(event_id)
    return {"when": e["ts"], "who": e["actor"],
            "asked": e["resource"]["query"],
            "shown": [chunk_store.get_version(c["chunk_id"], c["version"]) for c in e["evidence"]["retrieved"]],
            "answered": e["evidence"]["answer"],
            "under": e["metadata"]}

Pros

  • Defensible answers: the evidence chain proves what the system based its answer on.
  • Regulatory requirement satisfied in finance, healthcare, legal, and public sector.
  • Incident response: reconstructing exactly what a user saw is the first step in any investigation.
  • Dispute resolution: customer disagreements about an answer become a lookup, not an argument.
  • Denial records prove the access controls were working, not just that they existed.

Cons

  • Storage cost and volume for complete, long-retention records.
  • Audit logs contain sensitive content — they are themselves a high-value target and a residency/erasure concern (data residency & retention).
  • Tension with erasure: regulation may require retaining audit records that a subject asks to erase; this needs legal resolution (usually: audit records are retained under a legal basis, personal content within them is minimised).
  • Version pinning burden: reproducing what was shown requires retaining old document versions (document versioning).
  • Append-only storage complicates ordinary data lifecycle operations.

When to use

  • Always for actions with side effects and for authorisation decisions — these are cheap and universally useful.
  • Full evidence chains in regulated domains, high-stakes advice, and anywhere answers may be disputed.

When NOT to use (full content retention)

  • Low-stakes internal tools: record actor, action, evidence IDs, and outcome, but not full answer text — you keep accountability without the retention burden.

Production at scale

Audit is separate from observability, structurally. Different store, different retention, different access controls, different mutability. Deriving both from the same trace pipeline is fine; storing them together is not — traces get pruned, audit records must not.

The evidence chain needs versioned documents. Recording chunk IDs alone is insufficient once documents change; record doc_id + version + page and retain old versions for the audit period (document versioning). Otherwise "here's what it was based on" resolves to text that has since changed.

Record what the approver saw, not just their decision (human-in-the-loop). In a dispute, the question is whether the human had the information to decide well.

Denials are audit events. They prove the control was working and are the evidence for "no, that user could not have seen that document" (authentication & authorization).

Hash chaining is cheap tamper-evidence and satisfies most requirements without exotic infrastructure. Verify the chain periodically and alert on breaks.

Query patterns drive the schema: auditors ask by actor, by resource, by time window, by action type. Index for those; a write-only log nobody can query fails its purpose.

Resolve the erasure/audit tension explicitly with legal: typically, audit records are retained under a legitimate-interest or legal-obligation basis, with personal content minimised (store answer hashes plus evidence IDs rather than full text where possible), and the retention basis documented (data residency & retention).

Test reconstruction quarterly: pick a random past event and prove you can show exactly what the user saw. This exercise reliably surfaces gaps — missing versions, pruned chunks, absent prompt versions.

Access to audit logs is itself audited. Who read the audit log, when, and why.

Prod checklist

  • Auditable event list defined: queries, answers, authorisation decisions and denials, actions, approvals, config/prompt changes, erasures, exports
  • Append-only, tamper-evident store separate from application logs and traces
  • Evidence chain records chunk + document + version + page, with old versions retained for the audit period
  • Authorisation context recorded (principals used, filter applied)
  • Approver decisions recorded with what was shown
  • Queryable by actor, tenant, resource, time, action type
  • Audit store access-controlled, encrypted, region-scoped, and itself audited
  • Erasure/retention tension resolved with legal and documented
  • Hash-chain verification job with alerting
  • Quarterly reconstruction test

Metrics to watch

MetricWhy
Audit coverage (audited events / auditable events)Gaps in accountability
Hash-chain verification failures (must be 0)Tampering or corruption
Reconstruction test success rateWhether the chain actually works
Audit query latencyUsability for auditors
Denial events recordedEvidence the controls fire