Sub topic 4
Writing a repeatable evaluation harness
This chapter assumes you can write basic Python, have called an LLM through an SDK, and know what a pass rate is. It covers the working model of an evaluation harness and the errors people hit most often.
What a harness does
An evaluation harness is a small program that runs your application over a fixed set of cases, scores each output, and reports the results. It turns "this feels better" into a number you can compare between versions.
A harness has four parts:
- A dataset of cases, each with an id, an input, and an expected answer or grading criteria.
- The application under test, wrapped as a function from input to output.
- A scorer, which takes an output and the expected answer and returns a pass/fail or a number.
- A report, which gives the aggregate score and the per-case results.
A minimal harness
The scorer is passed in as an argument. You can then swap an exact-match check for a rule-based check or an LLM judge without touching the loop.
def exact_match(output: str, expected: str) -> bool:
return output.strip().lower() == expected.strip().lower()
def run_eval(app, cases, scorer):
results = []
for case in cases:
output = app(case["input"])
passed = scorer(output, case["expected"])
results.append({"id": case["id"], "output": output, "passed": passed})
pass_rate = sum(r["passed"] for r in results) / len(results)
return pass_rate, results
cases = [
{"id": "c1", "input": "Capital of France?", "expected": "Paris"},
{"id": "c2", "input": "Capital of Japan?", "expected": "Tokyo"},
]
def fake_app(question: str) -> str:
return "Paris" if "France" in question else "Kyoto"
rate, results = run_eval(fake_app, cases, exact_match)
print(rate) # 0.5The harness stores the output for every case, not only the score. When the pass rate drops, you open the failing results and read them. A bare number tells you that something broke, but not what.
What makes it repeatable
Repeatable means that two runs on the same version give comparable numbers, and that a change in the number means a change in the application. Keep these controls in place:
- Fix the dataset. Store cases in a versioned file. Do not edit cases between runs you plan to compare.
- Fix the configuration. Record the model name, prompt version, and temperature with each run. A silent model upgrade looks like a regression.
- Keep scoring separate from generation. Save raw outputs first, then score them. You can then re-score with a better scorer without paying for new model calls.
- Use stable ids. Match cases between runs by id, never by list position.
Common errors
| Error | Effect | Fix |
|---|---|---|
| Dividing by zero on an empty dataset | The run crashes or hides a loading bug | Check that the case list is not empty |
| One exception aborts the whole run | You lose all results after the failure | Catch errors per case and record them as failures |
| Scorer returns `None` for bad input | Counted as a pass or fail without notice | Count unscorable cases separately |
| Comparing runs with different settings | Differences reflect configuration, not quality | Log the configuration with each run |
Exercise
Exercise
The harness above crashes if app raises an exception on one case. How would you change run_eval so that the run finishes, and what should the failing case's result contain?
Show answer
Wrap the call to app in try/except inside the loop. On an exception, append a result with the case id, an output of None (or the error message), and passed set to False, plus an "error" field. Reporting errors separately from wrong answers lets you tell a broken application from a weak one.