Skip to content
The AI Agent Factory

19.16 Measuring retrieval

Approved by
Panaversity ·
12 min read

In everyday life. A new pharmacist is tested with a fixed set of prescriptions before she works alone, the same set for every new hire, each with the right answer written down by a senior pharmacist. If the training changes, the same test shows whether it got better or worse.

What you will learn. How a fixed list of questions, written with the expert, measures retrieval on every build, and how a person checks the answers that matter most.

Why it matters. A change that makes retrieval worse can look harmless in review. A fixed set of questions fails the build before any user sees the wrong citation.

One request, start to finish. A builder wants bigger passages, "so the model gets more context". The change is one setting: whole documents instead of one rule each. Before it merges, the build runs a fixed set of questions:

  1. Twelve questions, six per company, written with Ana and Luis: for each company, five that the policies answer and one off-topic question they do not. Each has the passages that should come back first, or "abstain". So 10 questions have an answer and 2 should abstain.
  2. With one passage per rule, every question that has an answer finds its passage in the top three, and every off-topic question abstains.
  3. With whole documents, Cedar Ridge's three documents become three passages, and every citation points at a document's first rule instead of the rule that answers. Three of Cedar Ridge's five answerable questions now fail: found in the top three drops from 10 of 10 to 7 of 10, and the build fails.

The bigger-passage change never merges. The questions said no, before any user saw it.

What is a question set?

A question set is a short, fixed list of questions, each with its expected answer: the file and line that should come back, or "abstain". It is written with the expert, because only the expert knows the right answer. Two measures, in plain words:

  • Found in the top three: for questions that have an answer, how often the right passage is among the first three returned.
  • Abstained when it should: for questions the policies do not answer, how often the tool abstained.

The set runs in the build, with a pass mark (in the lab, every question, with the test model), so a change to chunking, the model or the threshold that makes retrieval worse fails like any other test.

Is the right passage enough?

Finding the right passage is not the same as giving the right answer. The question set checks retrieval: did the right passages come back? It does not check what the model then says. A model can be handed both purchasing lines and still answer Luis's $400 question wrongly, or let AP pay an invoice that is more than its purchase order, forgetting the rule that holds it. So the lab adds a small answer check, written with the AP expert, as in Chapter 6's review contract. Each row has three parts. A citation gives the line in the file, counting the title, the owner's line and the blank lines: in approvals.md, rule 1 sits on line 5 and rule 4 on line 8.

Question (as the company)Expected decisionMust citeMust not claim
Cedar Ridge: "Do we need a PO for a $400 repair?"No purchase order neededpurchasing.md, lines 5 and 6That a purchase order is required
Cedar Ridge: "INV-6206 is $1,523.40, and its purchase order PO-3104 says $1,500.00. Can AP pay it?"Not yet: hold it until the buyer explains the difference. It is under $2,500.00, so it will not need the general managerapprovals.md, line 8That AP may pay it now, or that it needs the general manager
Brightline: "Who approves a $12,000.00 invoice?"The controller, before paymentap-triage-rules.md, line 8Any approver the policies do not name
Cedar Ridge: "What is the capital of France?"The policies do not answer thisNothingAny answer about France

The AP expert reads each answer and marks it pass or fail. A model's wording changes from run to run, so this check is read by a person, at each model or prompt change, not run on every build. The question set guards retrieval on every build. The answer check guards the answers that matter most.

Two rows of five boxes. Top row, the question set, every build, automatic: Cedar Ridge asks about a PO for a $400 repair, the search returns the top three passages, compared with the expected purchasing.md lines 5 and 6, found in the top three, pass. Between the rows, in red-orange: it can pass while the answer is still wrong, the right passages, the wrong decision. Bottom row, the answer check, at each model or prompt change, a person: Cedar Ridge asks whether AP can pay INV-6206, $1,523.40 against its purchase order's $1,500.00, the model answers with citations, the AP lead reads it, checking the decision, the citation and that no forbidden claim is made, pass.

Figure 19.27. Two checks. The question set guards retrieval on every build. The answer check guards the answer.

What happens when the model changes?

Changing the model means embedding again. Embeddings from two different models are not comparable: their numbers mean different things. So every stored embedding carries its model's name, and the search reads only rows from the model it is using. If the connector's model and the stored rows disagree, the tool says so plainly ("The policies were embedded with X, not Y. Tell the builder to run npm run embed.") instead of comparing numbers that mean different things. npm run embed embeds a company's passages again with the current model.

At work. In Lab 19, Task 14, OpenCode writes scripts/retrieval-eval.ts, which loads the question set, asks each question as its company, and prints a table and a pass or fail. Then it loads Cedar Ridge again with whole-document passages, runs the set, shows it failing, and loads it back.

The operating rule: write a question set with the expert, run it in the build with a pass mark, and store the model's name with every embedding. Then check a few answers with the expert too: the decision, the citation, and the claim the answer must not make.

A scorecard of four of the twelve questions, each asked as its company. Cedar Ridge, do we need a PO for a $400 repair, expected purchasing.md lines 5 and 6, found at 1. Brightline, who approves invoices over $12,000, expected ap-triage-rules.md line 8, found at 1. Cedar Ridge, can a vendor change bank details by email, expected vendor-bank-changes.md line 7, found at 1. Brightline, what is the weather in Columbus today, expected abstain, abstained. Below, the whole set run twice. One passage per rule: found in the top three 10 of 10, abstained 2 of 2, passes, with a tick. Cedar Ridge reloaded as whole documents: found 7 of 10, fails, because three of Cedar Ridge's questions now cite a document's first rule, not the rule that answers.

Figure 19.28. The question set, run on every build. A change that makes retrieval worse fails it.

Check yourself

Question 1 / 8

0 answered

A builder changes Cedar Ridge's chunking to whole documents, "so the model gets more context". What does the question set show?

Owner
Panaversity