Sub topic 12
Record-keeping and Automatic Logging in Practice
Assume you understand training, evaluation, deployment, monitoring, Git, CI/CD, and the basic purpose of a model registry. This chapter focuses on turning those practices into evidence that can be inspected later.
What must be recorded
For a high-risk AI system, the EU AI Act requires automatic recording of events during operation. The logs should support traceability: an investigator must be able to determine which system produced an output, under which conditions, and what happened afterward.
A useful event record includes:
- A timestamp in UTC, preferably from synchronized system clocks.
- The system, model, and deployed version.
- The request or transaction identifier.
- The actor or service that initiated the action.
- The action, such as
prediction,override,rollback, orshutdown. - References to the input and output, without unnecessarily copying sensitive data.
- Relevant configuration, policy, or feature-pipeline versions.
- The result, error, or intervention.
- A correlation identifier linking related events.
Do not treat a prediction log as a complete copy of every input. Store a secure reference or a minimized representation when the original data contains personal or confidential information.
| Record | Why it matters | Example |
|---|---|---|
| Model version | Identifies the exact artifact used | `credit-model:2.4.0` |
| Data reference | Connects output to input provenance | `request-81f2` |
| Human action | Shows oversight and intervention | `override: approved` |
| Timestamp | Establishes event order | `2026-11-30T10:15:13Z` |
Build logging into the serving path
Automatic logging is safer than asking operators to document events manually. Put the logger in the prediction service or a shared middleware layer so every request follows the same path.
import json
import logging
import time
import uuid
logging.basicConfig(level=logging.INFO, format="%(message)s")
def predict(model, features, model_version):
request_id = str(uuid.uuid4())
started = time.time()
try:
score = model(features)
event = {
"event": "prediction",
"request_id": request_id,
"model_version": model_version,
"status": "success",
"output_reference": "stored-separately",
"duration_ms": round((time.time() - started) * 1000, 2),
}
logging.info(json.dumps(event, sort_keys=True))
return score
except Exception as error:
event = {
"event": "prediction",
"request_id": request_id,
"model_version": model_version,
"status": "error",
"error_type": type(error).__name__,
}
logging.info(json.dumps(event, sort_keys=True))
raiseA production system should send these events to controlled, centralized storage. Restrict who can read or delete them, encrypt them, monitor access, and protect their integrity. Keep logs for the period required by the applicable law and risk assessment. For EU AI Act high-risk systems, the provider must generally retain automatically generated logs for at least six months unless another legal requirement sets a different period.
::: callout note Logging cannot repair missing lineage. Record references to the model artifact, code commit, data snapshot, and deployment configuration so an event can be reconstructed later. :::
Exercise
Why should a prediction log contain a model version and request identifier?
Show answer
The model version identifies the exact behavior that produced the result, while the request identifier links that result to its input, downstream actions, and any human intervention.