Sub topic 3
Measure first: an evaluation set and a baseline
This chapter assumes you have called a chat-style LLM API, can read Python, and know what an embedding and a context window are. Before you add examples, retrieval, or training to a system, you need a way to tell whether the change helped. This chapter shows how to build that measurement.
Why measure before changing anything
Teams often pick a technique first, such as "we need RAG", and judge the result by reading a few outputs. That approach has two problems. A handful of outputs hides regressions, and you cannot compare techniques without a shared yardstick.
An evaluation set (eval set) is a fixed list of inputs, each paired with the expected answer or a rule for judging the answer. A baseline is the score your current system gets on that set. Every later change is compared to the baseline.
Building the eval set
Start small. Fifty to a hundred cases is enough to see large differences. Draw them from real traffic when you can, because invented cases tend to be easier than real ones.
Good eval sets share these properties:
- They cover the common cases and the known hard cases, including inputs where the right answer is "I don't know".
- Each case has a clear expected result or a written grading rule.
- They are kept separate from any examples you put in prompts or training data. If a case appears in both, your score is inflated.
- They are labeled by failure type, such as "missing fact" or "wrong format". This links directly to the diagnosis in the previous chapter.
Choosing a metric
Pick the cheapest metric that matches the task.
| Task type | Metric | Notes |
|---|---|---|
| Classification | Exact match, accuracy | Cheap and unambiguous |
| Structured output | Schema validity, field match | Check that JSON parses first |
| Short factual answers | Normalized match | Lowercase and strip punctuation |
| Free-form text | Rubric scored by a person or a judge model | Spot-check the judge against human labels |
A minimal evaluation harness
The harness below scores any function that maps a question to an answer. It uses the ticket classification task from the few-shot example, so you can reuse it when you test prompting later.
def normalize(text):
return text.strip().lower()
def evaluate(system, cases):
"""system: function taking a question string, returning a string.
cases: list of (question, expected, tag) tuples."""
correct = 0
failures = []
for question, expected, tag in cases:
answer = system(question)
if normalize(answer) == normalize(expected):
correct += 1
else:
failures.append((question, expected, answer, tag))
return correct / len(cases), failures
cases = [
("I was charged twice this month.", "billing", "common"),
("The export button crashes the app.", "bug", "common"),
("Do you have a student discount?", "other", "edge"),
("App froze and then billed me again.", "billing", "ambiguous"),
]
def always_other(question):
return "other"
score, failures = evaluate(always_other, cases)
print(f"baseline accuracy: {score:.2f}")
for question, expected, answer, tag in failures:
print(f"[{tag}] expected={expected} got={answer}: {question}")Running this prints a baseline accuracy of 0.25 and three failures. A trivial system sets a floor. Your real system should beat it clearly, and the failure list tells you what to look at next.
Reading the baseline
A score alone is not a decision. Look at the failures and group them by tag. If most failures are wrong facts, you have a knowledge gap. If most are wrong format or tone, you have a behavior gap.
Also note the noise. Model outputs vary between runs, and a small eval set has wide error bars. A change from 0.72 to 0.74 on 50 cases is not evidence of improvement. Set a threshold before you start, for example "keep a change only if it adds at least five points", and run the set more than once when outputs are sampled.
Common errors
- Building the eval set after choosing a technique, so it favors that technique.
- Changing the prompt, the retrieval settings, and the model together, so you cannot tell which change mattered.
- Using only easy cases, which makes every approach look good.
- Treating a judge model's score as ground truth without checking it against human labels.
Exercise
Your eval set has 60 cases. The baseline scores 0.70, and after a prompt change it scores 0.73. You set a rule of "at least five points to keep a change". What do you do, and why?
Show answer
Do not keep the change on this evidence. A gain of three points is below your threshold and is within normal noise for 60 cases. Either add more cases or rerun to see whether the gain is stable, and meanwhile read the failures to see whether the change fixed the failure type you aimed at.