Learning path: Monitor and Improve a Model in Production · Step 8 of 8
Debugging a Model in Production: A Four-Step Runbook, Cheap Checks First
Accuracy fell overnight. Instead of jumping to the model, run four checks from cheap to expensive and stop at the first red. Three simulated churn incidents end in three different actions: hold the batch, roll back, or train a candidate.
What you will learn
- Follow a fixed four-step runbook when a model's score drops: data, features, model version, the world
- Write the runbook as a function that stops at the first red check
- Match each incident to its action: hold the batch, roll back, or train a candidate
- Explain why retraining is the last thing to try, not the first
Before you start
- Data Validation and Schema Checks
- Shadow, Canary and Rollback
You come in one morning and your model's score fell overnight. Where do you start? Most people jump straight to the model. We're going to use a runbook instead: four checks in a fixed order, from cheap to expensive.
Think of a doctor. You walk in with a bad headache. A good doctor doesn't send you straight for a brain scan. First it's your temperature and blood pressure: a minute each, almost free. Then a blood test. Then: what medicine are you taking? Maybe you were given the wrong one. Each answer rules something out, and if one finds the cause, you stop right there. The scan is still there for when everything else comes back normal.
The alert
It's our churn model, on synthetic data (tenure, spend in dollars and support calls). On a normal day the AUC is 0.772. This morning, on a new batch of 1,000 rows, it's 0.506, and 0.5 is a coin flip. Nobody touched the model last night, as far as anyone knows. So what do you do?
The runbook
- The data. Did it arrive, and is it in range? Nulls, odd values. That takes seconds.
- The features. Are they built the way this model expects, with the right feature version?
- The model version. Is the model that's serving the one we meant to serve?
- The world. If all of that's fine, maybe reality changed. That's the slow, expensive one, so it goes last.
The cheap checks
The first three steps are a few lines each in checks.py:
import numpy as np
RANGES = {"tenure": (0, 120), "spend_usd": (0, 500),
"calls": (0, 50)}
def check_input(df):
problems = []
if df.isna().any().any():
problems.append("nulls found")
for col, (lo, hi) in RANGES.items():
bad = int(((df[col] < lo) | (df[col] > hi)).sum())
if bad:
problems.append(f"{col}: {bad} bad rows")
return problems
def check_features(df, expected):
seen = sorted(df["feature_version"].unique())
if seen != [expected]:
return [f"features {seen}, want {expected}"]
return []
def check_model(serving, registry):
if serving != registry:
return [f"serving {serving}, registry {registry}"]
return []
Each one returns a list of problems; an empty list means green. Range checks like these are covered in more depth in Data Validation and Schema Checks.
Running the checks on this batch
incident.py calls the checks in order and prints each answer. Excerpt from its output:
v1 holdout auc 0.775
incident batch auc 0.506
1 input ['spend_usd: 997 bad rows']
2 features ["features ['v2'], want v1"]
3 model ok
mitigate: hold the batch, keep serving v1
- Input: spend has 997 bad rows out of 1,000.
- Features: we got
v2and we wantv1. Red as well. - Model: ok. In this small project the serving and registry names are passed in by hand. In your system you'd read them from the server and the registry.
(The 0.775 is v1 on a separate 2,000-row holdout; the "normal day" 0.772 below is v1 on a clean 1,000-row batch.) Average spend used to be about $60 and in this batch it's about $5,870. Spend is arriving in cents. So we hold the batch and keep serving version 1. We didn't need a single training run to find that.
The runbook in code
Here's the same order as a function, in runbook.py. The first red check stops it:
def runbook(batch, serving, registry):
if check_input(batch):
return "1 data"
if check_features(batch, FEATURE_VERSION):
return "2 features"
if check_model(serving, registry):
return "3 model version"
return "4 the world"
On the cents batch it stops at step 1. Now two more incidents, simulated this time. The whole script prints:
normal day auc 0.772
A spend in cents auc 0.506
stops at 1 data
action: hold the batch, keep v1
B wrong model out auc 0.325
stops at 3 model version
action: roll back to v1, auc 0.772
C world changed auc 0.553
stops at 4 the world
psi spend 0.01
action: v1 is already out, so no rollback,
train a candidate on fresh data, then gates
Incident B: the wrong model went out
Somebody shipped the wrong model. We built it from shuffled labels, so it learned nothing at all:
mixed = ref.copy()
rng = np.random.default_rng(6)
mixed["churn"] = rng.permutation(
mixed["churn"].values)
wrong = fit(mixed)
print("B wrong model out auc",
round(auc(wrong, clean), 3))
print(" stops at", runbook(clean, "v2", "v1"))
On a clean batch its AUC is 0.325, well below a coin flip. Don't read much into the exact number; it just means broken. The runbook goes: data fine, features fine, then model version. Serving says v2, the registry says v1. It stops at step 3.
This is the one incident where the fix is a rollback (Shadow, Canary and Rollback shows a tested one). Back on version 1, AUC is 0.772, the normal number, because it's the same model on the same batch. Roll back first, investigate after.
Incident C: the world changed
This is the hard one. AUC is 0.553. The runbook goes through: data fine, features fine, model version fine. It falls all the way to step 4.
def rule_changed(n, seed):
# Same inputs, but calls now mean less churn
df = make_batch(n, seed)
rng = np.random.default_rng(seed + 50)
z = (-0.04 * (df["tenure"] - 24)
+ 0.03 * (df["spend_usd"] - 60)
- 0.5 * (df["calls"] - 2) - 0.4)
p = 1 / (1 + np.exp(-z))
df["churn"] = (rng.random(n) < p).astype(int)
return df
Drift doesn't help here: PSI on spend is 0.01. In our simulation the inputs look the same, and the rule behind them changed. Calls used to mean more churn, and now they mean less.
A rollback won't help, because version 1 is already the one out there. This is where you go and ask people. Did the pricing change? Is there a new competitor? Did the business change something? And only then do you retrain: train a candidate on fresh data, and it still goes through the gates (Retraining Triggers).
Common mistakes
- Starting with the model. The cheap check found the cents problem in seconds. In the same project, a candidate trained on a window with cents rows scored 0.738 against 0.775 and failed every gate.
- Retraining when a rollback would do. If the wrong version is serving, put the right one back first.
- Trusting drift to explain every drop. In incident C, PSI was 0.01 while AUC fell to 0.553.
- Skipping the "ask people" step. When every check is green, the data alone may not tell you why the rule changed.
Recap
| Incident | AUC | Stops at | Action |
|---|---|---|---|
| Normal day | 0.772 | - | - |
| A: spend in cents | 0.506 | 1 data | Hold the batch, keep v1 |
| B: wrong model out | 0.325 | 3 model version | Roll back to v1 (0.772) |
| C: world changed | 0.553 | 4 the world | Ask why, train a candidate, gates |
Retraining was the right answer only once, and it was the last thing we tried.
Try it yourself
In runbook.py, pass "v1", "v1" for incident B, as if your version names were wrong, and see where the runbook stops.
Built this? Share your solution in the StudyEasy AI Community.
Join free on Skool →The User Feedback Loop: Why Thumbs Up Isn't the Truth, and What a Small Random Review Adds
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (opens in a new tab)Related articles
Python Virtual Environments: One Project, One Toolbox
Why every Python project gets its own virtual environment: two projects that need different versions of requests, python -m venv .venv, activation, pip install and the PyCharm setting that activates it for you.
Git and GitHub Fundamentals: Commits, Branches, Push and Pull
The Git you use every day, on a tiny hello-ml project: commits as save points, the staging area, a branch merged with a fast-forward, then push, clone, pull and pull requests with a teammate.
Testing a FastAPI App with pytest: Three Small Tests
A tiny FastAPI app with a Hello World route and a name route that refuses numbers, three pytest tests that check it, and what happens when the validation is deleted by mistake.
Config and Logging Instead of Hard-Coded Values in ML Training
Take the hard-coded values out of a car price training script: one config.toml read with tomllib, logging with levels and a run id on every line, and secrets read from the environment. Same RMSE, 2195, before and after.