Skip to content

How to Evaluate RAG System Quality

Briefly

Prototyping a RAG is easy, but understanding whether it performs well is harder. Here’s what to measure during the search and generation phases, how to evaluate responses using an LLM, and how to integrate metrics into the pipeline.

Why Evaluate Separately

RAG consists of two related but distinct parts:

  1. Search — how well the relevant document fragments are found;
  2. Generation — how well the model responds based on the found fragments.

A poor response could be due to bad search or bad generation. To know what to fix, you need to measure both parts.

flowchart LR
    A[Question] --> B[Search]
    B --> C[Generation]
    C --> D[Response]
    B -.->|search metrics| E[Evaluation]
    C -.->|generation metrics| E

Search Metrics

Evaluate whether the retriever has found the relevant fragments.

Metric What it shows
Recall@k Is the correct fragment among the top-k
Hit Rate Proportion of questions where a relevant fragment is found
MRR How high up in the ranking the first relevant fragment is

The rule is simple: first, aim for good Recall@k, then optimize generation. If search doesn’t find facts, no model can answer correctly.

Generation Metrics

Evaluate the quality of the response along three axes:

  • Faithfulness (groundedness) — the response is based on the found fragments and doesn’t make up information;
  • Answer Relevance — the response actually answers the question;
  • Context Relevance — the found fragments are relevant to the question.

Groundedness is the most important: it catches hallucinations.

Evaluation with LLM

Human evaluators are accurate but expensive and slow. In practice, often use an LLM judge: a strong model evaluates responses based on criteria.

Prompt for evaluating groundedness:

Below is the question, the found context, and the model’s response.
Evaluate the groundedness of the response: how much each claim
in the response is supported by the context.

Rating from 1 to 5:
1 — the response is almost entirely made up
5 — everything is supported by the context

Question: {question}
Context: {context}
Response: {answer}

An LLM judge isn’t perfect, but with clear criteria, it correlates well enough with humans for monitoring.

RAGAS — Ready Framework

RAGAS automates evaluation: calculates faithfulness, answer relevancy, and context relevancy using an LLM.

pip install ragas
from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevancy, context_precision
from datasets import Dataset

samples = Dataset.from_dict({
    "question": ["What is the warranty on the motor?"],
    "answer": ["The warranty is 2 years."],
    "contexts": [["Motor X has a 2-year warranty."]],
})

result = evaluate(samples, metrics=[faithfulness, answer_relevancy, context_precision])
print(result)

How to Build a Test Set

Evaluation is useless without good examples. Collect questions from real practice:

  • questions from your chat logs and widgets;
  • typical support queries;
  • edge cases: off-topic questions, with typos, with multiple conditions.

Each question needs a benchmark fragment (for search metrics) or at least a benchmark response (for comparison).

Integration with Langfuse

To keep metrics alive, integrate them into Langfuse: it stores each request, response, and found context, and evaluations can be linked to traces.

This gives a dynamic picture: quality drops → visible on which questions → clear what to fix.

Practical Order

  1. Collect 50–100 real questions.
  2. Measure Recall@k — fix search if it’s low.
  3. Measure faithfulness — fix generation if the model makes up information.
  4. Integrate evaluation into the pipeline and monitor the dynamics.

Checklist

  • have a test set of real questions;
  • search metrics are calculated (Recall@k, MRR);
  • groundedness of responses is measured;
  • evaluation is run regularly, not just once;
  • results are visible in observability.

See Also