AI from scratch · MLOps · Stage 7: production

MLOps

A model in production is never finished. The world it learned from keeps changing: new stations open, a holiday changes who rides when, a new kind of outage appears. Meanwhile the model keeps answering, confidently, with no idea anything moved. MLOps is the set of habits that notice and respond: watch the service, its inputs, its outputs and, when the truth arrives, its accuracy; ship new versions without betting the product on them; and keep the whole path from data to deployed model repeatable. The examples use this site's archive of NYC subway elevator and escalator outages.

1. The loop

data→validate→train→evaluate→register→deploy safely→monitor→ back to data

Every stage of this path appears in it: the toolkit's pipelines and experiment tracking, chapter 11 and 12's honest evaluation, the serving lesson's API. What production adds is that the loop runs again and again, mostly by itself, and that its last step, monitoring, decides when it runs.

Production ML is a loop, from data through a safe deployment to monitoring, and monitoring decides when to go round again.

2. What to watch

LayerWatchAvailable
ServiceLatency percentiles, errors, timeouts (the serving lesson)Immediately
InputsEach feature's distribution compared with training: data drift; missing values; new categoriesImmediately
OutputsThe distribution of predictions: share flagged, average probabilityImmediately
OutcomesAccuracy, AUC, calibration against the truthLate: an outage's length is known only when it ends; fraud, weeks later

The truth usually arrives late or never, so the cheap, immediate layers (inputs and outputs) are early warnings for the expensive one. A change in the relationship itself, the same inputs now meaning a different outcome, is concept drift, and only outcomes reveal it.

Watch four layers: the service and its inputs and outputs now, and its accuracy when the truth arrives.

3. Measuring drift

The population stability index compares a feature's distribution in a reference window (the training data) with a live window, bin by bin:

\[ \text{PSI} = \sum_{\text{bins}} (p_{\text{live}} - p_{\text{ref}}) \ln \frac{p_{\text{live}}}{p_{\text{ref}}} \]

Rules of thumb: below 0.1 stable, 0.1 to 0.25 worth a look, above 0.25 a real shift. Compare windows of the outage archive:

reference window live window

Two things stand out. Reasons drift between weekdays and weekends (PSI 0.32): planned work is 15% of weekday outages and 5% at weekends. That's the city's weekly rhythm, not a problem, and a monitor that compares Monday with Saturday will page someone every week; compare like with like (this weekend against last weekend). And between the first and second half of the week reasons drift too (0.22), yet a model trained on the first half scores better on the second (AUC 0.90 against 0.86 on its own training days). Drift is a reason to look, not proof of damage; outcomes decide.

Your fraud model's input drift alarm fires every year in late November. Accuracy, checked weeks later, was fine every time. What should change?

Seasonality is drift that's expected. Alarms that fire on it train people to ignore alarms. Choose the reference to include the normal cycles.

Drift scores like PSI compare live inputs with a reference; choose the reference so normal cycles don't alarm, and let outcomes decide whether drift hurt.

Why PSI is a two-way KL divergence
  1. Probability 5's KL divergence measures the extra surprise of using \(q\) when the truth is \(p\): \(\operatorname{KL}(p \Vert q) = \sum p \ln (p/q)\). It isn't symmetric.
  2. Add both directions: \(\operatorname{KL}(p \Vert q) + \operatorname{KL}(q \Vert p) = \sum p \ln (p/q) + \sum q \ln (q/p)\).
  3. The second sum is \(-\sum q \ln (p/q)\), so together: \(\sum (p - q) \ln (p/q)\). That's PSI, with \(p\) the live share and \(q\) the reference share.
  4. So PSI is the symmetric KL between the two windows: zero only when they match, never negative, and measured in the same "nats of surprise" as cross-entropy.
  5. It also explains PSI's weak spot: a bin that's empty in one window has \(\ln (p/0)\) = infinity, so implementations add a small floor to every share, and the score then depends on that choice (below).

4. Shipping a new model safely

A retrained model that wins offline can still lose in production (ML strategy's mismatch, the experiments lesson's lucky seed). So new versions climb a ladder, each rung exposing more traffic only if the last one held:

RungWhat happensRisk to users
Offline evaluationThe champion (current model) and the challenger both scored on the newest held-out data, paired (P4)None
ShadowThe challenger sees real requests and its answers are logged, but users get the champion'sNone (costs compute)
Canary1–5% of traffic gets the challenger; errors, latency and output distribution watchedA few users
A/B testA planned split with a business metric, sample size and guardrails (Probability 4)Half, briefly
Promote, keep rollbackThe challenger becomes champion; the old version stays one command away–

New models earn traffic step by step: offline, shadow, canary, A/B; and the previous version stays ready for a one-command rollback.

5. Pipelines that check themselves

A nightly retraining job's newest model scores AUC 0.61 on recent data; the champion scored 0.89 last week. What should the pipeline do?

A drop that big usually means broken data (a changed feed, a join gone wrong), not a changed world. Gates turn that into an alert instead of an outage. Then the data validation logs say where to look.

Automated pipelines validate data, gate models on evaluation, and register everything they produce, so a bad batch becomes an alert, not a deployed model.

Where it breaks: when the model changes its own data

6. What to carry forward

All lessons: AI from scratch. Google's "Rules of Machine Learning" and Chip Huyen's "Designing Machine Learning Systems" go much further.

Last updated