Papa Programmer
Lesson 09 of 11

Testing a context change

9 min read

Optional

Why spot checks mislead

You tweak the instructions, try two questions, and both look better. You ship it. A week later a customer finds that questions about sale items now get the wrong answer. Nothing you tried touched that case.

This is the normal result of testing by feel. A context has many parts, and a change to one can shift how the model uses the others. The fix is the same one used for any software: a set of tests that you run every time.

Build a fixed question set

An Evaluation setA list of tasks, each with a way to check the result, that you run again after every change to an agent. You measure how often the agent gets each task right. is a list of questions with the answers you expect. For a context, it should hold three kinds.

  • Common questions, the ones that make up most of your traffic.
  • Edge cases, such as the sale-item exception, a question with no answer in the documents, and a request that tries to override your rules.
  • Past failures. Every wrong answer you fix becomes a permanent test, so it cannot return.

Twenty to fifty questions is a good start. Write the expected answer as something a program can check, such as a fact that must appear, a source that must be cited or a phrase that must not appear. The Lesson Measuring answer faithfulness covers checking whether answers stay within their sources.

Keep the set fixed while you change the context. If you change both at once, you cannot tell which one moved the score.

Remove one part at a time

An AblationA test that removes one part of a system, such as one part of the context, and measures how the score changes. It shows which parts earn their place. test removes one part of the context and measures what happens. If the score barely moves, the part is not earning its space. If it drops sharply, you have learned that part matters, and which questions depend on it.

The program below runs this test. Its answer function is a stand-in for a model call. It picks the sentence that shares the most words with the question, ignoring small words and comparing the first four letters of each. That is crude, but it lets the example run anywhere and shows the method. In your own project you would replace it with a request to your model.

python
STOP = {"how", "many", "long", "does", "is", "the", "a", "to", "of", "can", "i", "do", "have"}

def clean(text):
	return {word[:4] for word in text.lower().replace("?", "").replace(".", "").replace(":", "").split() if word not in STOP}

def answer(question, sentences):
	best = max(sentences, key=lambda sentence: len(clean(question) & clean(sentence)), default="")
	return best if clean(question) & clean(best) else "I could not find that"

tests = [
	("How many days to return a bike?", "30 days"),
	("How many days to return a sale item?", "14 days"),
	("How long does shipping take?", "3 to 5"),
	("How long is the battery warranty?", "2 year"),
]

def passed(sentences):
	return {question: expected in answer(question, sentences) for question, expected in tests}

parts = {
	"policy": ["Unused bikes can be returned within 30 days.", "Sale items can be returned within 14 days."],
	"shipping": ["Standard shipping takes 3 to 5 business days."],
	"warranty": ["The battery has a 2 year warranty."],
	"history": ["Customer: Hello, I ordered last week."],
}

def flatten(parts, skip=None):
	return [sentence for name, sentences in parts.items() if name != skip for sentence in sentences]

print("all parts:", sum(passed(flatten(parts)).values()), "of", len(tests))
for name in parts:
	print(f"without {name}:", sum(passed(flatten(parts, name)).values()), "of", len(tests))

It prints:

text
all parts: 4 of 4
without policy: 2 of 4
without shipping: 3 of 4
without warranty: 3 of 4
without history: 4 of 4

With every part present, all four questions pass. Removing the shipping text costs one question, and so does removing the warranty text. Removing the policy costs two, because two questions depend on it. Removing the history costs nothing, so in this test it is dead weight and can go. In a real system, a result like that is a prompt to ask whether the history ever helps.

Catch the harmless-looking edit

Now compare two versions. A colleague adds an older version of the returns rule to the policy "for completeness". It looks harmless. Run the same tests before and after.

python
STOP = {"how", "many", "long", "does", "is", "the", "a", "to", "of", "can", "i", "do", "have"}

def clean(text):
	return {word[:4] for word in text.lower().replace("?", "").replace(".", "").replace(":", "").split() if word not in STOP}

def answer(question, sentences):
	best = max(sentences, key=lambda sentence: len(clean(question) & clean(sentence)), default="")
	return best if clean(question) & clean(best) else "I could not find that"

tests = [
	("How many days to return a bike?", "30 days"),
	("How many days to return a sale item?", "14 days"),
	("How long does shipping take?", "3 to 5"),
]

policy = ["Unused bikes can be returned within 30 days.", "Sale items can be returned within 14 days.", "Standard shipping takes 3 to 5 business days."]
changed = ["Unused bikes can be returned within 14 days."] + policy

for question, expected in tests:
	before = expected in answer(question, policy)
	after = expected in answer(question, changed)
	flag = "  <- changed" if before != after else ""
	print(f"{before!s:<5} -> {after!s:<5} {question}{flag}")

It prints:

text
True  -> False How many days to return a bike?  <- changed
True  -> True  How many days to return a sale item?
True  -> True  How long does shipping take?

The score for the bike question flips from pass to fail, and the other two stay the same. A spot check on shipping or sale items would have shown nothing wrong. Only the full set caught it, and the flagged line says which question to look at. The cause is a clash between two versions of one rule, one of the faults in How context goes wrong.

Read the failures

A total score hides the reason. After a run, open each failing case and read the exact context that was sent. Group the failures by cause: a missing fact, a wrong fact, a clash, noise, a format error. This is Error analysisExamining and grouping model failures to find patterns and decide what to investigate or change next., and it tells you what to fix. Three failures from one cause are worth more than ten random ones.

Repeat, and track cost

Model replies vary from run to run. For a real model, run each question a few times and look at the share that pass, not a single result. Record the tokens each version uses, as well as the score. A change that adds 2,000 tokens to every request for a gain of one question is rarely worth it, as the Context is a budget Lesson explains.

Make it a habit

Keep the question set in your repository. Run it on every change to the instructions, the examples, the retrieval settings or the model. Write down the score next to each change. Over time that log is the history of what worked.

Fixed questionsA set of known answers, run on every change, beats trying a few chats.
Remove one partAn ablation test shows which parts earn their space and which are dead weight.
Read the failuresGroup failing cases by cause, and fix the cause that repeats most.

Your reading position is saved

0%

0 of 11 complete

Contents

Learning tools