Why Retrieval Is Measured Separately
Split every RAG failure into a retrieval problem or a generation problem, see how a fluent answer can hide missing evidence, and learn what a retrieval evaluation needs and why it comes first.
Contentsand lesson path
On this page
- Why it matters
- Two stages, two questions
- A fluent answer can hide missing evidence
- What a retrieval evaluation needs, and why it comes first
- Check your understanding
- Apply it
- Review prompt
Lessons of RAG Retrieval Evaluation
- Why Retrieval Is Measured Separately Concept · 10 min
- Precision@k and Recall@k by Hand and in Code Worked example · 20 min
- Building a Labeled Query Set Lab · 20 min
- Lab: Compare Two Retrieval Configurations Lab · 25 min
- Rank-Aware Metrics and the Limits of Labels Concept · 15 min
- Check: Compute and Compare Retrieval Metrics Knowledge check · 10 questions
What you will learn
- Define precision@k and recall@k from labeled data.
Prerequisites: RAG Foundations
01. Why it matters
A retrieval-augmented generation (RAG) system answers in two steps: a retriever picks a few passages from your documents, then a model writes an answer from them. Users only ever see the answer, so it is tempting to judge only the answer. That hides the one thing you most need to know when quality drops: which of the two steps went wrong.
This skill teaches you to measure the retrieval step on its own, with labelled queries and simple metrics, before you look at answers. This lesson explains why the separation pays off. The next lesson computes the first two metrics, precision@k and recall@k, and the rest of the skill builds a labelled query set, compares two retrievers on it, and looks at what the numbers can and cannot tell you.
02. Two stages, two questions
For any answer you review, ask two questions in order. First: did the passages sent to the model contain the evidence the answer needs? Second: given those passages, is the answer correct and faithful to them? The two yes-or-no answers give four outcomes:
- ok: the evidence was retrieved and the answer is right.
- generation: the evidence was retrieved, but the model misread it, ignored it or added to it. Look at the prompt, the way the context is laid out, or the model.
- retrieval: the evidence never reached the model. Look at chunking, the embedding model, the query, k or the search method; no prompt change can fix this.
- ungrounded: the answer is right, but no retrieved passage supports it. The model answered from its training data or guessed. Count this as a failure too: the answer cannot be cited, and it breaks as soon as the facts are private, new or changed.
Sorting needs one extra piece of data per query: which documents hold the evidence. With that label, the check is mechanical.
"""Sort reviewed answers by the stage that failed."""
def failure_stage(retrieved: list[str], relevant: set[str], answer_ok: bool) -> str:
evidence_found = bool(relevant & set(retrieved))
if answer_ok:
return "ok" if evidence_found else "ungrounded"
return "generation" if evidence_found else "retrieval"
traces = [ # query, top 3 retrieved, labelled relevant, answer judged correct?
("charged twice for the same order", ["d01", "d13", "d02"], {"d01", "d13"}, True),
("pdf invoice download", ["d07", "d09", "d08"], {"d03"}, False),
("when is a double charge refunded", ["d01", "d13", "d02"], {"d01", "d13"}, False),
("which iOS version does the app need", ["d10", "d14", "d06"], {"d11"}, True),
]
for query, retrieved, relevant, answer_ok in traces:
print(f"{failure_stage(retrieved, relevant, answer_ok):<11} {query}")ok charged twice for the same order
retrieval pdf invoice download
generation when is a double charge refunded
ungrounded which iOS version does the app needThe four traces are made up to show one outcome each, but the document ids are the 14 help-centre articles from the hybrid search lab in Vector Search Foundations (d01 is the refund policy for duplicate payments, d03 the invoice download page, d11 the supported app versions). The second query failed before the model saw anything: the invoice article was not in the top 3, so no prompt would have produced a grounded answer. The third failed after retrieval: the refund article reached the model and the answer still got the timing wrong. Counted together as "wrong answers", they would leave you guessing which component to fix.
failure_stage() treats one relevant document in the context as enough evidence. When an answer needs two documents, a stricter check asks for all of them, which is exactly what recall@k measures in the next lesson.
03. A fluent answer can hide missing evidence
Question: When will I get my money back for a double charge?
Retrieved: d02 Change the card used for your subscription ...
d03 Download past invoices as PDF files ...
d07 Export reports to CSV or Excel ...
Answer: Duplicate charges are normally refunded to your card within
5 to 10 business days. You can track the refund under Billing.Nothing in the answer signals a problem. It is fluent, specific and close to the help centre's real policy, which promises the money back within five days (article d01). Yet none of the three passages mentions refunds: the model filled the gap with general knowledge about card payments, and its second sentence describes a feature that does not exist. A reviewer reading only the answer, or an LLM judge asked whether it is helpful, may well pass it.
Language models are trained to produce plausible text, and plausible is not the same as supported. Stronger models tend to write more convincing answers when retrieval fails, so judging answers alone can get worse at revealing retrieval problems as models improve. Comparing the retrieved ids with labels catches this case whenever the labels are right, because it never reads the answer.
04. What a retrieval evaluation needs, and why it comes first
A retrieval evaluation needs three things: realistic queries; labels that list, for each query, the documents that contain the evidence (its relevant documents); and the ranked ids your retriever returns for each query. Metrics then compare the top k results with the labels. The two basic metrics come straight from that comparison. Precision@k is the share of the top k results that are relevant: if 2 of the top 4 are relevant, precision@4 = 2/4 = 0.5. Recall@k is the share of the query's relevant documents that appear in the top k: if the query has 3 relevant documents and 2 of them are in the top 4, recall@4 = 2/3, about 0.67. Precision tells you how much of the context is noise; recall tells you how much of the evidence the generator never sees. The next lesson computes both by hand and in code.
This setup has four properties that answer-level evaluation lacks:
- Cheap and fast. No LLM writes or judges anything: you run the retriever, which costs at most one embedding call per query, and compare the ids with the labels. Hundreds of queries take seconds, so you can try many chunk sizes, embedding models, values of k or hybrid settings in an afternoon.
- Deterministic. The same retriever, index and labels give the same number on every run, so a change of 0.05 means the retriever changed. Scores from an LLM judge or human raters vary from run to run.
- An upper bound. If the evidence is missing from the top k for 30% of queries, no prompt can make those answers grounded. Fixing retrieval first means the answer evaluation that follows measures the generator, not the retriever.
- Pointed at a component. If recall drops after you re-chunk the corpus, you know which change to undo.
Two cautions. Relevant means "contains evidence needed to answer", not "about the same topic", and not "scored highly by the retriever". A similarity score only says how close two vectors are, and a nearest-neighbour search returns its best-scoring results even when nothing in the corpus answers the question. And retrieval metrics do not replace answer evaluation: a perfect retriever can still feed a generator that fails. You need both, in that order; the AI evaluation skill that follows this one covers the answer side.
05. Check your understanding
06. Apply it
Start the practice project for this skill. Collect 10 real questions that your RAG system has received, from logs, support tickets or a colleague, rather than questions you write while reading the documents. For each one:
- Record the ids of the documents that contain the answer. Search the corpus yourself; do not just copy what the retriever returned, or you will only ever label its successes.
- Run your system and save the top k retrieved ids and the answer.
- Mark the answer correct or not, then sort the query into ok, generation, retrieval or ungrounded with
failure_stage().
Count the four outcomes and keep the file. It becomes the first draft of the labelled query set you build properly in lesson 3.
07. Review prompt
Without looking back:
- Which two questions do you ask about a reviewed answer, in which order, and which four outcomes do they give?
- Why does a correct answer with no supporting passage count as a failure?
- Give three reasons to measure retrieval before you evaluate answers.
- What does relevant mean in a retrieval label, and why is a high similarity score not the same thing?
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
10 questions · about 12 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.