Sub topic 2
Designing Audit Trails for ML Systems
Assume you understand the basic ML lifecycle, Git, CI/CD, and Python. This chapter focuses on the evidence your system produces while data, code, and models move through that lifecycle.
An audit trail is a chronological record of actions and decisions. In ML, ordinary software logs are not enough. A model's behavior depends on training data, feature transformations, configuration, model artifacts, and human decisions. An audit trail connects these parts so that an investigator can answer:
- Who performed the action?
- What happened, and when?
- Which model version and data were involved?
- Which code and environment produced the result?
- What decision or approval followed?
Design an audit event
Treat each important action as an event. Examples include starting a training run, approving a model, deploying a version, changing a feature definition, and overriding a model recommendation.
A useful event contains stable identifiers rather than only free text.
| Field | Purpose |
|---|---|
| `event_id` | Uniquely identifies the event |
| `ts` | Records the UTC time |
| `actor` | Identifies a person or service |
| `action` | States what happened |
| `model` and `version` | Identify the model artifact |
| `data_ref` and `code_commit` | Link to input and source lineage |
| `outcome` | Records success, failure, or decision |
| `request_id` | Connects related events across services |
Do not put passwords, access tokens, or unnecessary personal data into events. Store large reports and artifacts in controlled storage, then record their URI and checksum in the event. Use synchronized UTC timestamps and retain the original event instead of silently editing it.
Make the trail tamper-evident
A write-once store with restricted access helps prevent deletion or modification. A hash chain adds evidence of later changes. Each event stores a cryptographic hash of the previous event. If an earlier event changes, verification fails.
import hashlib
import json
GENESIS = "0" * 64
def digest(body):
text = json.dumps(body, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(text.encode()).hexdigest()
def append(log, actor, action, model, version, details):
body = {
"actor": actor,
"action": action,
"model": model,
"version": version,
"details": details,
"prev_hash": log[-1]["hash"] if log else GENESIS,
}
event = {**body, "hash": digest(body)}
log.append(event)
return event
def verify(log):
previous = GENESIS
for event in log:
body = {k: v for k, v in event.items() if k != "hash"}
if body["prev_hash"] != previous or digest(body) != event["hash"]:
return False
previous = event["hash"]
return True
log = []
append(log, "training-service", "trained", "fraud-model", "3.1", {"data_ref": "ds-42"})
append(log, "release-bot", "deployed", "fraud-model", "3.1", {"environment": "prod"})
print(verify(log)) # True
log[0]["action"] = "deleted"
print(verify(log)) # FalseOperate the trail
Define retention by risk, contract, and applicable law. Restrict who can write, read, and administer the store. Monitor failed writes and verification failures. Test restoration and verification regularly. An audit trail is useful only when its records are complete, available, and understandable.
Exercise
A deployment event records only the model name and deployment time. What two changes would make it more useful for investigating a faulty release?
Show answer
Add the exact model version and links to the training data and code commit. Also record the actor or service, deployment environment, outcome, and request identifier.