How to Build an LLM Evaluation Harness

An LLM eval harness is three things: a golden set of test cases, a scoring function, and a runner that reports a pass rate. It's how you stop shipping prompt or model changes that quietly make answers worse. Here's how to build one.

AI Engineeraillmevaluation

Why you need one

LLM output is non-deterministic, so "it looked fine when I tried it" isn't a test. The moment you change a prompt or swap a model, quality can silently regress. An eval harness turns "looks fine" into a measurable score you can track and gate releases on.

1. A golden set

A list of representative cases with an expected signal. The simplest useful version is (question, expected_keywords):

GOLDEN = [
    {"q": "How do I reset my password?",
     "expected": ["reset", "email", "link"]},
    {"q": "What are your business hours?",
     "expected": ["9", "5", "monday"]},
]

Start with 10-20 real cases (including known failure modes). It grows every time a bug ships - add the case that broke.

2. A scoring function

Turn one response into a number in [0, 1]. Keyword coverage is a cheap, deterministic start:

def score(response: str, expected_keywords: list[str]) -> float:
    if not expected_keywords:
        return 0.0
    low = response.lower()
    hits = sum(1 for k in expected_keywords if k.lower() in low)
    return hits / len(expected_keywords)

Later you can add stronger scorers (regex, JSON-schema validity, or LLM-as-judge for open-ended answers) - the harness shape stays the same.

3. A runner that aggregates

Run every case, score it, and report the aggregate so you get one number to watch:

def run(chat_fn) -> float:
    scores = [score(chat_fn(c["q"]), c["expected"]) for c in GOLDEN]
    avg = sum(scores) / len(scores)
    print(f"avg score: {avg:.2f} over {len(scores)} cases")
    return avg

How to actually use it

Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.

Try it in a workspace →

What you'll practice

FAQ

What is an LLM evaluation harness?

A test setup for LLM output: a golden set of cases with expected signals, a scoring function that turns each response into a number, and a runner that aggregates a pass rate - so prompt/model changes are measured, not guessed.

How do you score LLM responses automatically?

Start with deterministic scorers like keyword coverage or JSON-schema validity. For open-ended answers, use LLM-as-judge (a second model grades the response against criteria). The harness structure is the same either way.

How do I stop LLM quality regressions?

Run the eval harness before and after every prompt or model change and gate on the score (e.g. fail CI if the average drops). Add every production failure to the golden set so it's caught next time.

What is an LLM evaluation harness?

An LLM eval harness is a small framework that runs a fixed set of test cases (a golden set) through your prompt or model, scores each output, and reports an aggregate pass rate - so you catch prompt and model regressions before they ship.

Keep learning

Restore a Broken LLM API IntegrationAI/ML projectHarden an LLM Pipeline Against API FailuresAI/ML projectParse JSON From an LLM (Strip Markdown Fences)AI/ML projectAI/ML roadmapStep by step to hiredAI/ML interview questionsSTAR answersAll AI/ML projectsProjects hub

Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.

Open the workspace →