PeaBox Lab › PeaLang › Learn › Chapter 20 › Lesson 3

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:

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.

FieldPurpose
prediction_id, correlation_idUnique identity and link to the request trace
timestamp (UTC)When the decision was made
model name, version, manifest hashWhich artifact decided (Lesson 1)
routeChampion, canary, or shadow (Lesson 2)
input reference or minimized featuresWhat the model saw, minimized and protected
schema resultAccepted, or rejected with reasons
score and calibrated probabilityRaw output
decision and thresholdAction taken and the policy that produced it
latencyOperational 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

KindContentsConsumersRetention
Audit recordsOne record per decisionAuditors, investigators, accuracy monitoringLong; set by policy
Operational logsEvents, errors, warningsOn-call engineersShort to medium
MetricsAggregated counts, rates, latenciesDashboards, alertingMedium, 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

Guided practice

Hands-on lab: An audit trail and monitoring store

Review questions

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.

← Previous Next →