Learning path: Monitor and Improve a Model in Production · Step 6 of 8
Retraining Triggers: Schedule, Drift and Performance, Wired to One Retrain Pipeline
When should a model be retrained? A schedule, drift and a performance floor run side by side for twelve simulated churn weeks, each feeding the same retrain function and gate. Drift fires three times for no gain and misses the week the rule changes.
What you will learn
- Name the three retraining triggers and the question each one asks
- Run all three side by side over twelve simulated weeks and see each fire when the others stay quiet
- Wire a trigger to a retrain function that trains a candidate and runs a gate
- Explain why a trigger only requests a retrain and the gate decides what ships
Before you start
- Drift Detection in Code
- CI/CD for ML: A Quality Gate
When should a model get retrained? There are three common answers: on a schedule, when drift shows up, and when performance drops. Here we run all three side by side over twelve simulated weeks and wire them into one retrain pipeline, with a gate that decides if the new model ships.
Servicing a car
Think about when you take your car in for a service.
- By mileage. Every 10,000 km, whatever's going on. Simple, and sometimes a waste.
- By warning light. A sensor spots something odd and the light comes on. It can come on while the car drives perfectly well.
- By how it drives. It pulls to one side, the brakes feel soft. The most direct sign, but by then something's already wrong.
And the garage is a separate step. Any of the three gets the car in the door. None of them says the repair was any good.
Three triggers, three questions
Here are the same three for a churn model, on synthetic data:
| Trigger | Question | Car | Our rule |
|---|---|---|---|
| Schedule | Has it been long enough? | Mileage | Every 6 weeks |
| Drift | Do the inputs look different? | Warning light | PSI on spend above 0.2 |
| Performance | Are the answers getting worse? | How it drives | AUC below 0.70 (illustrative floor) |
Each one can fire while the others stay quiet. Let's see that happen. (PSI is explained in Drift Detection in Code; AUC in ROC and AUC.)
The triggers in code
triggers.py is a small simulation with fixed seeds. make_week builds one week of customers with tenure, spend and support calls:
import numpy as np
import pandas as pd
from checks import psi
from data import auc, fit
from gate import drift_policy, run_gates
EVERY = 6 # schedule: every 6 weeks
FLOOR = 0.70 # performance floor, illustrative
SPEND_UP = 5 # from week 5 customers spend more
RULE_CHANGE = 9 # from week 9 calls mean less churn
def make_week(week, n, seed):
rng = np.random.default_rng(seed)
tenure = rng.integers(1, 61, n)
mean = 85 if week >= SPEND_UP else 60
spend = rng.normal(mean, 20, n).clip(5, 200)
calls = rng.poisson(2, n)
c = -0.5 if week >= RULE_CHANGE else 0.5
z = (-0.04 * (tenure - 24) + 0.03 * (spend - 60)
+ c * (calls - 2) - 0.4)
y = rng.random(n) < 1 / (1 + np.exp(-z))
return pd.DataFrame({
"tenure": tenure, "spend_usd": spend,
"calls": calls, "feature_version": "v1",
"churn": y.astype(int)})
Two changes are planted:
- From week 5, customers spend more (mean 85 instead of 60). Same rule for who leaves, just different inputs.
- From week 9, the rule itself changes. Calls now mean less churn, not more, and the inputs look exactly the same.
The model is a scaled logistic regression (fit in data.py). Each week the loop checks all three triggers:
ref = make_week(0, 4000, seed=0)
current = fit(ref)
ref_spend = ref["spend_usd"]
history = []
print("week psi auc fired cand promote")
for week in range(1, 13):
history.append(make_week(week, 500, 100 + week))
test = make_week(week, 1000, 200 + week)
d = psi(ref_spend, test["spend_usd"])
a = auc(current, test)
fired = []
if week % EVERY == 0:
fired.append("schedule")
if drift_policy(d) == "train candidate":
fired.append("drift")
if a < FLOOR:
fired.append("performance")
cand_auc, promote = "-", "-"
if fired:
cand, window, r = retrain(history, current, test)
cand_auc = round(r["cand_auc"], 3)
promote = r["promote"]
if promote:
current = cand
ref_spend = window["spend_usd"]
print(week, round(d, 2), round(a, 3),
"+".join(fired) or "-", cand_auc, promote)
week % EVERY is the schedule. drift_policy is the drift check. a < FLOOR is performance. Whichever fire go into the fired list, and that list is all a trigger produces. It's a request, nothing more.
Wiring a trigger to the pipeline
What happens to that request? retrain is a small stand-in for a full training pipeline (A Training Pipeline as a DAG) and its quality gate (CI/CD for ML: A Quality Gate):
def retrain(history, current, test):
window = pd.concat(history[-3:])
cand = fit(window)
return cand, window, run_gates(
cand, current, window, test)
It takes a window, the last three weeks of labelled rows, and fits a candidate on it. run_gates in gate.py then compares candidate and current model on this week's test batch, so both are judged on the same fresh rows:
from checks import check_features, check_input
from data import FEATURE_VERSION, auc
MAX_DROP = 0.005
def run_gates(cand, current, train_df, holdout):
cand_auc = auc(cand, holdout)
cur_auc = auc(current, holdout)
short = holdout[holdout["tenure"] <= 12]
problems = check_input(train_df) + check_features(
train_df, FEATURE_VERSION)
s_new, s_cur = auc(cand, short), auc(current, short)
gates = {
"data valid": not problems,
"auc ok": cand_auc >= cur_auc - MAX_DROP,
"slice ok": s_new >= s_cur - 0.02,
}
return {"gates": gates, "cand_auc": cand_auc,
"cur_auc": cur_auc,
"promote": all(gates.values())}
def drift_policy(psi_value, limit=0.2):
"""Drift asks for a candidate, never promotes."""
if psi_value > limit:
return "train candidate"
return "wait"
Three gates: the training data is valid, overall AUC didn't drop by more than 0.005, and AUC on the short-tenure slice (tenure <= 12) didn't drop by more than 0.02 (see Slice Evaluation). If promote is true, the candidate becomes current and its window becomes the new drift reference. If not, the current model stays.
Twelve weeks, side by side
week psi auc fired cand promote
1 0.01 0.761 - - -
2 0.02 0.783 - - -
3 0.01 0.754 - - -
4 0.02 0.772 - - -
5 1.36 0.752 drift 0.753 True
6 0.56 0.747 schedule+drift 0.747 True
7 0.28 0.773 drift 0.773 True
8 0.01 0.757 - - -
9 0.03 0.569 performance 0.668 True
10 0.02 0.692 performance 0.76 True
11 0.01 0.735 - - -
12 0.01 0.717 schedule 0.742 True
Weeks 5 to 7: the light is on
Weeks 1-4 are quiet. In week 5 spend goes up: PSI jumps to 1.36 and drift fires. But the current model's AUC is 0.752. It's fine. The candidate gets 0.753: no real change, but it passes, so it's promoted.
In weeks 6 and 7 drift fires again, because the reference is still catching up. Same story: 0.747 against 0.747, then 0.773 against 0.773. Three retrains and basically nothing gained. The inputs moved, but the rule stayed the same. The warning light was on, and the car drove fine.
Week 9: drift stays quiet
Now the rule changes. PSI is 0.03. Drift says nothing at all, because the inputs didn't move. And AUC has dropped to 0.569, so performance fires. The candidate gets 0.668: better, but only a third of its window has the new rule in it.
In week 10 AUC is 0.692, still under the floor. It fires again, and now the candidate reaches 0.76.
In week 12 the schedule fires with nothing else on, and AUC goes from 0.717 to 0.742. So that blind trigger found something here, while in week 6 it found nothing.
One honest note: this simulation knows its labels in the same week. With late labels the performance trigger fires later, and that's its big weakness (When Labels Arrive Weeks Late).
The gate still decides
Six retrains, and all six got promoted. So does the gate do anything? Those windows were all clean data, so there was nothing to stop. Here's a window that isn't clean, from incident.py: 3,000 good rows plus 1,000 rows with spend recorded in cents. Excerpt from its output:
psi spend 8.28 -> train candidate
raw window gates {'data valid': False, 'auc ok': False, 'slice ok': False}
raw candidate auc 0.738
promote False
PSI on spend is 8.28, so drift asks for a candidate. The candidate scores 0.738 against the current model's 0.775. Every gate is false, so promote is false. A trigger only opens the door. The gate decides what walks through. (How to trace an incident like this one step by step is in Debugging a Model in Production.)
Common mistakes
- Letting a trigger promote.
drift_policyreturns "train candidate", never "deploy". Every request goes through the same gate. - Relying on drift alone. It fired three times for almost no gain and stayed silent when the rule changed.
- Relying on performance alone. It's the direct signal, but it needs labels, and labels are often late.
- Training the candidate on a window that hasn't caught up. In week 9 only a third of the window had the new rule, so the first fix was partial.
Recap
| Trigger | Strength | Weakness, seen here |
|---|---|---|
| Schedule | Simple safety net | Blind: wasted in week 6, useful in week 12 |
| Drift | Early, needs no labels | 3 retrains for no gain; silent at the rule change |
| Performance | Direct | Late, and needs labels |
| Gate | Decides what ships | Blocked the cents window: promote False |
Use more than one trigger, send them all into the same pipeline, and let the same gate decide.
Try it yourself
Set EVERY to 4 in triggers.py, run it again, and count how many retrains actually gained anything.
Built this? Share your solution in the StudyEasy AI Community.
Join free on Skool →When Labels Arrive Weeks Late: What to Watch Before You Can Grade the Model
Next →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.