Sub topic 14
Measuring whether a context change helped
This chapter assumes you can read short Python and have already built a context with selection, trimming, or compaction. Those techniques change what the model sees. This chapter covers how to check whether a change made the output better.
Why impressions mislead
A new context often feels better after you try two or three prompts. Model output varies from run to run, and you tend to test the examples you just fixed. A change can help one question and quietly break another. You need a fixed set of cases and a score you can compare before and after.
Build a fixed test set
A test set is a list of questions with known good outcomes. Keep it small enough to run often, with 20 to 50 cases. Include these kinds of cases:
- Common questions that real users ask.
- Cases that failed before, such as a clash between old and new instructions.
- Cases that need information from far back in the history.
- Cases where the right answer is "the context does not say".
Do not tune the context against these cases and then report the same score as proof. Keep some cases hidden until you are ready to decide.
Choose what to score
Pick checks that a program can run. Exact-match or "contains" checks work for short factual answers. For open-ended answers, use a rubric and a second model as a grader, and spot-check its grades by hand. Also record cost, because a context change can improve quality and raise tokens at the same time.
| Measure | What it tells you |
|---|---|
| Pass rate | Whether answers are correct more often |
| Average prompt tokens | Whether the change costs more or less |
| Cases fixed and cases broken | Whether gains hide regressions |
| Latency | Whether a bigger context slows replies |
A comparison harness
The code below runs a baseline context builder and a candidate on the same cases. The fake_model function stands in for a real model call so the example runs as shown. Replace it with your provider's client.
DOCS = {
"refund": "Refunds are issued within 14 days.",
"shipping": "Shipping takes 3 to 5 days.",
}
CASES = [
{"question": "How long do refunds take?", "expected": "14 days"},
{"question": "How long does shipping take?", "expected": "3 to 5 days"},
]
def estimate_tokens(text):
return max(1, len(text) // 4)
def fake_model(prompt):
# Stand-in: echoes any known fact that appears in the prompt.
return " ".join(line for line in prompt.splitlines() if line in DOCS.values())
def evaluate(build_prompt, call_model, cases):
passed, total_tokens = [], 0
for case in cases:
prompt = build_prompt(case["question"])
answer = call_model(prompt)
passed.append(case["expected"].lower() in answer.lower())
total_tokens += estimate_tokens(prompt)
return passed, total_tokens / len(cases)
def compare(baseline, candidate):
base_pass, base_tokens = baseline
cand_pass, cand_tokens = candidate
pairs = list(zip(base_pass, cand_pass))
return {
"baseline_rate": sum(base_pass) / len(base_pass),
"candidate_rate": sum(cand_pass) / len(cand_pass),
"fixed": [i for i, (b, c) in enumerate(pairs) if c and not b],
"broken": [i for i, (b, c) in enumerate(pairs) if b and not c],
"token_change": cand_tokens - base_tokens,
}
baseline = lambda q: "\n".join([DOCS["refund"], q])
candidate = lambda q: "\n".join([DOCS["refund"], DOCS["shipping"], q])
result = compare(
evaluate(baseline, fake_model, CASES),
evaluate(candidate, fake_model, CASES),
)
print(result)The candidate fixes case 1 (the shipping question), breaks nothing, and adds tokens. That output supports adopting the change, if the added cost is acceptable.
Common errors
- Reading small differences as real. With 20 cases, one flipped answer moves the rate by 5 points. Run each case several times when the model is non-deterministic, and treat small gaps as noise.
- Looking only at the average. A higher pass rate can hide a broken case that matters most. Always read the "broken" list.
- Changing two things at once. If you add retrieval and compact history together, you cannot tell which one helped. Change one thing per run.
- Testing on short sessions only. Failures such as clash and distraction appear late in long sessions. Include long-history cases.
Exercise
A candidate context raises the pass rate from 70% to 75% on 20 cases, but the "broken" list contains one case that used to pass. What should you do before adopting it?
Show answer
Inspect the broken case to learn why it failed, then rerun both versions several times to check whether the 5-point gain is more than noise. Adopt the change only if the gain holds and the regression is understood or fixed.