Sub topic 1
What a RAG System Does and Where Quality Can Be Measured
Prerequisites
This chapter assumes you know the basic parts of a retrieval-augmented generation (RAG) pipeline: chunking, embedding, vector search, and prompting an LLM. It also assumes you can read short Python snippets and know precision and recall from classification. No metrics are computed here. This chapter sets up the map that later chapters fill in.
What a RAG system does
A RAG system answers a question using documents it retrieves at query time, instead of relying only on what the language model memorized in training. The flow has two connected parts:
- The retriever takes the question and returns a ranked list of passages (chunks) from your corpus.
- The generator is an LLM that receives the question plus those passages in its prompt and writes an answer.
Each part can fail on its own, and a failure in the first part often looks like a failure in the second. A model that gives a wrong answer may have been handed the wrong passages, or it may have been handed the right ones and ignored them. Evaluation exists to tell these cases apart.
Where quality can be measured
Every RAG request leaves behind four artifacts: the question, the retrieved passages, the answer, and optionally a reference answer or a set of passages a human marked as relevant. Each measurement compares two or more of these.
| Question you are asking | Compares | Needs labels? | Covered in |
|---|---|---|---|
| Did retrieval return the relevant passages? | Retrieved passages vs. labelled relevant passages | Yes, relevance labels | Precision@k, recall@k, MRR, nDCG |
| Is the retrieved context useful for this question? | Question vs. retrieved passages | No, if a judge is used | Context relevance |
| Is the answer supported by what was retrieved? | Answer vs. retrieved passages | No | Faithfulness |
| Does the answer address what was asked? | Question vs. answer | No | Answer relevance |
| Is the answer true? | Answer vs. a reference answer | Yes, reference answers | Correctness |
Note the difference between the last two rows and faithfulness. Faithfulness (also called groundedness) checks the answer against the retrieved context, not against the world. An answer can be faithful to a passage that is itself wrong or out of date. An answer can also be correct but unfaithful, if the model recalled the right fact from training and the context did not contain it. For a RAG system, an unfaithful answer is usually a problem even when it happens to be correct, because you cannot trace it to a source.
Why measure the parts separately
A single end-to-end score, such as "the answer was rated good or bad," hides where the problem is. Suppose 30% of answers are bad. If the retriever misses the relevant passage in 25% of those cases, improving the prompt will not help much. If the retriever succeeds but the generator adds unsupported claims, changing the chunk size will not help.
Measuring both sides gives you a way to assign each failure to a component:
- Check whether the relevant passages were retrieved at all.
- If they were, check whether the answer is supported by them.
- If the answer is supported but still wrong, suspect the corpus or the question's labels.
The rest of this tutorial follows that order. You will first build a labelled evaluation set, then measure retrieval, then measure the generated answer, and finally combine the two to diagnose failures and report results with confidence intervals.
Exercise
A RAG system answers "How long is the warranty?" with "36 months." The retrieved passage says "The warranty lasts 24 months." Which of these is the problem: poor retrieval, an unfaithful answer, or both? Explain which measurement would detect it.
Show answer
The answer is unfaithful: it states 36 months, but the retrieved passage says 24. Retrieval looks fine, since the relevant passage was returned. A faithfulness check of the answer against the retrieved context would catch it. A retrieval metric such as recall@k would not, because it only looks at which passages were returned.
Exercise
Why is an answer that is correct but not supported by the retrieved passages still a concern in a RAG system?
Show answer
The model produced it from its training memory, not from your documents. You cannot trace it to a source, it may be outdated, and the same behavior can produce wrong answers on other questions. Faithfulness measures this separately from correctness.