Automatic and Model-Graded Evaluation

Updated · 7 min read
hardPython 3.13aievaluationllm-as-judgeregression-testingpython

Automatic evaluation grades an AI application's output without a person: with code checks (rules such as "contains $45") or with model-graded checks, where a model grades each answer against a written rubric. Because it is automatic, it can run after every change and block a release that makes things worse.

Code Checks vs Model-Graded Checks

Rules, a judge, and a gate before release
Code checksModel-graded checks
HowRules in codeA model with a rubric
Cost and speedFree, instantTokens, seconds per case
Best forFacts, formats, declinesHelpfulness, completeness, tone
RiskToo strict or too looseBias, inconsistency, favoring long answers

Example 1: A Model as Judge

judge.py
from typing import Literal
 
from pydantic import BaseModel
 
from model_api import ModelClient
 
class Verdict(BaseModel):
    correct: Literal["yes", "partly", "no"]
    reason: str
 
RUBRIC = ("You grade answers from a school assistant against reference facts. "
          "correct is yes if the answer answers the question using the facts, partly if it is related "
          "but misses the point, and no if it is wrong or off topic. Judge correctness only, not style or length.")
client = ModelClient()
 
def judge(question, answer, reference):
    verdict, reply = client.parse(
        [{"role": "user", "content": f"Question: {question}\nAnswer: {answer}\nReference facts: {reference}"}],
        Verdict,
        system=RUBRIC,
        max_tokens=500,
    )
    return verdict
 
CASES = [  # real answers from the 50-question evaluation, with reference facts
    ("Do brothers and sisters get a discount?",
     "Siblings get a 10% discount on tuition from the second child onward.",
     "Siblings get 10% off tuition from the second child."),
    ("We can't afford the fees. Is there help?",
     "You can pay fees online by card or bank transfer through the parent portal.",
     "Families with financial need can apply for a fee waiver at the school office."),
    ("What is the school's policy on pets?",
     "The school is closed on Thanksgiving Day and the day after.",
     "Not covered by the school's documents; the assistant should decline."),
]
for question, answer, reference in CASES:
    verdict = judge(question, answer, reference)
    print(f"{verdict.correct:6} {question}\n       {verdict.reason}")

Example output. The verdicts and reasons were produced by an AI model on November 16, 2026, to show the shape; the program ran end to end against a local test server that returned them, not a live API. The three answers being graded are real failures and passes from "Building an Evaluation Set".

Output
yes    Do brothers and sisters get a discount?
       States the 10% sibling discount on tuition, matching the reference.
partly We can't afford the fees. Is there help?
       About paying fees, but misses the fee waiver the family needs.
no     What is the school's policy on pets?
       Off topic; the assistant should have declined.

Explanation: The rubric defines exactly what yes, partly and no mean and tells the judge to ignore style and length, a common source of bias. Structured output returns a Verdict the code can count. The judge gives more detail than the code check: the waiver answer is partly right rather than simply failed.

Example 2: Comparing Two Versions

A proposed change: raise the decline threshold from 0.35 to 0.45, so Campus Helper declines more off-topic questions. This uses run_eval.py from "Building an Evaluation Set":

compare.py
from run_eval import run_eval
 
baseline = run_eval(threshold=0.35)   # the current version
candidate = run_eval(threshold=0.45)  # the proposed change: decline more often
 
print(f"Baseline {sum(r['passed'] for r in baseline)}/50 -> candidate {sum(r['passed'] for r in candidate)}/50")
for old, new in zip(baseline, candidate):
    if old["passed"] != new["passed"]:
        change = "FIXED " if new["passed"] else "BROKEN"
        print(f"  {change} #{new['id']} {new['question']}")
Output
Baseline 44/50 -> candidate 42/50
  BROKEN #6 Can I pay in cash?
  BROKEN #33 My son lost his jacket. What happens to lost things?
  BROKEN #37 Where can my son get help with homework?
  FIXED  #41 What is the school's policy on pets?

Explanation: The change fixes the pets question, the problem it was meant to solve, but breaks three questions that used to work, so the score falls from 44 to 42. Comparing case by case shows exactly what changed, which a single score hides. This change should be rejected.

Example 3: A Regression Gate

check_regression.py
import sys
 
from run_eval import run_eval
 
BASELINE = 44  # the last accepted score; raise it when a change improves the score
 
score = sum(r["passed"] for r in run_eval())
if score < BASELINE:
    print(f"FAIL: score fell from {BASELINE} to {score}/50. Do not release.")
    sys.exit(1)
print(f"PASS: score {score}/50 (baseline {BASELINE})")
Output
PASS: score 44/50 (baseline 44)

Explanation: The script exits with an error code when the score drops below the last accepted baseline. Run it in continuous integration (CI), the system that tests every code change automatically, by adding the command python check_regression.py to the pipeline; a failing check then blocks the release.

Common Mistakes

Trusting a judge you never checked. Measure its agreement with human labels first.

Looking only at the total. Compare case by case to see what broke.

Letting the baseline drift down. Raise the baseline when the score improves; never lower it quietly.

Interview Corner

What is LLM-as-judge evaluation, and what are its limits?

A model grades outputs against a rubric. It handles open-ended answers well, but it costs tokens, can be inconsistent, and can favor longer or more confident answers, so it must be calibrated against human judgments.

How do you prevent regressions in an AI application?

Run a fixed evaluation set on every change, compare it with the accepted baseline case by case, and block releases when the score drops, usually as a step in CI.

Practice

Exercise: try another change. Lower the threshold to 0.30 in compare.py. Which cases change? Would you accept this version? Update BASELINE only if you do.

Check your understanding:

  1. When do you need a model judge instead of a code check?
  2. Why did the 0.45 threshold get rejected?
  3. What does the regression gate do in CI?

Quick check: automated evaluation

4 questions
1What did raising the threshold to 0.45 do?
2Why tell the judge to ignore style and length?
3What does check_regression.py do when the score drops?
4What should you do before trusting a model judge?

Key Takeaways

Quick reference

NeedTool
Fact checkexpected in answer
Open-ended gradeA judge with a rubric and structured output
What changedCase-by-case comparison
Block regressionsExit with an error below the baseline

Was this tutorial helpful?

Lesson 111 of 186