Sub topic 1
Why Evaluating LLM Applications Is Different
Prerequisites
This chapter assumes basic Python, experience calling an LLM through an API or SDK, and familiarity with prompts. You should also know the mean and proportion.
Why LLM Evaluation Is Different
Conventional software usually maps the same input to the same output. A test can compare the actual value with an expected value. A large language model (LLM) can produce several valid answers for one input. Small prompt, model, or sampling changes can also alter the result.
Correctness is often broader than text equality. An answer may use different words, follow the user’s intent, and still be correct. An answer can also sound convincing while containing unsupported claims. Evaluation must therefore measure the behavior that matters, not only whether strings match.
Defining What Good Looks Like
Start by listing the task’s success criteria and failure modes.
- Correctness: Does the answer contain the required facts?
- Relevance: Does it address the user’s request without unnecessary content?
- Format: Does it follow the required schema or structure?
- Safety: Does it avoid prohibited or harmful behavior?
- Grounding: Are claims supported by supplied sources?
Make each criterion observable. For example, “helpful” is vague, but “returns a valid JSON object with a priority field” can be checked directly.
Building an Evaluation Dataset
An evaluation dataset is a collection of representative test cases. Each case should include an input, an identifier, and either an expected output or grading criteria.
Include common requests, boundary cases, ambiguous inputs, and known failures. Keep a separate test set for final comparisons so that repeated tuning does not overfit the examples.
Writing a Repeatable Evaluation Harness
An evaluation harness runs the application on every case, applies a scorer, and records each result. Recording per-case outputs makes failures available for error analysis.
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, resultsExercise
Why should an evaluation harness record each case instead of only returning the average score?
Show answer
Per-case results show which inputs failed and support error analysis.