Sub topic 12
Measuring chunking quality with recall@k
This chapter assumes you can read basic Python, know what an embedding and vector similarity search are, and have seen the chunkers from earlier chapters. You will build a small evaluation set and use it to compare chunking settings with numbers instead of intuition.
Why measure
Chunk size, overlap, and splitter choice all change what the retriever returns. Reading a few results by hand will not tell you whether 200 words beats 400. A small evaluation set will.
An evaluation set is a list of test questions. Each question is paired with the part of the source text that answers it. You run every chunking configuration against the same questions and compare scores.
What recall@k means
Recall@k is the fraction of known relevant items that appear in the top k retrieved results. With k = 5, you look only at the five best-ranked chunks.
def recall_at_k(retrieved_ids, relevant_ids, k=5):
if not relevant_ids:
return 0.0
hits = len(set(retrieved_ids[:k]) & set(relevant_ids))
return hits / len(relevant_ids)If two chunks are relevant and one appears in the top 5, recall@5 is 0.5. Average the score over all questions to get one number per configuration.
Recall matters here because the generator can only use what the retriever hands it. A relevant chunk missing from the top k cannot help the answer.
Building the evaluation set
Chunk IDs change whenever you change the chunker, so you cannot label questions with chunk IDs. Label them with something stable, such as a character range or a short answer phrase from the source document.
- Pick 20 to 50 questions that real users would ask.
- For each question, record the exact answer phrase as it appears in the source.
- After chunking, mark a chunk as relevant if it contains that phrase.
def relevant_chunk_ids(chunks, answer_phrase):
return [i for i, c in enumerate(chunks) if answer_phrase in c]A phrase that straddles a chunk boundary appears whole in no chunk, so it matches nothing. That is a real signal. With overlap, the phrase may appear whole in one chunk, and the metric rewards that.
Running the comparison
The loop below chunks once per configuration, indexes the chunks, retrieves for every question, and averages recall@k. It assumes a search(query, chunks, k) function that returns the indexes of the top k chunks. Use your own vector search for it.
def evaluate(chunker, questions, document, k=5):
chunks = chunker(document)
scores = []
for q in questions:
relevant = relevant_chunk_ids(chunks, q["answer_phrase"])
retrieved = search(q["question"], chunks, k)
scores.append(recall_at_k(retrieved, relevant, k))
return sum(scores) / len(scores)Call it once per setting, for example with chunk_words at sizes 100, 200, and 400. Keep the questions, the embedding model, and k fixed. Change one thing at a time.
Reading the results
Recall alone can mislead. Large chunks often score higher recall because each one contains more text, so the answer phrase is more likely to be inside. But large chunks add noise to the prompt and use more of the context budget.
Track at least one more number next to recall, such as average chunk length in tokens or the total tokens sent to the generator. Choose the smallest configuration whose recall is close to the best.
| Setting | recall@5 | Avg. chunk tokens |
|---|---|---|
| 100 words, overlap 20 | 0.62 | 140 |
| 200 words, overlap 40 | 0.78 | 280 |
| 400 words, overlap 80 | 0.81 | 560 |
In this made-up example, going from 200 to 400 words adds 0.03 recall and doubles the tokens per chunk. The 200-word setting is likely the better choice.
Common errors
- Too few questions. With 5 questions, one change moves the score by 0.2. Use enough questions that single results do not dominate.
- Questions copied from the text. If a question reuses the source wording, even weak retrieval looks good. Write questions the way users phrase them.
- Labeling by chunk ID. The labels break as soon as you re-chunk. Use answer phrases.
- Changing several things at once. You cannot tell which change caused the difference.
- Tuning on the test set only. Keep some questions aside to confirm the winning setting.
Exercise
A configuration retrieves 5 chunks for a question that has 2 relevant chunks. Only 1 relevant chunk is among the 5. What is recall@5, and why can't you label questions with chunk IDs when comparing chunkers?
Show answer
Recall@5 is 1/2 = 0.5. Chunk IDs depend on how the document was split, so they change when you change the chunk size, overlap, or splitter. Labels must be tied to something stable, such as an answer phrase found in the source text, and matched against each configuration's chunks.
Exercise
Setting A has recall@5 of 0.84 with 800-token chunks. Setting B has 0.80 with 250-token chunks. Which would you pick for a prompt with a tight context budget, and what would you check before deciding?
Show answer
Setting B is the likelier choice, because it loses 0.04 recall but sends about a third of the tokens per chunk. Before deciding, confirm the gap holds on enough questions (not noise from a small set), and check answer quality on a held-out set of questions.