Skip to content
bits
Retrieval without tears · Lesson 8 of 9

Measuring retrieval

Tutor:

You've now got a lot of dials: chunk size, overlap, embedding model, BM25 or not, contextual chunks, reranking, how many results to keep. Without a number, you're tuning by vibes.

The good news: you can measure retrieval on its own, before Claude writes a word. If the right chunk never reaches the prompt, no prompt can save the answer.

Tutor:

The measure is recall@k. Write down a set of real questions, and for each one, the chunk that answers it. Run your search. Recall@k is the share of questions where that chunk shows up in the top k results.

Twenty questions, right chunk in the top 5 for seventeen of them: recall@5 is 17 ÷ 20 = 0.85. Anthropic's contextual retrieval tests used exactly this, at k = 20, and reported the misses (1 − recall@20).

type Case = { question: string; answerId: string }; // the chunk that answers it
export async function recallAt(
k: number,
cases: Case[],
search: (q: string) => Promise<string[]>, // returns chunk ids, best first
) {
let hits = 0;
for (const c of cases) {
const ids = await search(c.question);
if (ids.slice(0, k).includes(c.answerId)) hits++;
else console.log(`miss: ${c.question}`);
}
return hits / cases.length;
}
TypeScriptUTF-8● lines 9–13: hit if the chunk is in the top k
Tutor:

Run it once per setup, and keep the misses: reading why a question missed teaches you more than the score. Here's the kind of table you're after (illustrative numbers):

OUTPUT
npx tsx eval.ts
setup recall@5 recall@20
embeddings only 0.70 0.85
+ BM25 (hybrid) 0.80 0.90
+ contextual chunks 0.85 0.95
+ rerank top 150 0.90 0.95
illustrative numbers, not a real run
Tutor:

A few tips. Twenty to fifty questions is plenty to start, as long as they're real: pull them from support tickets or search logs. Phrase them like users do, not by copying the chunk's wording, or keyword search will look better than it is. And check answers separately: high recall with bad answers means the problem is in the prompt, not the search.

quick check+20 neurons
Tutor:

You have 10 test questions. The right chunk is in the top 3 for 6 of them, and in the top 10 for 9. What's recall@3?

    Type your answer or tap an option · Enter checks

    quick check+10 neurons
    Tutor:

    Recall@20 is 0.95, but users say the answers are often wrong. Where do you look first?

      Type your answer or tap an option · Enter checks

      Tutor:

      No test questions yet? Claude can draft them. Your turn: ask for five questions a customer might ask that this passage answers, phrased the way customers talk, not copied from the passage.

      Your turn

      Run your prompt and the replies land here.

        • Asks for questions, not yet
        • Sets how many, not yet
        • Asks for customers' own words, not yet
        • Ran at 1.0, not yet
        run at
        ~0 tokens

        Turn on JavaScript to run prompts.