Learning path: Monitor and Improve a Model in Production · Step 4 of 8
A/B Tests for Models: Did the New Model Make Things Better for People?
Two ticket models both look fine. A simulated A/B test splits customers with a sticky hash, plans the size, reads the gap with its margin, and shows two ways to fool yourself: an A/A false winner and peeking every day.
What you will learn
- Explain what an A/B test answers that accuracy, shadow and canary can't
- Make the four decisions before day one: sticky random split, one outcome, planned size, fixed end
- Read a result as a gap with a margin, and know what that margin promises
- Spot two traps with simulations: an A/A false winner and the cost of peeking every day
Before you start
- Shadow, Canary and Rollback
- Basic NumPy
Our new ticket model passed shadow and canary (Shadow, Canary and Rollback). So no harm was seen. That's a different question from the one the business cares about: did it make things better for people?
Think about a bakery with a new loaf. The tasting panel likes it. That's good, but it's the panel, not the customers. So the baker tosses a coin for each loyalty card: heads gets the old loaf, tails gets the new one, and it stays that way every visit. Before starting, the baker picks one measure, did they come back this week?, writes down the end date, and doesn't touch the sign until then. Tasting isn't buying. Only people using it can tell you what it does for people.
Every outcome in this article is simulated: no real users, a fixed seed, and true rates we set ourselves. That lets us see how the test behaves, but it says nothing about the real models.
What accuracy can't tell you
Here's where our two models stand from the shadow run on 250 synthetic tickets:
v2 agrees with v1 : 0.856
v1 accuracy : 0.628
v2 accuracy : 0.644
A gap of four tickets, and the models differ on just 36. Now, does v2 make tickets get solved faster? Accuracy can't say: it compares a prediction with a label. Shadow can't say either, because nobody ever acted on a shadow answer. A canary asks is it safe to keep going? and is built to stop early.
An A/B test asks a different question: did this model cause a better outcome, and by how much?
Four decisions before day one
Write these down before you start:
- Who goes where. Random, and sticky: each customer always stays on one side.
- One outcome. Here: was the ticket solved within a day?
- How many people you need, which depends on the smallest gain worth having.
- When it ends. And you stick to that.
The same four work if you're comparing two prompts for a language model instead of two classic models.
Split and size in code
Here's the top of ab_test.py. Side A is v1 and solves 60% within a day; side B is v2 at 63%. In real life you wouldn't know these rates.
import numpy as np
from router import bucket
PER_DAY, DAYS = 600, 14
RATE_A, RATE_B = 0.60, 0.63 # simulated, set by us
ids = np.arange(1, PER_DAY * DAYS + 1)
side_b = np.array([bucket(i) < 50 for i in ids])
rnd = np.random.default_rng(19)
def planned(p, lift):
q = p + lift
spread = p * (1 - p) + q * (1 - q)
return int(np.ceil(2.8 ** 2 * spread / lift ** 2))
def outcomes(rate_a, rate_b):
rate = np.where(side_b, rate_b, rate_a)
return rnd.random(len(ids)) < rate
To split, we reuse bucket from router.py, the same hash we used for the canary, on the customer's ID. Under 50 goes to B, so the same customer always lands on the same side.
planned works out how many customers each side needs to spot a gain of three points most of the time: 4,122 per side. At 600 customers a day for 14 days, we end up with 4,172 on A and 4,228 on B, so both sides clear the plan.
Reading the result
read gives the gap between the two sides and a margin around that gap:
def read(solved, upto):
a = solved[:upto][~side_b[:upto]]
b = solved[:upto][side_b[:upto]]
pa, pb = a.mean(), b.mean()
var = pa * (1 - pa) / len(a)
var += pb * (1 - pb) / len(b)
margin = 1.96 * var ** 0.5
return len(a), len(b), pa, pb, pb - pa, margin
simulated: solved within a day, A 0.60, B 0.63
planned per side: 4122
look nA nB rateA rateB gap margin
day1 291 309 0.608 0.605 -0.003 +-0.078
day3 884 916 0.600 0.631 +0.031 +-0.045
day7 2093 2107 0.602 0.640 +0.038 +-0.029
day14 4172 4228 0.593 0.647 +0.054 +-0.021
same model on both sides (A/A):
day14 4172 4228 0.579 0.604 +0.025 +-0.021
read. The planned day clears zero. So does the A/A run, where nothing differs.- Day 1: the gap is about zero and the margin is ±0.078. That's nothing yet.
- Day 3: +0.031 with a margin of 0.045. The whisker still crosses zero.
- Day 14, the day we planned for: B is ahead by 0.054 ± 0.021. The whisker clears zero, and B wins.
But by how much? We set the true gain at 0.03, and this run landed higher. The size is rougher than the direction. And because we set those rates ourselves, this says nothing about v2 itself. It only shows you how to read a test.
Trap 1: same model, both sides
A good habit is to run the same model on both sides. People call that an A/A test. Both sides have the same true rate here, 60%, so the gap should be nothing.
But look at the A/A row: +0.025 with a margin of 0.021. That whisker clears zero, and there's nothing there to find. We didn't plan for it; that's just how this seed fell.
This is what the margin promises: it's wrong about one time in twenty, even when you do everything right. So one win is evidence, not proof. An A/A test also checks your split and your logging before you trust them.
Trap 2: the cost of peeking
You check the result every morning and stop the moment it looks good. To see what that costs, the script runs 2,000 tests with no difference at all, so any winner is false:
end, daily, runs = 0, 0, 2000
for _ in range(runs):
same = outcomes(RATE_A, RATE_A)
days = range(1, DAYS + 1)
looks = [read(same, PER_DAY * d) for d in days]
wins = [abs(r[4]) > r[5] for r in looks]
end += wins[-1]
daily += any(wins)
print(f"{runs} A/A tests, false winners:")
print("look once at the end :", round(end / runs, 3))
print("look every day :", round(daily / runs, 3))
2000 A/A tests, false winners:
look once at the end : 0.054
look every day : 0.221
Look once at the end, and 0.054 of the tests declare a winner: about one in twenty, as promised. Look every day and stop at the first win, and it's 0.221: more than one in five. Notice that our simulated B-vs-A run already cleared zero on day 7. Stopping there would have been peeking.
So keep the planned end date. Looking is fine. Deciding early isn't.
Common mistakes
- Using accuracy as the A/B outcome. The test exists to measure what people experience. Pick one outcome like "solved within a day".
- Non-sticky assignment. If a customer bounces between sides, neither side measures a clean effect.
- Reading the gap without the margin. +0.031 on day 3 looked like a win and wasn't one yet.
- Stopping at the first good morning. That turned 1 in 20 false winners into more than 1 in 5.
Recap
| Idea | In this simulation |
|---|---|
| The question | Did v2 cause more tickets solved within a day? |
| Four decisions | Sticky hash split, one outcome, 4,122 per side, 14 days |
| Read | Day 14: +0.054 ± 0.021, B wins; true gain was 0.03 |
| A/A trap | +0.025 ± 0.021 with nothing to find |
| Peeking trap | False winners 0.054 at the end vs 0.221 daily |
Try it yourself
In ab_test.py, set RATE_B to 0.61 and see whether 14 days can still find the gain.
Built this? Share your solution in the StudyEasy AI Community.
Join free on Skool →Shadow, Canary and Rollback: Releasing a New Model Without Betting Everything on It
Next →When Labels Arrive Weeks Late: What to Watch Before You Can Grade the Model
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.