Learn › Testing & Evaluating AI Agents and RAG › Scoring RAG: Retrieval vs Generation

Scoring RAG: Retrieval vs Generation

Intermediate 🕐 17 min Lesson 7 of 11
What you'll learn
  • Separate retrieval metrics from generation metrics
  • Explain context precision, context recall, faithfulness, and response relevancy
  • Diagnose whether a bad answer came from retrieval or generation
  • State what an LLM-judged metric requires to run
  • Plan reference answers for the most important tasks

Two Halves, Two Kinds of Failure

A RAG pipeline has a retrieval half and a generation half, and they fail differently. If you only grade the final answer, you cannot tell which half to fix, and a common waste of effort is rewriting prompts when the retriever is the problem, or the reverse. The Ragas library is one popular open-source toolkit that scores each half separately.

Retrieval Metrics

  • Context precision — whether the relevant chunks are ranked above the irrelevant ones; the LLM version needs a reference answer
  • Context utilization — the same idea judged against the response instead of a reference
  • Context recall — what fraction of the claims in the reference answer are supported by the retrieved context (needs a reference)
  • Context entities recall — what share of the entities in the reference appear in the retrieved context

Low recall means the retriever is missing information. Low precision means it is returning noise that crowds the useful text.

Generation Metrics

  • Faithfulness — the fraction of claims in the response that are supported by the retrieved context; no reference needed
  • Response relevancy — how well the response addresses the question; Ragas generates artificial questions from the response and compares them to the original (needs an LLM and an embedding model)
  • Noise sensitivity — how often the response makes incorrect claims when given relevant or irrelevant context; lower is better

Ragas also documents non-LLM and ID-based variants that compare retrieved passages or chunk IDs to reference ones, which are cheaper and deterministic when you have labeled sources.

Reading the Scores

  • Low recall: fix retrieval first (chunking, hybrid search, reranking, more content)
  • Good recall, low faithfulness: the model has the facts and is drifting; tighten the prompt, require citations, or lower randomness
  • Good faithfulness, low relevancy: the answer is grounded but off-topic; check how the question is rewritten or routed
  • Low precision: too much noise in the context; reduce top-k, rerank, or filter
  • High faithfulness but wrong answer: the retrieved text itself is wrong or outdated; this is a content problem

A worked diagnosis, with illustrative numbers: your suite shows 91% faithfulness but only 64% correctness. The model is faithfully repeating what it retrieved, so generation is not the issue. Context recall is 58%, so the right information is missing from the context in many cases. Work on retrieval.

What You Will Need

Ragas scores these metrics with an LLM, so you need an API key for a supported provider. The documentation example uses an OpenAI key from platform.openai.com; this is required, and there is no key-free option for the LLM-judged metrics. Because the scores come from a model, calibrate them against a few human-labeled examples, as in the judge lesson.

from openai import OpenAI
from ragas.llms import llm_factory
from ragas.metrics.collections import Faithfulness

llm = llm_factory("gpt-4o-mini", client=OpenAI())
scorer = Faithfulness(llm=llm)
result = scorer.score(
    user_input="When was the first super bowl?",
    response="The first superbowl was held on Jan 15, 1967",
    retrieved_contexts=["The first AFL-NFL World Championship Game was played on January 15, 1967."],
)
print(result.value)

The Ragas docs describe two API generations and say the older ragas.metrics style, built around SingleTurnSample, will be deprecated. They show no single version number, so check the docs for the version you install before copying code.

Reference Answers Cost Effort

Several retrieval metrics need a reference answer, which means someone must write one per task. Start with faithfulness, which needs none, and add reference-based metrics for your most important tasks. Even 30 well-written references are enough to see whether retrieval changes help.

Try This

Score 20 of your test cases with at least one retrieval metric and one generation metric. Sort the failures into "retrieval problem" and "generation problem" and count each. Decide which half to fix first, change one thing, and score again to confirm that the right half improved.

Key takeaways
  • Score retrieval and generation separately
  • Low recall points at retrieval; low faithfulness with good recall points at generation
  • High faithfulness with wrong answers means the sources are wrong
  • LLM-judged metrics need an API key and should be calibrated
  • Check current docs for API changes before copying code
Learn more every week Get 5 hand-picked prompts + new AI tools every Monday. Free.