How do you test retrieval quality before building a RAG application?
How we test retrieval before any model writes an answer: a labelled question set from real questions, hit rate at k, chunking and threshold tuning, and off topic controls. Grounded in the 187 question audit of the search on this site.
Contents9 sections
Test retrieval on its own, before any model writes an answer. Collect a few dozen real questions from the people who will use the system, label the passage that answers each one, and measure how often that passage lands in the top k results. Tune chunking and a similarity threshold against that number, add off topic controls, and add generation only once retrieval passes.
This is the order we follow on retrieval work. The search on this site is the retrieval layer of a RAG system with no generation behind it, and the first note covers how it works. This one covers how we measured whether it works, using a 187 question audit of that search.
Why retrieval is tested before generation
A RAG system fails quietly. When retrieval ranks the wrong passage first, the language model still writes a fluent paragraph, and that paragraph is what the user sees. Judging generated answers by eye mixes two problems together, and the cheaper one to fix is almost always underneath. Almost every RAG failure we have debugged was a retrieval failure: the right passage was never chunked in a way that could match, or a threshold tuned on a sample folder fell apart on the real corpus. So we test retrieval against the actual corpus first, and we measure precision before anyone touches a prompt.
Start with a labelled question set
The question set is the whole method. Everything after it is tuning against a number, and the number is only as good as the questions behind it. Four rules decide whether it is worth anything.
- Use real questions from support tickets, emails, search logs and the people who will use the system. Questions an engineer writes while reading the documents borrow their vocabulary and score better than real ones will.
- Keep the voice of the asker. Short and long, vague and specific, a few with typos. A real user types "do u guys do rag chatbots", and the set should contain questions like it.
- Label every question with the passage or passages that answer it, and mark the ones the corpus cannot answer at all. Those are worth keeping because they show what retrieval does when there is nothing to find.
- Add controls. A handful of off topic questions that should return nothing at all.
For the search on this site we wrote a bank of 187 questions in the voice of a CTO, a founder and an operations owner, a few with typos, plus five off topic controls such as "weather tomorrow" and "who won the world cup". Each question was tagged with the sections that answer it, and each result got one of four verdicts.
- Answered: a top three result genuinely answers it.
- Weak: related results, none of which answers it.
- Missed: nothing above the threshold, or only unrelated results.
- Not on site: the corpus holds no answer, so retrieval cannot be expected to find one.
The automatic check marked a question answered when an expected section was in the top three. Then every row was read by hand against the passage text, and seven of the before verdicts were corrected. A question about hallucination matched only the introduction to a section about where AI projects stall, which the script counted as a hit and a person counted as weak. The script tells you where to look. Reading the passages tells you what happened.
Measure whether the right passage is in the top k
Two numbers do most of the work. Hit rate at k is the share of answerable questions where at least one correct passage appears in the top k results. Precision at k is the share of those top k results that are relevant. Hit rate tells you whether the answer can be found at all. Precision tells you how much noise surrounds it, which starts to matter once a language model has to read everything you hand it.
Pick k for whoever reads the results. Our audit scored the top three, because a person scanning result cards rarely reads further, even though the search returns five. A generation step reads whatever fits in its context, so k for RAG is usually larger, and every irrelevant passage is another chance for the model to quote the wrong thing. The harness itself is small.
type Case = { question: string, expected: string[] | null }
// expected is null for an off topic control, which should return nothing.
// Questions the corpus cannot answer are reported apart, outside the hit rate.
// The threshold belongs to one model and one corpus. Set your own.
async function evaluate(cases: Case[], k = 3, threshold = 0.35) {
let hits = 0, answerable = 0, precisionSum = 0, controlLeaks = 0
for (const c of cases) {
const results = (await search(c.question))
.filter((r) => r.score >= threshold)
.slice(0, k)
const expected = c.expected
if (expected === null) {
if (results.length > 0) controlLeaks += 1
continue
}
answerable += 1
const relevant = results.filter((r) => expected.includes(r.source)).length
if (relevant > 0) hits += 1
precisionSum += relevant / k
}
return {
hitRate: hits / answerable,
precisionAtK: precisionSum / answerable,
controlLeaks,
}
}Run it on every change to the chunker, the embedding model, the threshold or the corpus, and keep the result for each question as well as the totals. A total that rises while one important question falls is a regression the total hides.
Tune chunking against the number
Chunk size is a retrieval parameter. A chunk that covers six topics is a weak match for a question about one of them, because its embedding blends everything in it. We measured this directly on an earlier version of the site search. The AI integration service description said plainly that we read PDF and DOCX files, yet "Can you build something that reads our PDFs?" scored 0.28 against it, under the 0.35 threshold. The PDF sentence on its own scored 0.41. A version of the chunk with only the body text still scored 0.29, which ruled out the title and stack tags as the cause. A dedicated shorter passage carrying the same published claim scored 0.42, and the question found its answer.
The settings that matter, in the order we try them:
- Split by structure first. Start a new passage at every heading, so a chunk is about one section, and prefix the heading so the passage carries its context. Our chunker does this for the pages and notes on this site.
- Then cap the size. Sentences are packed into a passage of up to about 300 tokens, using a token estimate we checked against the model's own WordPiece tokenizer, with a mean error under three percent, so long sections split on sentence boundaries instead of in the middle of a thought.
- Then add overlap. Up to 40 tokens carried from one passage into the next keeps a claim that straddles a boundary findable from both sides. The multilingual document system in SYS/RAG-01 chunks with overlap for the same reason, and its parser keeps tables and images so a clause inside a table survives to be chunked at all.
- Then write targeted passages where a measured question still misses. Each one may restate a claim the source already makes and may never add a new one.
Rerun the whole set after each change. In our audit, adding one sentence about testing to the passage for the final process step lowered its score for "What happens after the project launches?" from 0.49 to 0.46, so testing got a passage of its own instead. Chunking changes move other questions, and only a full run shows which.
Set a threshold so a weak match becomes an honest miss
Without a threshold, retrieval always returns something, and a confident something is the most dangerous answer it can give. With one, a question the corpus cannot answer comes back empty and the interface can say so. The search on this site drops anything below 0.35 cosine similarity and tells the visitor that nothing cleared the threshold: an honest miss, shown as a miss.
Do not copy that number. A threshold belongs to one embedding model on one corpus. Set it from your own question set: compare the scores of the correct passages for answerable questions with the top scores for the controls, and draw the line where it keeps the first group and drops the second. When the two groups overlap, the fix is usually in chunking, and moving the line only hides it.
Check languages and off topic controls
Controls are the questions that prove the threshold works. Our five off topic questions returned nothing in both runs of the audit, and they stay in the set for every run after it, because a change that makes retrieval more generous can make it generous everywhere.
A multilingual corpus needs its own slice of the question set. SYS/RAG-01 serves retrieval over an archive of PDF and DOCX files in several languages, embedded with a multilingual model. For a corpus like that, ask each labelled question in every language the users write in, including questions whose answer sits in a document written in another language, and score each language separately. A blended hit rate can look healthy while one language quietly fails. Run the controls in each language too, since scores do not always carry over between languages.
What the audit changed
Every question in the bank ran before and after the passage changes. The index grew from 77 to 110 chunks. Every new passage restated a claim already published on the section it links to, and no question that was answered before was anything else after.
| Verdict | Before | After |
|---|---|---|
| Answered | 86 | 146 |
| Weak | 12 | 1 |
| Missed | 50 | 1 |
| Not on site | 39 | 39 |
The 39 questions marked not on site stayed that way on purpose. Retrieval cannot find an answer the corpus does not hold, and writing one is a decision about the content, made in the copy first. The one remaining miss was the useful lesson. "What is the similarity threshold" found nothing because 0.35 sat inside one long list in the first note, which is indexed as a single chunk. It is the same dilution problem in a new place, and the fix is the same: give the long block passages of its own. Writing this note was the fix in practice. With it indexed, its threshold section ranks first for that question at 0.48.
Only then add generation
Once hit rate and precision are measured and acceptable on real questions, generation is the smaller job. Every answer carries the passages it was built from, and a grounding check makes the model abstain when no source supports a claim. The question set becomes the start of the evaluation harness that tells you whether a prompt change made things better or worse. Later upgrades are a reranker when precision is the weak number, and a hybrid keyword and vector score once exact terms such as product codes start to miss.
How we approach this for a client
For a client, this testing happens inside a Discovery Sprint: about two weeks, at a fixed price agreed before we start. Retrieval runs against your real documents and is measured before anyone touches a prompt. At the end you have an architecture document with the measured results, the evaluation plan and the risks in order, and a working spike of retrieval over your documents in a repository you own. You keep both whether or not we continue.
Baraqix Engineering. Written by the engineers who did the work.
NOTE/06. First published .