Sub topic 13
Monitoring Models in Production
What monitoring covers
Assume you know how to train and evaluate a supervised model, use basic Git and command-line tools, and deploy a web service. This chapter focuses on what happens after deployment.
A production model is part of a larger system. Monitoring collects measurements from that system so you can detect failures, degradation, and unexpected use. A model can return valid predictions while still causing harm. For example, its input data may change, its response time may become too slow, or its predictions may become less useful.
Monitor four related areas:
| Area | Example measurement | What it can reveal |
|---|---|---|
| Service health | Error rate and latency | Broken dependencies or slow requests |
| Input data | Missing values and value ranges | Invalid or changed inputs |
| Model behavior | Prediction distribution | A changed usage pattern |
| Outcome quality | Accuracy after labels arrive | Model degradation |
Add monitoring at the prediction boundary
Record a small, privacy-safe event for each request. Useful fields include a model version, timestamp, request status, latency, selected input-quality checks, and the prediction. Do not log raw sensitive values unless you have a clear security and retention policy.
The following example records basic counters in memory. A real service would export these values to a metrics system such as Prometheus or a cloud monitoring service.
import random
import time
class Monitor:
def __init__(self):
self.requests = 0
self.errors = 0
self.latencies_ms = []
self.predictions = {"approve": 0, "reject": 0}
def observe(self, latency_ms, prediction, error=False):
self.requests += 1
self.errors += int(error)
self.latencies_ms.append(latency_ms)
if not error:
self.predictions[prediction] += 1
def report(self):
if not self.latencies_ms:
return {}
ordered = sorted(self.latencies_ms)
p95_index = int(0.95 * (len(ordered) - 1))
return {
"requests": self.requests,
"error_rate": self.errors / self.requests,
"p95_latency_ms": ordered[p95_index],
"predictions": self.predictions,
}
monitor = Monitor()
for _ in range(100):
start = time.perf_counter()
prediction = random.choice(["approve", "reject"])
monitor.observe((time.perf_counter() - start) * 1000, prediction)
print(monitor.report())Set alert thresholds before an incident. For example, alert when the error rate exceeds 2%, the 95th-percentile latency exceeds 300 milliseconds, or a required input field is missing in more than 1% of requests. Thresholds should reflect service-level objectives and business risk.
Measure quality when labels arrive
Many predictions cannot be evaluated immediately. A fraud decision may receive its confirmed label weeks later. Store the prediction, model version, and later outcome under a protected identifier. Join them when the label arrives, then calculate quality by time period and important user groups.
A quality alert should trigger investigation, not automatic replacement. Check for label delays, data pipeline errors, changes in user behavior, and unfair effects before retraining or rolling back.
Exercise
Exercise
A model's accuracy falls from 0.92 to 0.84, but its latency and error rate are normal. What should you check first?
Show answer
Check label timing, input-data changes, the deployed model version, and performance across important subgroups before deciding whether to retrain.