News / Learn / Retrieval-augmented generation (RAG)
How do you evaluate a RAG system?
You'll be able to design a test set and metrics that show whether your RAG system retrieves the right text and answers correctly.
In plain English
A RAG system has two jobs. It must find the right passages, and then it must write an answer that the passages support. Evaluation means measuring each job separately. You build a set of questions with known good answers and known source passages, run the system on them, and score the results.
Why it matters
A RAG demo can look good on five hand-picked questions and still fail on real traffic. Without measurements, you cannot tell whether a change to chunking, the embedding model, or the prompt made things better or worse. A fixed evaluation set lets you compare versions with evidence instead of impressions.
How it works
Start with a dataset. Each item holds a question, the reference answer, and the document passages that contain the answer. Collect these from real user queries when you can. Synthetic questions generated from your documents are a useful supplement, but review them, because generated questions often differ from what users actually ask.
Score retrieval first. For each question, check whether the relevant passages appear in the top k results you pass to the model. Common metrics are recall at k, which asks whether the right passage was retrieved at all, and precision at k, which asks how much of what was retrieved is relevant. Mean reciprocal rank and normalized discounted cumulative gain also reward putting relevant passages higher. If retrieval recall is low, no prompt change will fix the answer.
Score generation second. Given the retrieved context, check whether the answer is correct and whether every claim is supported by that context. The second check is called faithfulness or groundedness. An answer can be correct by accident and still be ungrounded, and that matters for trust. Scoring can use human reviewers, exact or fuzzy matching against reference answers for short facts, or an LLM acting as a judge with a clear rubric. If you use an LLM judge, check its agreement with a human sample before you rely on it.
Run these evaluations on every change, and keep the dataset fixed so results stay comparable. Add new failure cases as you find them in production. A small set you trust beats a large set nobody checks.
A concrete example
A minimal retrieval check: for each test item, see whether a known relevant document ID appears in the top k results.
def recall_at_k(test_items, retrieve, k=5):
hits = 0
for item in test_items:
results = retrieve(item["question"], top_k=k)
retrieved_ids = {r["doc_id"] for r in results}
if item["relevant_doc_id"] in retrieved_ids:
hits += 1
return hits / len(test_items)
# Compare two configurations on the same fixed set
# score_a = recall_at_k(test_items, retrieve_v1, k=5)
# score_b = recall_at_k(test_items, retrieve_v2, k=5)A short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Evaluating only the final answer Measure retrieval and generation separately. If you only score answers, you cannot tell which component caused a failure.
- Testing on questions you wrote after seeing the documents Build the test set from real or realistic user questions. Questions written by someone who has read the source text tend to match its wording too closely.
- Changing the test set between runs Freeze the dataset for each comparison. Add new cases in a separate set so scores stay comparable over time.
- Trusting an LLM judge without checking it Have a human label a sample of outputs and measure how often the judge agrees. Revise the rubric until agreement is acceptable.
Going deeper
Study retrieval metrics such as recall at k and mean reciprocal rank, then learn faithfulness and answer relevance scoring. Read about building regression suites for prompts and tracking evaluation results over time in your deployment pipeline.
FAQ
How many test questions do I need?
Start with a few dozen that cover your main query types and known failure cases. Grow the set as you learn where the system breaks. Quality and realism matter more than raw count.
Should I use retrieval metrics or answer metrics first?
Retrieval first. If the right passages are not in the context, the model cannot give a grounded answer, so fixing retrieval usually gives the biggest gain.
Can I generate the test set with an LLM?
Yes, it speeds up the work. Review generated questions and answers by hand, because generated items can be easy, repetitive, or wrong about the source.
What is faithfulness?
Faithfulness checks whether each claim in the answer is supported by the retrieved context. An answer can be correct but unfaithful, which is a risk when users rely on the cited sources.
How often should I run evaluations?
Run them on every change to chunking, embeddings, retrieval settings, or prompts, and on a schedule against production samples to catch drift.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.