Evaluation & Testing at Scale
After this lesson, you will be able to:
- Design a layered evaluation plan that combines automated metrics, AI-based scoring, human review, and behavioral tests
- Run A/B tests for ML models the right way, with proper statistics and without common mistakes like peeking at results too early
- Build a red teaming process that systematically finds model failures before your users do
Before you start
#Why Accuracy Is Not Enough
Evaluation might not sound as exciting as training a model, but it is the skill that separates models that work in demos from models that work in the real world. Master this, and you will never ship a model that silently embarrasses you.
#The Evaluation Pyramid
Evaluation Pyramid
Each layer catches different types of failures. Skip a layer and you have blind spots.
Try it! Pick any classifier you have (even a simpleif "positive" in textrule). Write 5 normal test cases and 5 tricky edge cases (negation, sarcasm, ambiguity). Run them through and count how many pass. You will be surprised how quickly "95% accurate" models fail on adversarial inputs.
#Layer 1: Automated Metrics
Fast, cheap, run on every training iteration:
#Layer 2: Behavioral Testing (CheckList / Unit Tests for Models)
Behavioral tests define specific input-output expectations, like unit tests for models:
Invariance Test: Paraphrases Should Match
| Input | Expected Output | Constraint |
|---|---|---|
| What is the capital of France? | Paris | Baseline |
| Tell me the capital city of France | Paris | Must match |
| France's capital is what? | Paris | Must match |
Directional Test: Negation Should Flip Sentiment
| Input | Expected Output | Constraint |
|---|---|---|
| Rate this review: Great product! | Positive | Baseline |
| Rate this review: Terrible product! | Negative | Must flip |
Minimum Functionality Test: Basic Cases
| Input | Expected Output |
|---|---|
| 2 + 2 | 4 |
| What color is the sky? | blue (or similar) |
Tests: Run the full behavioral test suite. Identify which test types have the highest failure rate. The robustness tests should reveal model fragility.
#Layer 3: LLM-as-Judge
Use a powerful LLM (GPT-4, Claude) to evaluate outputs from the model being tested:
Where it fails
- LLMs have positional bias (prefer the first response in A/B comparisons)
- LLMs are verbose, they may prefer longer responses regardless of quality
- LLMs may not catch domain-specific factual errors
- Self-evaluation (using the same model as judge and generator) has obvious bias
Best practices
- Randomize position in pairwise comparisons (swap A and B, average scores)
- Use a stronger model as judge than the model being evaluated
- Calibrate against human annotations on a subset
- Use structured rubrics, not open-ended "is this good?"
You use GPT-4 as a judge to evaluate Claude's outputs. GPT-4 rates Claude's responses an average of 3.2 out of 5. Is this a reliable evaluation?
#Layer 4: A/B Testing
The gold standard for measuring business impact: expose real users to two model versions and measure outcomes.
Statistical rigor requirements
- Pre-define: sample size, test duration, primary metric, and success criteria before starting
- Random assignment: users randomly split into control (old model) and treatment (new model)
- Sufficient sample size: use a power analysis to determine how many users you need
- No peeking: do not check results daily and stop when they look good (inflates false positive rate)
- Multiple comparison correction: if testing multiple metrics, apply Bonferroni or FDR correction
- CUPED / variance reduction: use pre-experiment covariates (historical user behavior) to shrink required sample size by 20-50%
- Interleaving for ranking systems: when comparing rankings (e.g., search models), team-draft interleaving needs 10-100x fewer users than parallel A/B for the same statistical power.
The lab below runs a simulated A/B test. Two arms receive traffic, conversions pile up, and after every batch the lab recomputes the lift, a 95% confidence interval and a p-value.
Do: Set traffic to treatment at 5%, press Run, choose 5x speed and watch the p-value. Then press Reset, set the split to 50%, and run again at 5x.Notice: In the code the treatment truly converts at 9.4% against 8.2% for control, so a real winner exists. Early on the observed lift jumps around and the p-value stays well above 0.05. At 50% both arms fill at the same speed, and the p-value usually crosses 0.05 sooner than at 5%.Why: The p-value comes from a two-proportion z-test on the counts so far, so it needs enough visitors in both arms. A small split protects users but starves the treatment arm. Simulated stand-in: visitors are random draws at fixed rates, not real traffic. The treatment arm also shows higher latency, a guardrail to read before you ship.
#Shadow Mode: A/B Without User Risk
Use shadow mode to:
- Verify the candidate matches or exceeds the control on a representative live traffic distribution (not a static eval set).
- Catch latency, error-rate, and cost regressions before any user sees them.
- Build a labeled comparison corpus you can later score with LLM-as-judge.
Here is shadow mode as a lab. Every request is served by v1 for the user while a copy goes to v2, and the lab compares the two and reads out a verdict.
Do: With v2 quality on Healthy, press Start traffic and let the count pass 200 requests. Then press Reset, choose Minor regression, and start again. Last, choose Broken.Notice: Healthy shows only a few percent disagreement and the verdict reads Safe to promote. Minor regression pushes disagreement past the 12% line and the verdict flips to Regression detected. Broken adds errors and v2 latency about three times v1.Why: The verdict rule in the code needs at least 200 requests, then fails above 2% v2 errors or 12% disagreement, and warns when v2 is 1.5 times slower. Those thresholds are this lab's own choices, not a standard. Drawn stand-in, not a trained model: predictions are random labels and each regression level is a preset rate. A disagreement is not always a mistake by v2, so review the cases before judging.
#Sizing an A/B Test (the math you should be able to do)
Most ML A/B tests are underpowered because no one runs the sample-size calculation. Do it explicitly.
A/B Test Timeline
#Red Teaming: Breaking Your Model Before Users Do
Red teaming is systematic adversarial testing, trying to make the model fail, produce harmful outputs, leak private data, or behave unexpectedly:
Categories of red teaming
- Safety: Can the model be tricked into generating harmful content?
- Factuality: Does the model hallucinate confidently about verifiable facts?
- Robustness: Does the model break on unusual inputs (empty strings, very long inputs, special characters)?
- Privacy: Can the model be made to reveal training data or PII?
- Fairness: Does the model exhibit bias across demographic groups?
- Security: Is the model vulnerable to prompt injection, jailbreaks, or extraction attacks?
Red teaming approaches
- Manual expert red teaming: Domain experts try to break the model. High quality, low scale.
- Automated adversarial generation: Use another LLM to generate adversarial prompts. High scale, moderate quality.
- Community red teaming: Open the model to a large group of testers. High diversity of attacks.
- Continuous automated scanning: Run adversarial test suites on every deployment. Catches regressions.
#Key Takeaways
- Multi-layered evaluation covers what single metrics miss. Combine automated metrics (BLEU, ROUGE), LLM-as-judge scoring, human evaluation, and behavioral testing for comprehensive quality assessment
- A/B testing is the gold standard for production evaluation. Statistical rigor (proper sample sizes, significance testing, no peeking) is essential to avoid false conclusions about model improvements
- Red teaming finds failures before users do. Systematically probing for adversarial inputs, edge cases, bias, and safety violations reveals weaknesses that standard benchmarks miss
- Behavioral testing checks specific capabilities. Like unit tests for software, behavioral tests verify that the model handles specific input patterns correctly (negation, ambiguity, format changes) regardless of overall accuracy
#Quick Check
Why is 'peeking' at A/B test results before the pre-determined end date problematic?