Sub topic 1
Why Evaluating LLM Applications Is Different
Prerequisites
This chapter assumes you can:
- Read and write basic Python, including functions, lists, and dictionaries.
- Call an LLM through an API or SDK and build a prompt.
- Understand a system built around a model, such as a prompt template plus retrieval plus post-processing.
- Compute a mean, a proportion, and a variance.
No evaluation experience is needed. This chapter sets up the problem that the rest of the tutorial solves.
The core difference
A conventional unit test calls a function with a fixed input and asserts one exact output. If add(2, 3) returns 5 today, it returns 5 tomorrow. A pass or fail result tells you something definite.
An LLM application breaks this pattern in four ways:
- The output space is open. Many different strings can be correct answers to the same question.
- The output can vary between runs. The same input can produce different text.
- Quality is a matter of degree. An answer can be accurate but rude, or helpful but a little wrong.
- The system has many moving parts. The model, prompt, retrieval index, and settings can all change the result.
You therefore stop asking "does this test pass?" and start asking "how often does this system do well enough, on which kinds of input, and how sure are we?"
Open-ended outputs
Consider a summarization app. For one article there may be thousands of acceptable summaries. An exact string comparison will reject nearly all of them.
expected = "The city council approved the new budget on Tuesday."
output = "On Tuesday, the council passed the new city budget."
print(output == expected) # False, yet the output is a good summaryThe assertion fails even though a human would accept the output. The opposite failure also happens. A string can match a loose check while being wrong. A check like "Paris" in output passes for "The capital is not Paris."
So you need scoring methods that match the kind of task. The later chapters cover them: deterministic checks, reference metrics, LLM judges, and human review. Choosing among them is a central skill in this field.
Nondeterminism
Most LLM APIs sample tokens. With a temperature above zero, two calls with the same prompt can return different text. Even at temperature zero, outputs can differ slightly across runs because of batching, hardware, or provider-side model updates. Do not assume that zero temperature gives you a deterministic system.
This changes how you read a result. Suppose you run a 20-case test set twice and get 15 passes, then 17. That difference of two cases may be pure noise. A single run is one sample from a distribution.
The simulation below shows the effect. Each case passes with probability 0.75, and we run the same 20 cases five times.
import random
rng = random.Random(42)
n_cases = 20
true_pass_prob = 0.75
for run in range(1, 6):
passes = sum(rng.random() < true_pass_prob for _ in range(n_cases))
print(f"run {run}: {passes}/{n_cases} = {passes / n_cases:.2f}")The system never changes, yet the pass rate moves between runs. If you "improve" your prompt and the score rises from 0.70 to 0.75, you cannot conclude that the prompt helped. A later chapter shows how to attach confidence intervals to scores so that you can separate real change from noise.
Quality has several dimensions
A single output can succeed on one dimension and fail on another. A customer-support answer might be:
- Correct, meaning the facts are right.
- Grounded, meaning every claim is supported by the supplied documents.
- On format, meaning it follows the required JSON or length limit.
- In tone, meaning it is polite and matches your brand.
- Safe, meaning it does not leak data or follow harmful instructions.
These can pull against each other. A longer answer may be more complete but less concise. A cautious answer may be safer but less helpful. Averaging them into one number hides these trade-offs, so you usually track several scores and decide which ones are hard requirements and which are goals to improve.
Many components can change the result
In conventional software, a bug lives in your code. In an LLM application, behavior depends on many layers:
| Layer | Example change | Possible effect |
|---|---|---|
| Model | Provider updates the model version | Style or accuracy shifts without any code change |
| Prompt | You reword an instruction | Format failures appear |
| Retrieval | New chunk size or embedding model | Different documents reach the model |
| Parameters | Temperature or max tokens | Variability or truncated answers |
| Post-processing | A new output parser | Valid answers rejected |
Because of this, an evaluation must be repeatable and tied to specific versions. When a score changes, you need to know which layer moved. If you change three things at once, you cannot tell which one caused the shift.
Failures are quiet
Traditional software often fails loudly. It throws an exception, returns a 500 error, or crashes. An LLM application usually fails quietly. It returns fluent, confident text that is wrong. Nothing in your logs flags it.
Common quiet failures include:
- Hallucination: a claim that is not supported by facts or by the supplied context.
- Omission: a correct answer that leaves out a key detail.
- Format drift: output that is almost valid JSON but not quite.
- Silent regression: a prompt edit that fixes one case and breaks ten others.
Since no error signal exists, you have to build the signal yourself. That is what an evaluation does.
Averages hide slices
An overall pass rate can look healthy while a group of inputs fails badly. Suppose 90% of your test cases are short English questions and 10% are long multilingual ones. A system can score 92% overall and 40% on the multilingual slice.
results = [
{"slice": "short_english", "passed": True},
{"slice": "short_english", "passed": True},
{"slice": "short_english", "passed": True},
{"slice": "short_english", "passed": True},
{"slice": "multilingual", "passed": False},
{"slice": "multilingual", "passed": True},
{"slice": "multilingual", "passed": False},
]
def pass_rate_by_slice(results):
totals = {}
for r in results:
passed, count = totals.get(r["slice"], (0, 0))
totals[r["slice"]] = (passed + r["passed"], count + 1)
return {name: p / c for name, (p, c) in totals.items()}
overall = sum(r["passed"] for r in results) / len(results)
print(f"overall: {overall:.2f}")
print(pass_rate_by_slice(results))Always report results by slice (topic, language, input length, user type) as well as overall. The slices you pick should come from the failure modes you care about, which the next chapter covers.
Evaluation is part of development
Conventional tests are often written after the code and run as a gate. LLM evaluation works better as a loop that drives development:
- Define what good means for your task.
- Collect representative cases.
- Score the system.
- Inspect failures.
- Change one thing.
- Score again and compare.
Teams that skip this loop tune prompts by trying a few examples and trusting their impression. This is sometimes called "vibe checking." It is fine for a first prototype. It fails as the system grows, because impressions do not scale, do not detect regressions, and favor the examples you happened to try.
Common mistakes
- Treating one run as the truth. Run enough cases and, where cost allows, repeat runs.
- Testing on the examples used to write the prompt. If you tuned the prompt on a case, that case no longer measures generalization. Keep some cases held out.
- Using one metric for everything. A score that suits classification does not suit open-ended writing.
- Changing several things at once. You lose the ability to attribute a score change.
- Ignoring cost and latency. A more accurate system that is ten times slower or more expensive may be unusable. Track these alongside quality.
What the rest of the tutorial builds
This chapter explained why the problem is hard. The following chapters build the solution in order: define success criteria, build a dataset, write a harness, choose scoring methods, validate judges, quantify uncertainty, and then extend to retrieval, agents, safety, and production monitoring.
Exercise
A teammate changes a prompt, runs your 20-case test set once, and reports that the pass rate rose from 0.70 to 0.75 and that the new prompt is better. Give two reasons to doubt this conclusion and one step to make it more trustworthy.
Show answer
First, one run is one sample. With sampled outputs, identical systems can differ by a case or two between runs, and 0.70 to 0.75 is a one-case difference on 20 cases. Second, 20 cases is a small set, so the uncertainty around each pass rate is wide, and the improvement may be within the noise. To make the result trustworthy, repeat the runs or enlarge the dataset, then report a confidence interval for each version (or for the difference) and also check that no important slice got worse.
Exercise
Why does the check "Paris" in output give unreliable results for the question "What is the capital of France?"
Show answer
It accepts wrong answers that contain the word, such as "The capital is not Paris," and rejects correct answers that phrase it differently or use another form, such as "París." Substring checks measure the presence of a string, not whether the answer is correct, so they need to be paired with a stronger method or applied only where the output format is tightly controlled.