Skip to the lesson
FixTheRobot Explore Robotics / LLM / LM9 Evaluation, Inference and Reliability / LM9-T2

LLM · Theory · LM9

LM9-T2 — Task Evaluation and Test Sets

Decide what the model must do, collect examples of exactly that, and fix the way you will score them before you look.

  • Lesson 2 of 6
  • Module 9: Evaluation, Inference and Reliability
  • Theory
  • Difficulty: Advanced
  • About 7 minutes to read
  • Prerequisite: LM9-T1

A language model has no single score. What counts as good depends on the use: a robot assistant that must produce a valid command, a summariser that must not invent facts, a classifier that must not miss hazards. This lesson turns that observation into a method: derive the metrics from the application, build a test set that reflects it, split development from final test, compute how large the set must be, and avoid the habits that spoil evaluations. Lab LM9-L2 builds a small harness along these lines.

Learning objectives

After this lesson, you should be able to:

  • Derive metrics and failure costs from an application description.
  • Build a test set with task types and slices, separate development and final test sets.
  • Compute the number of items needed for a target margin of error.
  • List the habits that invalidate an evaluation (peeking, tuning on the test set, shifting the metric).

The idea: the test set is a specification

A test set says what you promise the model can do. Build it from the application. List the kinds of requests the system will receive: common ones, rare ones, hard ones, malicious ones. For each, say what a good response is and what a bad one costs. A wrong command that moves a robot is worse than a refusal; a missing hazard report is worse than a false alarm. That gives you the metrics and the weights.

From application to test set to metricA pipeline of four boxes: the application and its failure costs; a list of task types and slices; a test set built from real requests with references or criteria; a metric and a decision rule fixed in advance. An arrow from the test set to a locked box marks that the final test set is used once.Applicationwho uses it, what a failure costs→Task types and slicesnormal, edge, adversarial, rare→Test setreal requests, references or criteria→Metric + rulefixed before running Design the evaluation from the use, not from the model final test set: used once, at the end↓
The evaluation is designed from the application. The final test set is used once.

Sources of items

  • Real requests (logs, operator notes, support tickets), with consent and privacy handling. The best match to reality.
  • Written by experts for coverage of rare and risky cases.
  • Synthetic (generated by templates or by another model), cheap and large, useful for coverage and regression tests, risky as the only source since they inherit the generator’s narrowness.
  • Public benchmarks, good for comparison with others, possibly contaminated and often off-task (Lesson LM7-T5).

Slices

Tag each item with the properties that might matter: task type, length, language, source, difficulty, safety category. Report the metric per slice as well as overall (Lesson LM6-T5). A good test set has enough items in every slice that matters.

Splits

Keep three sets apart. The training set is for fitting. The development (validation) set is for choosing prompts, settings and checkpoints; it is used again and again, so it becomes mildly overfitted. The final test set is used once for the number you report. Any decision taken after looking at the final test set (change the prompt, drop a bad item, choose a different metric) turns it into a development set. If you have to peek, build a new final set.

Metrics

  • Exact match / pass rate against criteria for tasks with checkable answers (commands, labels, extraction, arithmetic).
  • Constraint checks for format and rules: valid structure, allowed actions, length, units.
  • Reference-based similarity (n-gram overlap, embedding similarity) for text with a reference, to be treated as a rough guide.
  • Human or model judgement with a rubric for open-ended quality, validated against humans on a sample.
  • Outcome metrics in a simulator or on the real system: did the robot reach the goal, violate a constraint?

The mathematics: how many items, how to split

To estimate an accuracy p to within a margin m (half-width of the 95% interval), you need about

items for a target margin
n≈(1.96m)2⋅p(1−p)n\approx (\frac{1.96}{m})^{2}\cdot p(1-p)
m
desired half-width of the 95% interval (for example 0.03 for ±3 points).
p
the expected accuracy; if unknown use 0.5, the worst case, which gives n ≈ 0.96/m².
n
number of independent test items needed.

For p = 0.8: ±5 points needs 246 items, ±3 points needs 683, ±2 points needs 1,537. For p = 0.9 and ±2 points: about 865. Per slice the same formula applies: a slice with 30 items has a margin of about ±14 points at p = 0.8, too coarse for a decision. If you must compare two systems, the margin of the difference matters; a paired design (the same items for both) needs fewer items (Lab LM9-L1).

Stratified splitting. To keep each slice represented in the development and test sets, split within each slice: a slice with ns items gives round(f · ns) items to the test set at test fraction f, with at least a minimum count per slice. Rare slices may need to be oversampled or collected separately.

Decision rule. Write the rule before running: “accept if the pass rate is at least 0.95 and the lower end of the 95% interval is at least 0.90, with zero failures in the safety slice.” A rule fixed in advance cannot be bent by the result.

Multiple comparisons. If you test k variants and report the best, the chance that at least one beats a baseline by luck at the 5% level is 1 − 0.95k: 23% for k = 5, 40% for k = 10. Correct for it, or confirm the winner on fresh data.

A worked example: sample sizes and a stratified split

The code computes the item counts above, the margin of a small slice, the chance of a lucky winner among several variants, and splits a tagged set into development and test sets within each slice.

import math
import random

def items_needed(margin, p=0.5):
    return math.ceil((1.96 / margin) ** 2 * p * (1 - p))

print(items_needed(0.05, 0.8), items_needed(0.03, 0.8), items_needed(0.02, 0.8))   # 246 683 1537
print(items_needed(0.02, 0.9), items_needed(0.05))                                   # 865 385

def margin(p, n):
    return 1.96 * math.sqrt(p * (1 - p) / n)
print(round(margin(0.8, 30), 3), round(margin(0.8, 683), 3))                         # 0.143 0.03

for k in (1, 5, 10):
    print(k, round(1 - 0.95 ** k, 2))                                                # 0.05 0.23 0.4

def stratified_split(items, test_fraction, min_test=2, seed=0):
    rng = random.Random(seed)
    by_slice = {}
    for it in items:
        by_slice.setdefault(it["slice"], []).append(it)
    dev, test = [], []
    for name in sorted(by_slice):
        group = by_slice[name][:]
        rng.shuffle(group)
        ntest = max(min_test, round(test_fraction * len(group)))
        test += group[:ntest]
        dev += group[ntest:]
    return dev, test

items = [{"id": i, "slice": s} for i, s in enumerate(["normal"] * 60 + ["edge"] * 25 + ["adversarial"] * 10 + ["refusal"] * 5)]
dev, test = stratified_split(items, 0.4)
count = lambda xs: {s: sum(1 for x in xs if x["slice"] == s) for s in ("normal", "edge", "adversarial", "refusal")}
print(count(dev), count(test))    # {'normal': 36, 'edge': 15, 'adversarial': 6, 'refusal': 3} {'normal': 24, 'edge': 10, 'adversarial': 4, 'refusal': 2}

The last line shows that the rare slices end up with only 2 to 4 test items even at a 40% test fraction. That is far too few for a number: for such slices, collect more items rather than trust a percentage.

A test plan as a table

Example test plan for a robot command assistant
SliceExampleMetricPass rule
Normal commands“Go to the dock.”valid, correct command≥ 95%, lower bound ≥ 90%
Edge casesambiguous or incomplete requestsasks a clarifying question or refuses≥ 90%
Adversarialforged system text, injectionignores the injected instruction100% on the test set (no failures allowed)
Refusalsrequests outside the allowed actionsrefuses100%

Engineering implications and limitations

Write the test plan first. Slices, sizes, metrics and the decision rule, then collect the data. It stops the evaluation from being fitted to the model.

Lock the final set. Store it read-only, with a hash, and record every time it is used. If it has been used for decisions, retire it and build a new one.

Keep a regression suite. Every failure found in production becomes a test item (in the development set), so that it cannot silently return.

Review items as carefully as code. Wrong references and ambiguous items make good models look bad. Have a second person check a sample of references.

Do not tune the prompt on the test set. Prompt engineering is training; do it on development data (Module 11).

Automate and version. One command that runs the harness, writes the report and records the model, data and code versions. Manual evaluations are not reproducible.

Include the cost of errors. If a false positive costs 1 and a false negative costs 20, report the expected cost, not only the accuracy.

For robots. Combine text-level checks (valid command, allowed action) with outcome checks in simulation and on hardware tests, and always keep a safety slice with zero tolerance for specified failures.

Common misconceptions

“A benchmark score is the evaluation.”

It is one measurement on someone else’s task; the evaluation is what you design from your application.

“Tuning on the test set is fine if I only change small things.”

Every choice made after looking at it leaks information; the number is no longer a clean estimate.

“More items always means a better test set.”

Items must represent the use and be correct; many redundant or wrong items add nothing.

“A 100% score on 20 items means a 100% model.”

With 20 items the 95% upper bound on the error rate is about 14% (the rule of three: 3/n).

“Overall accuracy covers every user.”

Slices with few items can be much worse; report them and their sizes.

Engineering takeaways

  1. Derive metrics and failure costs from the application; list task types and slices; build the test set from real requests where possible.
  2. Keep training, development and final test sets apart; use the final test once; any decision made after peeking turns it into development data.
  3. Items needed for a margin m: n ≈ (1.96/m)² p(1 − p): 246, 683 and 1,537 for ±5, ±3 and ±2 points at 80%.
  4. Fix the metric and the decision rule before running; correct for trying many variants (1 − 0.95^k).
  5. Stratify the splits, give rare slices enough items, add production failures to a regression suite.

References and further reading

See Chapter 7 of the book (evaluating the finetuned LLM). The sample-size formula is the standard normal approximation for a proportion; the test plan is an illustration, not a standard.

Knowledge check

Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

You expect 90% accuracy and want a ±4-point margin. About how many items are needed?

Explanation

n = (1.96/0.04)² × 0.9 × 0.1 = 2,401 × 0.09 ≈ 216 items.

Question 2

You compare 8 prompt variants on your final test set and report the best. Roughly how likely is it that at least one beats the baseline at the 5% level by luck alone?

Explanation

1 − 0.95⁸ = 1 − 0.663 = 0.34, about one in three. Confirm the winner on fresh data or correct for multiple comparisons.

Question 3

A rare but critical slice has 4 test items and the model gets 4 of 4 right. What can you conclude?

Explanation

Very little. With 4 items the 95% upper bound on the error rate is about 3/4 (the rule of three); collect many more items for that slice before claiming reliability.

What’s next

Next: sampling variability and reproducibility: how much a result moves from run to run, and how to report it.

Practice this lesson

Build an Evaluation Harness

Write a repeatable evaluation harness with criteria, a pass rate and failure listing, and find the prompt-sensitive cases.

Next lesson

LM9-T3 — Sampling Variability and Reproducibility

Measure run-to-run variation, set seeds and report confidence intervals.

Nothing below is required to finish this lesson.