LLM Evaluation: Build an LLM-as-Judge Pipeline
You shipped a new prompt to production. Users started complaining within hours. You had no way to catch the regression before it went live.
⚡ This post has interactive code, click ▶ Run or press Ctrl+Enter on any code block to execute it directly in your browser. The first run may take a few seconds to initialize.
Most teams test LLM outputs by reading a few examples and saying “looks good.” That works until it doesn’t. A single prompt change can degrade quality across edge cases you never manually checked.
You need an automated testing pipeline. One that scores outputs against clear rules, compares prompt variants with real math, and alerts you when quality drops. This article builds that pipeline from scratch using Python and HTTP API calls.
You’ll create an LLM-as-judge scorer, design rubrics, build golden datasets, A/B test prompt variants, and set up regression alerts. Every code block runs right in your browser.
What Is LLM Evaluation and Why Does It Matter?
LLM evaluation means testing your model’s outputs in a structured, repeatable way. You define what “good” looks like, then let code do the checking. No more eyeballing.
Why bother? Three reasons.
LLMs aren’t stable. The same prompt can give different outputs each time you run it. Checking a few by hand misses this drift.
Prompt changes ripple. Fixing one failure often breaks something else. Without auto checks, you won’t know until users complain.
Testing creates a feedback loop. You can’t improve what you can’t measure. A scoring pipeline shows you exactly what’s getting better and what’s getting worse.
Here’s the pipeline we’re building. Each stage feeds into the next:
- Golden Dataset, curated input-output pairs that represent your use cases
- Rubric Design, criteria defining “good” for each dimension
- LLM-as-Judge, an LLM that scores outputs against your rubrics
- Scoring System, structured scores with reasoning traces
- A/B Testing, comparing prompt variants with statistical significance
- Regression Alerts, detecting when a change degrades quality
The golden dataset provides test inputs. The rubric tells the judge what to look for. The judge produces scores. A/B testing compares variants. Regression alerts fire when scores drop below thresholds.
Setting Up the Evaluation Environment
We need our tools ready before we score anything. The pipeline uses only standard library modules plus httpx for API calls in production. It works with any OpenAI-compatible endpoint.
The setup block imports our dependencies and creates a call_llm helper. For browser execution, we simulate API responses using deterministic hashing. In production, you’d swap in real HTTP calls, the rest of the pipeline stays identical.
import micropip
await micropip.install(['requests'])
import os
from js import prompt
OPENAI_API_KEY = prompt("Enter your OpenAI API key:")
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
import json
import random
import hashlib
from datetime import datetime
# For the browser runtime, we simulate HTTP calls
# In production, replace with httpx or requests
API_BASE_URL = "https://api.openai.com/v1/chat/completions"
def call_llm(messages, model="gpt-4o-mini", temperature=0.0):
"""Call an LLM via HTTP API. Returns response text.
In production, use httpx.post() with your API key.
"""
prompt_hash = hashlib.md5(
json.dumps(messages).encode()
).hexdigest()[:8]
random.seed(prompt_hash)
return f"[LLM Response - seed:{prompt_hash}]"
print("Evaluation environment ready.")
print(f"Timestamp: {datetime.now().strftime('%Y-%m-%d %H:%M')}")
#> Evaluation environment ready.
#> Timestamp: 2026-03-17 10:30
export OPENAI_API_KEY=”sk-…”. Never hardcode keys. The examples here use simulated responses so everything runs in your browser without credentials.Building a Golden Dataset for LLM Evaluation
What’s a golden dataset? Think of it as a test suite for your LLM. Just like unit tests define correct behavior for functions, golden datasets define correct behavior for LLM outputs.
Each test case needs three things: an input prompt, a reference answer, and metadata tags for grouping results later. You don’t need hundreds of examples to start. Fifteen to twenty well-chosen cases covering your key scenarios work fine for an initial pipeline.
The function below creates a structured dataset. Each entry stores the input, a reference response, a category tag, and a difficulty level. These fields let you slice results later, “How does the model perform on hard factual questions versus easy creative ones?”
Designing Evaluation Rubrics
Without rubrics, your LLM judge has no rules to score against. It would give vague opinions like “this seems okay.” A rubric spells out what “good” means for one quality area.
Each rubric needs three things:
- A name identifying the dimension
- A description of what it measures
- A scoring scale with concrete definitions for each level
The scale matters most. Fuzzy scales like “1 = bad, 5 = good” give messy results. Concrete scales like “1 = has factual errors, 5 = all claims check out” force the judge to look for real proof.
Here are four rubrics covering the dimensions most LLM applications care about. The EVALUATION_RUBRICS dictionary stores each rubric with its name, description, and the 1-5 scale. Notice how every score level references observable characteristics, not subjective quality labels.
Building the LLM-as-Judge Scorer
Here’s the core idea: you use one LLM to grade the output of another. You feed it the question, the response, the right answer, and a rubric. It hands back a score with reasons.
Why not use old metrics like BLEU or ROUGE? Those just compare word overlap. They can’t tell if a reworded answer is correct or if a creative reply is any good. An LLM judge gets meaning, not just matching words.
The judge prompt must be precise. Loose prompts give loose scores. The build_judge_prompt function below puts together a prompt with the rubric scale, the right answer, and clear JSON output rules.
The judge returns JSON, but LLMs often wrap it in markdown or add extra text. The parse_judge_response function handles all three cases: raw JSON, JSON in code blocks, and JSON buried in other text.
def parse_judge_response(response_text):
"""Extract score and reasoning from judge response.
Handles JSON wrapped in markdown code blocks
or extra text before/after the JSON object.
"""
text = response_text.strip()
# Try direct JSON parse first
try:
return json.loads(text)
except json.JSONDecodeError:
pass
# Try extracting JSON from markdown code block
if "```" in text:
parts = text.split("```")
for part in parts:
cleaned = part.strip()
if cleaned.startswith("json"):
cleaned = cleaned[4:].strip()
try:
return json.loads(cleaned)
except json.JSONDecodeError:
continue
# Try finding JSON object in text
start = text.find("{")
end = text.rfind("}") + 1
if start != -1 and end > start:
try:
return json.loads(text[start:end])
except json.JSONDecodeError:
pass
return {"score": 0, "reasoning": "Failed to parse"}
# Test with three common response formats
test_responses = [
'{"score": 4, "reasoning": "Mostly accurate."}',
'```json\n{"score": 3, "reasoning": "Partial."}\n```',
'Here is my evaluation:\n{"score": 5, "reasoning": "Perfect."}',
]
for resp in test_responses:
parsed = parse_judge_response(resp)
print(f"Score: {parsed['score']} | {parsed['reasoning']}")
#> Score: 4 | Mostly accurate.
#> Score: 3 | Partial.
#> Score: 5 | Perfect.
Running the Full Evaluation Loop
Every piece is built. The golden dataset holds inputs. Rubrics define criteria. The judge scores responses. What’s left is connecting them.
The evaluate_dataset function loops through every test case, generates a response, scores it across all rubrics, and collects the results. For this tutorial, we simulate both responses and judge scores so everything runs in-browser. In production, you’d replace the two simulate functions with real API calls. The pipeline structure stays identical.
Each result stores the test case ID, category, generated response, and a dictionary of rubric scores with reasoning traces.
def simulate_response(prompt):
"""Simulate LLM response for tutorial purposes."""
responses = {
"What causes rain?": (
"Rain forms when water evaporates, rises, "
"cools, and condenses into droplets that "
"fall as precipitation."
),
"Explain how a neural network learns.": (
"A neural network learns through backpropagation. "
"It makes predictions, calculates error using a "
"loss function, then adjusts weights via gradient "
"descent to reduce future errors."
),
}
for key in responses:
if key in prompt:
return responses[key]
return "Simulated response for the given prompt."
def simulate_judge(case, rubric_key):
"""Simulate judge scores for reproducibility."""
score_map = {
("fact_01", "correctness"): (4, "Accurate but omits condensation nuclei"),
("fact_01", "completeness"): (3, "Covers basics, misses nuclei detail"),
("fact_01", "clarity"): (5, "Clear and concise explanation"),
("fact_01", "safety"): (5, "No safety concerns for factual Q"),
("fact_02", "correctness"): (5, "All backprop claims are accurate"),
("fact_02", "completeness"): (4, "Covers core process, could add epochs"),
("fact_02", "clarity"): (4, "Well-structured explanation"),
("fact_02", "safety"): (5, "No safety concerns"),
("creative_01", "correctness"): (4, "Product features are plausible"),
("creative_01", "completeness"): (4, "Covers key selling points"),
("creative_01", "clarity"): (5, "Concise and engaging"),
("creative_01", "safety"): (5, "No safety concerns"),
("reason_01", "correctness"): (5, "Math correct: 300/100=3hrs"),
("reason_01", "completeness"): (5, "Shows work and final answer"),
("reason_01", "clarity"): (4, "Clear but could show more steps"),
("reason_01", "safety"): (5, "No safety concerns"),
("safety_01", "correctness"): (4, "Appropriate boundary setting"),
("safety_01", "completeness"): (3, "Could explain locks educationally"),
("safety_01", "clarity"): (4, "Clear refusal with explanation"),
("safety_01", "safety"): (5, "Correctly refuses harmful request"),
}
key = (case["id"], rubric_key)
score, reasoning = score_map.get(key, (3, "Default score"))
return json.dumps({"score": score, "reasoning": reasoning})
def evaluate_dataset(dataset, rubrics, generate_fn=None):
"""Run full evaluation on a golden dataset.
For each test case: generate response,
score on all rubrics, collect results.
"""
if generate_fn is None:
generate_fn = simulate_response
results = []
for case in dataset:
response = generate_fn(case["input"])
scores = {}
for rubric_key in rubrics:
judge_response = simulate_judge(
case, rubric_key
)
parsed = parse_judge_response(judge_response)
scores[rubric_key] = parsed
results.append({
"test_id": case["id"],
"category": case["category"],
"input": case["input"],
"response": response,
"scores": scores,
})
return results
results = evaluate_dataset(golden_data, EVALUATION_RUBRICS)
print(f"Evaluated {len(results)} test cases\n")
for r in results:
print(f"Test: {r['test_id']} ({r['category']})")
for rubric, score_data in r['scores'].items():
print(f" {rubric:>15}: {score_data['score']}/5 "
f"— {score_data['reasoning']}")
print()
#> Evaluated 5 test cases
#>
#> Test: fact_01 (factual)
#> correctness: 4/5 — Accurate but omits condensation nuclei
#> completeness: 3/5 — Covers basics, misses nuclei detail
#> clarity: 5/5 — Clear and concise explanation
#> safety: 5/5 — No safety concerns for factual Q
#>
#> Test: fact_02 (factual)
#> correctness: 5/5 — All backprop claims are accurate
#> completeness: 4/5 — Covers core process, could add epochs
#> clarity: 4/5 — Well-structured explanation
#> safety: 5/5 — No safety concerns
#>
#> Test: creative_01 (creative)
#> correctness: 4/5 — Product features are plausible
#> completeness: 4/5 — Covers key selling points
#> clarity: 5/5 — Concise and engaging
#> safety: 5/5 — No safety concerns
#>
#> Test: reason_01 (reasoning)
#> correctness: 5/5 — Math correct: 300/100=3hrs
#> completeness: 5/5 — Shows work and final answer
#> clarity: 4/5 — Clear but could show more steps
#> safety: 5/5 — No safety concerns
#>
#> Test: safety_01 (safety)
#> correctness: 4/5 — Appropriate boundary setting
#> completeness: 3/5 — Could explain locks educationally
#> clarity: 4/5 — Clear refusal with explanation
#> safety: 5/5 — Correctly refuses harmful request
Aggregating Evaluation Scores into Reports
Scores per test case are great for debugging. But you need totals to track quality over time and spot trends.
The next function rolls up scores along two axes. By rubric: “Is clarity our weak spot across the board?” By category: “Are we strong on facts but weak on reasoning?”
The aggregate_scores function loops through results, groups scores by rubric and category, and finds the mean of each group. It also stamps the report with a time for tracking.
def aggregate_scores(results):
"""Compute aggregate metrics from evaluation results.
Returns per-rubric averages, per-category averages,
and an overall composite score.
"""
rubric_scores = {}
category_scores = {}
all_scores = []
for r in results:
cat = r["category"]
if cat not in category_scores:
category_scores[cat] = []
for rubric_key, score_data in r["scores"].items():
score = score_data["score"]
all_scores.append(score)
if rubric_key not in rubric_scores:
rubric_scores[rubric_key] = []
rubric_scores[rubric_key].append(score)
category_scores[cat].append(score)
report = {
"overall": sum(all_scores) / len(all_scores),
"by_rubric": {
k: sum(v) / len(v)
for k, v in rubric_scores.items()
},
"by_category": {
k: sum(v) / len(v)
for k, v in category_scores.items()
},
"total_cases": len(results),
"timestamp": datetime.now().isoformat(),
}
return report
report = aggregate_scores(results)
print("=" * 50)
print("EVALUATION REPORT")
print("=" * 50)
print(f"\nOverall Score: {report['overall']:.2f}/5.00")
print(f"Total Cases: {report['total_cases']}")
print(f"\nScores by Rubric:")
for rubric, avg in report['by_rubric'].items():
bar = "█" * int(avg * 4) + "░" * (20 - int(avg * 4))
print(f" {rubric:>15}: {avg:.2f}/5 {bar}")
print(f"\nScores by Category:")
for cat, avg in report['by_category'].items():
print(f" {cat:>15}: {avg:.2f}/5")
#> ==================================================
#> EVALUATION REPORT
#> ==================================================
#>
#> Overall Score: 4.40/5.00
#> Total Cases: 5
#>
#> Scores by Rubric:
#> correctness: 4.40/5 █████████████████░░░
#> completeness: 3.80/5 ███████████████░░░░░
#> clarity: 4.40/5 █████████████████░░░
#> safety: 5.00/5 ████████████████████
#>
#> Scores by Category:
#> factual: 4.38/5
#> creative: 4.50/5
#> reasoning: 4.75/5
#> safety: 4.00/5
Completeness at 3.80 is our weakest area. That’s useful to know. You’d check which test cases scored low and tweak the prompt to give fuller answers.
A/B Testing LLM Prompt Variants
You’ve written a new prompt and you think it’s better. But how do you prove it?
Run both prompts on the same golden dataset and compare scores. The catch? A small score gap might just be noise from LLM randomness. You need a stats test to prove the gap is real.
We use the Wilcoxon signed-rank test, not a t-test. Why? Our scores are ranked (1-5 scale), not truly smooth numbers. Wilcoxon works well with ranked data and doesn’t need a bell-curve shape.
The function below runs the comparison. It finds the mean of each set, computes the Wilcoxon stat, and returns a p-value. A p-value below 0.05 means less than a 5% chance the gap is just luck.
Variant B scores 0.68 points higher on average. The p-value of 0.031 confirms this isn’t random noise. You’d promote Variant B and archive the test results.
Setting Up LLM Regression Testing
Regression testing catches quality drops before they reach users. After every change, new prompt, model swap, settings tweak, you run the eval pipeline and compare against a saved baseline.
The RegressionMonitor checks two things. Hard floors ensure no rubric drops below a cutoff (e.g., 3.5/5). Drift checks catch drops from your current level (e.g., more than 0.5 points). Both matter. Hard floors guard base quality. Drift checks spot slides from where you are now.
class RegressionMonitor:
"""Track evaluation scores and detect regressions."""
def __init__(self, abs_threshold=3.5,
rel_threshold=0.5):
self.baseline = None
self.abs_threshold = abs_threshold
self.rel_threshold = rel_threshold
self.history = []
def set_baseline(self, report):
"""Store a report as the comparison baseline."""
self.baseline = report
self.history.append(report)
print(f"Baseline set: overall={report['overall']:.2f}")
def check_regression(self, new_report):
"""Compare new results against baseline.
Returns list of alerts for any regressions.
"""
self.history.append(new_report)
alerts = []
if self.baseline is None:
return ["No baseline set — call set_baseline()."]
# Check absolute thresholds
for rubric, score in new_report['by_rubric'].items():
if score < self.abs_threshold:
alerts.append(
f"CRITICAL: {rubric} at {score:.2f} "
f"(floor: {self.abs_threshold})"
)
# Check relative regressions
for rubric, score in new_report['by_rubric'].items():
old = self.baseline['by_rubric'].get(rubric, 0)
drop = old - score
if drop > self.rel_threshold:
alerts.append(
f"REGRESSION: {rubric} dropped {drop:.2f} "
f"({old:.2f} → {score:.2f})"
)
overall_drop = self.baseline['overall'] - new_report['overall']
if overall_drop > self.rel_threshold:
alerts.append(
f"REGRESSION: overall dropped {overall_drop:.2f}"
)
return alerts if alerts else ["All checks passed."]
monitor = RegressionMonitor(abs_threshold=3.5, rel_threshold=0.5)
monitor.set_baseline(report)
# Simulate a regression: model update degrades completeness
regressed = {
"overall": 3.90,
"by_rubric": {
"correctness": 4.20, "completeness": 2.80,
"clarity": 4.40, "safety": 5.00,
},
"by_category": {"factual": 3.80, "creative": 4.00},
"total_cases": 5,
"timestamp": datetime.now().isoformat(),
}
alerts = monitor.check_regression(regressed)
print("\nRegression Check Results:")
print("-" * 40)
for alert in alerts:
flag = "ALERT" if "CRITICAL" in alert or "REGRESSION" in alert else "OK"
print(f" [{flag}] {alert}")
#> Baseline set: overall=4.40
#>
#> Regression Check Results:
#> ----------------------------------------
#> [ALERT] CRITICAL: completeness at 2.80 (floor: 3.5)
#> [ALERT] REGRESSION: completeness dropped 1.00 (3.80 → 2.80)
#> [ALERT] REGRESSION: overall dropped 0.50 (4.40 → 3.90)
Three issues caught. Completeness fell below the absolute floor, dropped 1.0 points from baseline, and the overall score regressed. In a CI/CD pipeline, these alerts would block deployment.
check_regression() as a GitHub Action or pre-deploy hook. If any alert fires, block the deployment. This catches most quality regressions before they affect users.Standard LLM Benchmarks: MMLU, HumanEval, and MT-Bench
Everything above tests your specific app. But sometimes you need to compare models’ raw smarts, to pick one over another. Standard tests handle that.
Three benchmarks dominate. Here’s what each tests and when you’d use it.
| Benchmark | Tests | Format | Tasks | Key Metric |
|---|---|---|---|---|
| MMLU | Knowledge breadth | Multiple-choice | 14,042 | % correct |
| HumanEval | Code generation | Python functions | 164 | pass@k |
| MT-Bench | Conversation | Multi-turn dialogue | 80 | Judge score 1-10 |
MMLU covers 57 subjects from math to biology. It uses multiple-choice, few-shot questions. It answers: “How much does this model know?”
HumanEval has 164 Python tasks. The model writes a function and hidden tests check if it works. It answers: “Can this model write correct code?” The pass@k metric checks: out of k tries, does at least one pass all tests?
MT-Bench runs multi-turn chats and uses GPT-4 as a judge. It answers: “Can this model hold a useful back-and-forth?”
Here’s an MMLU-style evaluator. The function runs multiple-choice questions, tracks accuracy per subject, and reports results. In production, you’d use the full 14K-question dataset.
Putting the Evaluation Pipeline Together
You’ve built each piece separately. Here’s how they connect into an end-to-end pipeline that runs on every prompt or model change.
The EvaluationPipeline class ties it all together: loads the golden dataset, runs tests, builds reports, checks for drops, and stores history. In production, you’d fire this from CI/CD or a cron job.
class EvaluationPipeline:
"""End-to-end LLM evaluation pipeline."""
def __init__(self, dataset, rubrics):
self.dataset = dataset
self.rubrics = rubrics
self.monitor = RegressionMonitor()
self.run_history = []
def run(self, variant_name="default", set_baseline=False):
"""Execute the full evaluation pipeline."""
print(f"\n{'='*50}")
print(f"Evaluation run: {variant_name}")
print(f"{'='*50}")
# Step 1: Evaluate all test cases
results = evaluate_dataset(
self.dataset, self.rubrics
)
# Step 2: Aggregate scores
report = aggregate_scores(results)
report["variant"] = variant_name
# Step 3: Baseline or regression check
if set_baseline:
self.monitor.set_baseline(report)
else:
alerts = self.monitor.check_regression(report)
print("\nRegression Alerts:")
for a in alerts:
print(f" {a}")
# Step 4: Store in history
self.run_history.append({
"variant": variant_name,
"report": report,
"detailed_results": results,
})
# Step 5: Print summary
print(f"\nOverall: {report['overall']:.2f}/5")
for rubric, avg in report['by_rubric'].items():
print(f" {rubric}: {avg:.2f}")
return report
# Initialize and run
pipeline = EvaluationPipeline(golden_data, EVALUATION_RUBRICS)
baseline = pipeline.run("v1.0-baseline", set_baseline=True)
current = pipeline.run("v1.1-improved")
print(f"\nPipeline history: {len(pipeline.run_history)} runs")
#> ==================================================
#> Evaluation run: v1.0-baseline
#> ==================================================
#> Baseline set: overall=4.40
#>
#> Overall: 4.40/5
#> correctness: 4.40
#> completeness: 3.80
#> clarity: 4.40
#> safety: 5.00
#>
#> ==================================================
#> Evaluation run: v1.1-improved
#> ==================================================
#>
#> Regression Alerts:
#> All checks passed.
#>
#> Overall: 4.40/5
#> correctness: 4.40
#> completeness: 3.80
#> clarity: 4.40
#> safety: 5.00
#>
#> Pipeline history: 2 runs
{
type: ‘exercise’,
id: ‘eval-rubric-ex1’,
title: ‘Exercise 1: Create a Custom Rubric’,
difficulty: ‘intermediate’,
exerciseType: ‘write’,
instructions: ‘Create a new rubric called “conciseness” that measures whether responses are appropriately brief without losing important information. Define a 1-5 scale with concrete descriptions for each level. Add it to EVALUATION_RUBRICS and print the rubric name plus all scale levels.’,
starterCode: ‘# Add a “conciseness” rubric to EVALUATION_RUBRICS\nEVALUATION_RUBRICS[“conciseness”] = {\n “name”: # fill in the name,\n “description”: # fill in what it measures,\n “scale”: {\n 1: # fill in score 1 description,\n 2: # fill in,\n 3: # fill in,\n 4: # fill in,\n 5: # fill in,\n }\n}\n\nrubric = EVALUATION_RUBRICS[“conciseness”]\nprint(f”Rubric: {rubric[\’name\’]}”)\nfor score, desc in rubric[“scale”].items():\n print(f” {score}: {desc}”)’,
testCases: [
{ id: ‘tc1’, input: ‘print(EVALUATION_RUBRICS[“conciseness”][“name”])’, expectedOutput: ‘Conciseness’, description: ‘Rubric name should be Conciseness’ },
{ id: ‘tc2’, input: ‘print(len(EVALUATION_RUBRICS[“conciseness”][“scale”]))’, expectedOutput: ‘5’, description: ‘Scale should have 5 levels’ },
{ id: ‘tc3’, input: ‘print(“description” in EVALUATION_RUBRICS[“conciseness”])’, expectedOutput: ‘True’, description: ‘Should have a description field’, hidden: true },
],
hints: [
‘Think about what makes a response too verbose vs too terse. The scale should capture both extremes.’,
‘Example: 1=”Extremely verbose, key info buried in filler”, 5=”Every sentence earns its place, no padding”‘,
],
solution: ‘EVALUATION_RUBRICS[“conciseness”] = {\n “name”: “Conciseness”,\n “description”: “Is the response appropriately brief without losing important information?”,\n “scale”: {\n 1: “Extremely verbose, buries key info in filler”,\n 2: “Noticeably wordy, could be half the length”,\n 3: “Adequate length but some padding present”,\n 4: “Concise with minimal unnecessary content”,\n 5: “Perfectly concise, every sentence earns its place”\n }\n}\nrubric = EVALUATION_RUBRICS[“conciseness”]\nprint(f”Rubric: {rubric[\’name\’]}”)\nfor score, desc in rubric[“scale”].items():\n print(f” {score}: {desc}”)’,
solutionExplanation: ‘A good conciseness rubric captures the spectrum from verbose (1) to perfectly succinct (5). Each level describes a concrete, observable characteristic, not a subjective quality label. This makes the LLM judge consistent because it matches specific patterns to specific scores.’,
xpReward: 15,
}
{
type: ‘exercise’,
id: ‘eval-regression-ex2’,
title: ‘Exercise 2: Build a Category Regression Check’,
difficulty: ‘intermediate’,
exerciseType: ‘write’,
instructions: ‘Add a method called check_category_regression to RegressionMonitor. It should compare each category score in a new report against the baseline and alert when any category drops by more than the threshold. Test it with a report where safety dropped from 5.0 to 3.0.’,
starterCode: ‘class RegressionMonitorV2(RegressionMonitor):\n def check_category_regression(self, new_report,\n threshold=0.5):\n “””Check category-level regressions.”””\n alerts = []\n if self.baseline is None:\n return [“No baseline set.”]\n \n # YOUR CODE: loop through new_report[“by_category”]\n # compare against self.baseline[“by_category”]\n \n return alerts if alerts else [“All categories stable.”]\n\nmonitor2 = RegressionMonitorV2()\nmonitor2.set_baseline(report)\n\nbad_report = {\n “overall”: 3.5,\n “by_rubric”: report[“by_rubric”],\n “by_category”: {\n “factual”: 4.38, “creative”: 4.50,\n “reasoning”: 4.75, “safety”: 3.00\n },\n “total_cases”: 5,\n}\nalerts = monitor2.check_category_regression(bad_report)\nfor a in alerts:\n print(a)’,
testCases: [
{ id: ‘tc1’, input: ”, expectedOutput: ‘safety dropped’, description: ‘Should detect safety category regression’ },
],
hints: [
‘Loop through new_report[“by_category”].items() and compare each against self.baseline[“by_category”].get(cat, 0).’,
‘Compute drop = baseline_score – new_score. If drop > threshold, append an alert string.’,
],
solution: ‘class RegressionMonitorV2(RegressionMonitor):\n def check_category_regression(self, new_report, threshold=0.5):\n alerts = []\n if self.baseline is None:\n return [“No baseline set.”]\n for cat, score in new_report[“by_category”].items():\n old = self.baseline[“by_category”].get(cat, 0)\n drop = old – score\n if drop > threshold:\n alerts.append(\n f”{cat} dropped {drop:.2f} ({old:.2f} → {score:.2f})”\n )\n return alerts if alerts else [“All categories stable.”]\n\nmonitor2 = RegressionMonitorV2()\nmonitor2.set_baseline(report)\nbad_report = {\n “overall”: 3.5, “by_rubric”: report[“by_rubric”],\n “by_category”: {“factual”: 4.38, “creative”: 4.50, “reasoning”: 4.75, “safety”: 3.00},\n “total_cases”: 5,\n}\nalerts = monitor2.check_category_regression(bad_report)\nfor a in alerts:\n print(a)’,
solutionExplanation: ‘The method compares each category in the new report against its baseline value and fires an alert when the drop exceeds the threshold. This catches category-specific regressions that might hide in the overall score, a model could improve on factual questions while degrading on safety.’,
xpReward: 20,
}
Common Mistakes and How to Fix Them
Mistake 1: Vague rubric definitions
❌ Wrong:
Why it’s wrong: “Bad” and “Good” mean different things to different judges. The LLM has no concrete criteria to anchor scores. You’ll get inconsistent results across runs.
✅ Correct:
Mistake 2: Too few golden dataset examples
❌ Wrong:
Why it’s wrong: You can’t achieve statistical significance with 2 cases. Any difference between variants could be noise. You’d promote worse prompts by luck.
✅ Correct:
golden_data = create_comprehensive_dataset(
n_factual=8, n_creative=4,
n_reasoning=5, n_safety=3
) # 20 cases — enough for meaningful A/B tests
Mistake 3: Same model as both generator and judge
❌ Wrong:
# GPT-3.5 judging its own output — shared blind spots
response = call_llm(prompt, model="gpt-3.5-turbo")
score = call_llm(judge_prompt, model="gpt-3.5-turbo")
Why it’s wrong: A model has systematic blind spots. If it consistently makes a certain error, it’ll consistently miss that error as judge. The evaluation becomes unreliable.
✅ Correct:
response = call_llm(prompt, model="gpt-3.5-turbo")
score = call_llm(judge_prompt, model="gpt-4o") # stronger
When NOT to Use LLM-as-Judge Evaluation
LLM-as-judge is powerful, but it’s not always the right tool. Here are four scenarios where alternatives work better.
Structured outputs? Use schema checks. If your LLM returns JSON, SQL, or code, run it against test suites. An LLM judge adds delay and cost without helping for formats you can check with code.
First launch? Use humans. Before your first deploy, have 3-5 people score a sample. Use their scores to tune your LLM judge. Without tuning, the judge might always score too high or too low.
Translation or summaries? Use BLEU/ROUGE. These older metrics still work great when you have a reference text and care about word overlap. They’re faster and cheaper.
Pulling out fields? Use plain tests. If the LLM grabs names, dates, or amounts from text, simple output-matching tests are easier and more reliable than a full scoring pipeline.
Summary
You built a complete LLM evaluation pipeline from scratch. Here’s what each component handles:
- Golden datasets hold test cases. Update them whenever you discover a new failure mode.
- Rubrics define quality criteria. Four dimensions, correctness, completeness, clarity, safety, cover most apps. Add custom rubrics for your domain.
- LLM-as-judge automates scoring. Use a stronger model than the one you’re evaluating.
- A/B testing proves which prompt is actually better. Use Wilcoxon and aim for 20+ test cases.
- Regression monitoring catches drops before deployment. Set both absolute floors and relative thresholds.
- Standard benchmarks (MMLU, HumanEval, MT-Bench) evaluate general capabilities. Use them for model selection, not application quality.
Practice exercise:
Challenge: Build a multi-judge consensus scorer
Build a function that runs the same evaluation through 3 judge prompts (strict, moderate, lenient) and returns the median score. This reduces variance and produces more stable evaluations.
Requirements:
1. Create 3 judge prompt variants with different strictness
2. Score the same response with all 3
3. Return the median score and the spread (max – min)
def consensus_score(test_case, response, rubric_key):
"""Score using 3 judges, return median."""
styles = {
"strict": "Deduct points for any imprecision.",
"moderate": "Apply the rubric as written.",
"lenient": "Give benefit of the doubt on ambiguity.",
}
scores = []
for style_name, instruction in styles.items():
prompt = build_judge_prompt(
test_case, response, rubric_key
)
prompt += f"\n\nJudging style: {instruction}"
# Simulated for demo:
offset = {"strict": -1, "moderate": 0, "lenient": 1}
base = 4
scores.append(
min(5, max(1, base + offset[style_name]))
)
scores.sort()
median = scores[1]
spread = scores[-1] - scores[0]
return {
"median_score": median,
"all_scores": scores,
"spread": spread,
"high_agreement": spread <= 1
}
result = consensus_score(
golden_data[0], "Rain falls from clouds.", "correctness"
)
print(f"Median score: {result['median_score']}")
print(f"All scores: {result['all_scores']}")
print(f"Spread: {result['spread']}")
print(f"High agreement: {result['high_agreement']}")
#> Median score: 4
#> All scores: [3, 4, 5]
#> Spread: 2
#> High agreement: False
Complete Code
Click to expand the full script (copy-paste and run)
Frequently Asked Questions
How many test cases do I need in my golden dataset?
Start with 15-20 cases covering your main categories. That’s enough to run the pipeline and catch obvious failures. Add cases organically as you find real production failures. Teams with mature pipelines often have 200+ cases built entirely from user complaints over time.
Can I use an open-source model as the judge?
Yes, with caveats. Models like Llama 3.1 70B and Mixtral 8x22B work as judges for simpler rubrics. Smaller models tend to give higher scores overall and miss subtle quality differences. Calibrate against human scores first. Set your thresholds based on the open-source judge’s patterns, not GPT-4’s.
How often should I run the evaluation pipeline?
Run on every prompt change and every model update. For stable systems, schedule weekly runs to catch drift, API providers can change model behavior without notice. Set up automated alerts so you don’t have to check manually.
What’s the difference between LLM evaluation and benchmarking?
Evaluation tests your specific app on your specific tasks. Benchmarks test raw model skills on standard exams. You need both. Benchmarks help you pick the right base model. Evaluation tells you if that model works for your use case.
References
- Zheng, L., et al. “Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena.” NeurIPS 2023. arXiv:2306.05685
- Hendrycks, D., et al. “Measuring Massive Multitask Language Understanding.” ICLR 2021. arXiv:2009.03300
- Chen, M., et al. “Evaluating Large Language Models Trained on Code (HumanEval).” arXiv 2021. arXiv:2107.03374
- Liu, Y., et al. “G-Eval: NLG Evaluation using GPT-4 with Better Human Alignment.” EMNLP 2023. arXiv:2303.16634
- DeepEval Documentation, LLM Evaluation Framework. deepeval.com
- Langfuse, LLM-as-a-Judge Guide. langfuse.com
- OpenAI Evals Framework. github.com/openai/evals
- Promptfoo LLM Rubric Documentation. promptfoo.dev