Part IV — Expert Production Engineering · Chapter 20 · Lesson 3 of 7 · 5 min read
Lesson 3: Prediction Audits, Monitoring Stores, and Structured Logs
Lesson level: Expert
Purpose: Record every prediction with enough context to explain, reproduce, and evaluate it later, store the records so that monitoring can compute health and accuracy, and write logs that are machine-readable and free of sensitive data.
Learning objectives
By the end of this lesson you will be able to:
- design a prediction audit record
- separate audit records, operational logs, and metrics
- join delayed outcomes back to predictions
- design a monitoring store with retention and access rules
- write structured, correlated, redacted logs
- sample and aggregate high-volume traffic responsibly
3.1 The prediction audit record
An audit record is evidence for one decision. Design a versioned contract covering successful predictions and rejected requests. The shipped PipelineRuntime records successful predictions, while authentication and request-limit failures occur before that audit call. Add host-level rejection auditing and correlate it with prediction records; do not assume complete coverage without testing each failure path.
| Field | Purpose |
|---|---|
| prediction_id, correlation_id | Unique identity and link to the request trace |
| timestamp (UTC) | When the decision was made |
| model name, version, manifest hash | Which artifact decided (Lesson 1) |
| route | Champion, canary, or shadow (Lesson 2) |
| input reference or minimized features | What the model saw, minimized and protected |
| schema result | Accepted, or rejected with reasons |
| score and calibrated probability | Raw output |
| decision and threshold | Action taken and the policy that produced it |
| latency | Operational health |
| caller identity (pseudonymized) | Accountability without exposing personal data |
3.2 Minimize and protect inputs
Audit records tend to accumulate personal data. Store only what is needed to explain and reproduce the decision. Options, in order of preference: a reference to the source record that is already governed elsewhere; the model-ready feature vector without direct identifiers; pseudonymized identifiers with the mapping held separately. Encrypt audit storage at rest (Lesson 6), restrict access to named roles, and apply your organization's retention and deletion rules — including deletion requests where the law requires them.
3.3 Three kinds of telemetry
| Kind | Contents | Consumers | Retention |
|---|---|---|---|
| Audit records | One record per decision | Auditors, investigators, accuracy monitoring | Long; set by policy |
| Operational logs | Events, errors, warnings | On-call engineers | Short to medium |
| Metrics | Aggregated counts, rates, latencies | Dashboards, alerting | Medium, downsampled over time |
Keep them separate. Audit records must never be dropped for cost reasons; logs and metrics can be sampled and downsampled. Mixing them leads either to losing audit evidence or to storing debug noise for years.
3.4 Joining outcomes
Accuracy can only be measured once the true outcome is known: whether the customer actually churned, whether the claim was actually fraudulent. Outcomes arrive late, sometimes weeks later, and from different systems. Store them in an outcomes table keyed by the same business identifier and prediction time, then join:
Illustrative listing.
from pealang.data import scan
preds = scan("monitoring/predictions/*.jsonl")
outcomes = scan("monitoring/outcomes/*.jsonl")
labelled = (preds
.join(outcomes, on="customer_id", how="left")
.filter((col("outcome_time") > col("timestamp")) &
(col("outcome_time") <= col("timestamp") + days(90)))
.collect())
The time condition matters: only outcomes observed after the prediction and within the target's definition window count. Report the share of predictions that have a matched outcome; accuracy computed on a small, biased subset of labelled cases can mislead.
3.5 Structured logs
Write logs as one JSON object per line with consistent field names — timestamp, level, event, correlation_id, model, version, and event-specific fields. Structured logs can be searched, aggregated, and alerted on reliably; free-text logs cannot. Never log secrets, tokens, raw personal data, or whole requests; log identifiers and sizes instead.
{"ts":"2026-11-02T03:14:07Z","level":"WARN","event":"schema_reject",
"correlation_id":"c-9f12","model":"churn","version":"1.1.0",
"route":"canary","field":"age","reason":"out_of_range"}
3.6 The monitoring store
The monitoring store holds audit records, outcomes, and daily aggregates in a form that analytics can query efficiently — partitioned by date and model version, in an append-only format such as JSONL files or a database table (the SQLite reference backend from Chapter 17 is enough for learning; production needs a certified backend). Precompute daily aggregates per model version and segment: request counts, rejection counts by reason, score histograms, decision rates, latency percentiles, and, once outcomes arrive, accuracy metrics. Lesson 4 compares these aggregates against reference windows to detect drift.
3.7 Volume, sampling, and cost
High-traffic services can produce millions of predictions a day. Audit records for regulated decisions must be complete. For low-stakes, high-volume predictions, an explicit policy may permit sampling audit detail while keeping complete counts and aggregates — but decide this with the governance owner, document it in the model card, and never sample in a way that correlates with outcome or segment.
Common mistakes
- Logging whole requests: personal data and secrets end up in log stores; minimize and redact.
- No model version on predictions: incidents cannot be attributed.
- Mixing audit and debug logs: one gets sampled, the other kept forever.
- Joining outcomes without a time window: outcomes from before the prediction leak in.
- Accuracy from a biased labelled subset: report label coverage.
- Free-text logs: unusable for automated alerting.
Guided practice
- Write the audit record schema for the churn model and mark each field as required, optional, or protected.
- Generate 10,000 synthetic predictions and 3,000 delayed outcomes; join them with a correct time window and report label coverage.
- Rewrite three free-text log messages as structured events.
Hands-on lab: An audit trail and monitoring store
- Add audit-record writing to the churn scoring function, including rejected requests.
- Write structured logs with correlation IDs and a redaction filter; test that a fake secret never appears in output.
- Build daily aggregates per version and segment in a local monitoring store.
- Join outcomes and compute rolling weekly precision, recall, and calibration for each version.
- Answer an audit question from the store alone: "Show every decision about customer X last month and which model made it."
Review questions
- Which fields make a prediction traceable?
- How should inputs be minimized in audit records?
- Why keep audit records, logs, and metrics separate?
- Why does outcome joining need a time window?
- What is label coverage, and why report it?
- When, if ever, may audit detail be sampled?
Applied challenge
Design the audit and monitoring architecture for a loan-approval model subject to a seven-year record-keeping requirement and individual deletion requests. Show how both requirements are satisfied simultaneously, including how pseudonymization, encryption, and retention interact.
Lesson summary
Every prediction produces an audit record tying it to a model version, inputs, decision, and policy. Audit records, logs, and metrics are kept separately with different retention. Outcomes are joined with correct time windows and label coverage is reported. Structured, redacted logs and a partitioned monitoring store make health, accuracy, and investigations answerable from data.
Lesson status: Ready for review.