Build a minimal LLM evaluation harness in Python
Sources checked 2 min read · By the Eval Suites editors
In brief
An LLM evaluation harness runs cases through an application, scores each output and records a result you can inspect later. You can build the first version around a stubbed adapter. That lets you verify the wiring before you connect a model or pay for inference.
- Runs with
- Python 3 standard library
- Evidence produced
- Per-case JSON report
- Inference
- Stubbed example; no provider calls
What goes into an evaluation harness?
Use a case ID, an input and the expected behavior. Pass the input through an adapter for the application. Send the output and expected behavior to a scorer. Write one result per case before deciding how the run should exit. Inspect AI documents its own tasks and scorers. Its interfaces are examples of the separation between running and grading a task.[1][2]
| Part | Input | Output |
|---|---|---|
| Cases | Reviewed examples | Versioned IDs, input and expected route |
| Adapter | A case input | Application output with a route |
| Scorer | Output and expected route | Pass or fail per case |
| Report | Cases and verdicts | JSON with case, configuration and failure details |
Run the harness and inspect an incorrect route
Save the code as eval_harness.py and run python3 eval_harness.py. One case routes to billing correctly. The stub routes a damaged parcel inquiry to billing, so that case fails. Open results.json and look at expected_route and actual_route before changing the scorer. The command exits with status 1 in the supplied example.
import json
from pathlib import Path
CASES = (
{"id": "invoice", "text": "Where is my invoice?", "expected_route": "billing"},
{
"id": "damaged-parcel",
"text": "My parcel arrived damaged.",
"expected_route": "shipping",
},
)
def application(text):
return {"route": "billing"}
def main():
results = []
for case in CASES:
output = application(case["text"])
actual_route = output.get("route")
results.append({
"case_id": case["id"],
"expected_route": case["expected_route"],
"actual_route": actual_route,
"passed": actual_route == case["expected_route"],
})
report = {
"case_version": "route-cases-v1",
"adapter_version": "stub-v1",
"scorer_version": "exact-route-v1",
"results": results,
}
Path("results.json").write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
passed = sum(row["passed"] for row in results)
print(f"{passed}/{len(results)} cases passed; see results.json")
return int(passed != len(results))
if __name__ == "__main__":
raise SystemExit(main())Replace the stub without losing the baseline
Connect your application at the adapter boundary. Save the model, configuration, prompt version and runner version for the new run. Use the same case IDs and grading rules so a comparison can distinguish an output change from a scorer change.
In a live harness, record timeouts and execution errors separately from completed wrong answers. An exact route check leaves tone and explanation quality unmeasured. Add a reviewed rubric only if those properties affect the decision.
For an example of comparing two saved prompt versions, see the prompt regression fixture.
For documented tool choices, see the runner comparison.
Distinguish a runner failure from a wrong answer
Give the harness three small inputs before using it for a release decision. One should produce the expected result, one should return a valid response with a wrong value and one should produce an unreadable response. Confirm that the case record tells you which of these happened. A parser exception and a wrong business decision need different fixes.
Now rerun the same inputs in reverse order. A change in verdict is evidence worth investigating for state carried between cases. Keep each case identifier with its captured output so a reordered file cannot silently pair a prediction with the wrong reference. Report the counts of attempted, successfully graded and failed cases alongside any aggregate score.
Related suites
Related reading
Sources
- 1. Inspect AI documentation · UK AI Security Institute · Official docs · Sources checked
- 2. Inspect AI: Scorers · UK AI Security Institute · Official docs · Sources checked