Sub topic 1
Why Evaluating LLM Applications Is Different
Prerequisites
This chapter assumes you can read basic Python, have called an LLM through an API or SDK, and know what a prompt is. It also assumes you know the mean and the proportion from basic statistics. No evaluation experience is needed.
Why ordinary tests fall short
A conventional unit test checks that a function returns one known value for a given input. add(2, 3) returns 5 every time, so assert add(2, 3) == 5 is a complete test.
An LLM application breaks that model in three ways.
- Outputs vary. The same prompt can return different wording on each call, because the model samples tokens from a probability distribution.
- Many answers are correct. "Paris is the capital of France" and "France's capital is Paris" are both right. A string comparison treats them as different.
- Quality is a matter of degree. A summary can be accurate but too long, or fluent but missing a key fact. Pass or fail often hides this.
Because of this, an evaluation (an "eval") does not ask whether one output is correct. It asks how often outputs meet your standard across many realistic inputs.
The shift from tests to measurements
A test gives a verdict on one case. An eval gives a measurement over a set of cases. The result is a rate or score, such as "84% of answers passed," and you judge it against a threshold you chose in advance.
The following diagram shows the basic loop that the rest of this tutorial builds out.
A first look at the loop
The code below is the smallest version of this loop. It runs an application over a list of cases, scores each output, and returns the pass rate along with per-case results.
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.5Three things to notice:
- The scorer is a separate function, so you can swap it for a better one later.
- The harness keeps per-case results, not only the rate. You need them to find out why a case failed.
exact_matchwould mark "The capital is Paris." as wrong. This is the second problem above, and later chapters cover better scorers.
Common mistakes
- Choosing metrics before defining success. A score is only useful if it reflects what your users need.
- Comparing two versions on one run. Outputs vary between runs, so a small gap may be noise. A later chapter covers confidence intervals.
- Looking only at the average. A 90% pass rate can hide a category of inputs that fails every time.
What comes next
The next chapters follow the order in which you would build an eval. You define what good looks like, build a dataset, and write a harness. Then you add scorers: reference metrics, deterministic checks, LLM judges, and human review. After that come uncertainty, retrieval, error analysis, and CI.
Exercise
A teammate says, "I ran our summarizer on five of my own documents and the outputs looked good, so it is ready." Give two reasons this is weaker than an eval.
Show answer
Any two of these: five cases are too few to estimate a rate; the documents were chosen by the person who built the system, so they are probably not representative; "looked good" is a judgment with no stated criterion or threshold; no per-case record exists to repeat the check after a change; and one run does not show how much outputs vary.