Sub topic 4
Logging Predictions So Versions Can Be Compared
Assume you can serve a model behind an HTTP or RPC endpoint and understand basic metrics such as accuracy, precision, recall, and latency. This chapter focuses on the data you need to compare model versions after they receive the same request.
Why prediction logs matter
Offline test sets cannot represent every request in production. Production logs let you ask:
- Which model version handled each request?
- Did both versions receive the same input?
- How different were their predictions?
- What was the latency and error rate?
- What outcome eventually happened?
A prediction log is a structured record of one model invocation. It should contain enough information to reproduce or compare the prediction without storing unnecessary sensitive data.
For a direct comparison, log a pair of predictions. Both the stable model and the candidate model should use the same request and the same feature snapshot. A shared request_id connects the two records.
| Field | Purpose |
|---|---|
| `request_id` | Connects predictions made for one request |
| `model_version` | Identifies the exact model artifact |
| `input_digest` | Checks whether both models saw the same input |
| `prediction` | Stores the model output or a safe summary |
| `latency_ms` | Measures serving performance |
| `timestamp` | Supports time-based analysis |
| `label` | Stores the outcome when it becomes available |
Use a stable event schema
Log events as JSON Lines, with one JSON object per line. Include the model version, feature or input digest, prediction, and timing information. Avoid logging raw passwords, payment data, or other sensitive values. Use a digest or an approved feature reference instead.
import hashlib
import json
import time
from typing import Any
def digest_input(features: dict[str, Any]) -> str:
payload = json.dumps(features, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(payload.encode()).hexdigest()
def log_prediction(
request_id: str,
model_version: str,
features: dict[str, Any],
prediction: float,
latency_ms: float,
) -> None:
event = {
"event": "prediction",
"request_id": request_id,
"model_version": model_version,
"input_digest": digest_input(features),
"prediction": prediction,
"latency_ms": round(latency_ms, 2),
"timestamp": time.time(),
}
print(json.dumps(event))
features = {"amount": 125.0, "country": "GB"}
log_prediction("req-42", "stable-2026-11-01", features, 0.08, 14.7)
log_prediction("req-42", "candidate-2026-11-28", features, 0.13, 16.2)The two events have the same request_id and input_digest, so an analysis job can join them. The different model_version values identify the versions being compared.
Log enough context to replay safely
Request replay means running a recorded request through a model again. It helps investigate disagreements, errors, and regressions. A replayable record usually needs:
- A versioned feature snapshot or a reference to one
- The model version and configuration
- The preprocessing version
- The request timestamp and relevant region
- The prediction and response status
A feature reference is often safer than copying raw data into every log. It also prevents a later feature-store change from silently altering the replay input.
Compare paired predictions
A comparison job can group events by request_id, verify that the input digests match, and calculate disagreement rates. Also compare latency, missing events, and failures. A candidate that disagrees often may be useful, harmful, or simply differently calibrated. Disagreement is a signal for investigation, not a quality metric by itself.
Common errors
- Generating different features for the two model versions
- Omitting the exact model or preprocessing version
- Using a request ID that is not unique
- Logging raw sensitive input without approval
- Treating missing candidate logs as agreement
- Measuring only prediction quality and ignoring latency or errors
- Allowing logging failures to increase request latency or take down serving
Exercise
Two prediction records have the same request ID but different input digests. Can you compare their predictions directly?
Show answer
No. The models did not receive the same recorded input. Investigate feature generation or logging before comparing the outputs.