Simple definition#
Evaluation is measuring how well your AI system does its job, on a fixed set of test cases, with repeatable scores. For RAG you measure two things separately: did we find the right information (retrieval), and did we use it correctly (generation)?
What to evaluate#
flowchart LR
Q["Question"] -->|"Context relevance:<br/>is the retrieved context<br/>relevant to the question?"| C["Retrieved context"]
C -->|"Faithfulness / groundedness:<br/>is every claim in the answer<br/>supported by the context?"| A["Answer"]
A -->|"Answer relevance:<br/>does it address<br/>the question?"| Q
A -->|"Correctness:<br/>does it match the<br/>reference answer?"| REF["Reference answer"]
The golden dataset#
A golden dataset (evaluation set) is a list of test cases:
| Field | Example |
|---|---|
| Question | “Can contractors claim home-office equipment?” |
| Reference answer | “No. Only employees on permanent or fixed-term contracts may claim, up to €500 per year.” |
| Relevant document/chunk IDs | policy-equipment-2026#3.1 |
| Metadata | Category: HR; difficulty: medium; type: single-hop |
How to build it:
- Start with real questions from users, support tickets, search logs or subject-matter experts.
- Cover the space — easy and hard, single- and multi-hop, every major document type, questions with no answer in the corpus, adversarial inputs.
- Synthetic generation — an LLM generates question–answer pairs from chunks; experts review a sample. Fast for coverage, but check realism.
- Size — 50–100 cases to start, growing to several hundred. Add every production failure.
- Version it like code.
Retrieval metrics#
Given the known relevant chunks for each question:
| Metric | Question it answers | Formula, simply |
|---|---|---|
| Recall@k | Did the relevant chunks appear in the top k? | relevant found in top k ÷ total relevant |
| Precision@k | How much of the top k is relevant? | relevant in top k ÷ k |
| Hit rate@k | Was at least one relevant chunk in the top k? | % of questions with a hit |
| MRR (mean reciprocal rank) | How high is the first relevant result? | average of 1 ÷ rank of first relevant |
| nDCG@k | Are the most relevant results at the top? | rank-weighted gain, normalised to the ideal order |
Example. Relevant chunk found at rank 3 → reciprocal rank 1/3. Over three questions with first-hit ranks 1, 3 and “not found”: MRR = (1 + 0.33 + 0) / 3 = 0.44.
Recall@k is the most important for RAG: if the right chunk is not retrieved, nothing downstream can fix it.
Generation metrics#
| Metric | What it measures | How it is computed |
|---|---|---|
| Faithfulness / groundedness | Are the answer’s claims supported by the retrieved context? (hallucination check) | LLM splits the answer into claims and checks each against the context |
| Answer relevance | Does the answer address the question? | LLM judge, or similarity between the question and questions generated from the answer |
| Context precision | Are the relevant chunks ranked at the top? | LLM or labels judge each retrieved chunk |
| Context recall | Does the context contain everything needed for the reference answer? | Check reference-answer claims against context |
| Answer correctness | Does it match the reference? | LLM judge or semantic similarity to reference |
| Citation accuracy | Do citations point to sources that support the sentence? | Rule check + LLM judge |
| Format / policy compliance | Length, tone, required disclaimers, no PII | Rules, regex, classifiers |
These are the metrics implemented by frameworks such as RAGAS, DeepEval, TruLens, Arize Phoenix, LangSmith and Langfuse. Spring AI has RelevancyEvaluator and FactCheckingEvaluator for tests; see Spring AI in production.
LLM-as-a-judge#
Using a strong LLM to grade outputs against a rubric. It scales human-like judgement to thousands of cases. Judges have known biases — position, verbosity, self-preference — covered in evaluation methodology.
flowchart LR
DS[("Golden dataset")] --> SUT["System under test<br/>RAG app, version N"]
SUT --> OUT["Answers + retrieved context"]
OUT --> J["Judge LLM<br/>rubric + few-shot examples"]
DS --> J
J --> S["Scores + reasons<br/>per case"]
S --> R["Report vs baseline<br/>pass / fail gate"]
Make judges reliable:
- Specific rubrics — “Score 1 if every claim is supported by the context, else 0”, not “rate quality 1–10”.
- Binary or small scales with explanations before the score.
- Calibrate — compare judge scores with expert labels on 50–100 cases; aim for high agreement.
- Known biases — position bias (prefers first option), verbosity bias (prefers longer), self-preference (prefers its own model family). Mitigate with swapped order, length-neutral rubrics and a different model family as judge.
- Pairwise comparison (A vs B) is often more reliable than absolute scores when comparing versions.
Evaluation in the lifecycle#
flowchart TB
subgraph Offline["Offline — before release"]
O1["PR changes prompt / chunking / model"] --> O2["Run eval suite in CI"]
O2 --> O3{"Scores ≥ baseline<br/>on all key metrics?"}
O3 -->|"yes"| O4["Merge + deploy canary"]
O3 -->|"no"| O5["Block + investigate"]
end
subgraph Online["Online — in production"]
N1["Sample live traces"] --> N2["Judge: faithfulness,<br/>relevance, safety"]
N3["User feedback,<br/>escalations"] --> N4["Dashboards & alerts"]
N2 --> N4
N4 --> N5["Failures → new<br/>golden test cases"]
end
O4 --> N1
N5 --> O1
Also run A/B tests for business outcomes (resolution rate, time saved, customer satisfaction) — the metrics that justify the investment.
Human evaluation#
Automated metrics do not replace experts for high-stakes domains. Use human review to build the golden dataset, calibrate judges, review samples of production traffic, and sign off releases for regulated use cases. Keep review guidelines written and consistent.
Common mistakes#
Review questions#
How do you evaluate a RAG system? Build a golden dataset of real questions with reference answers and relevant chunk IDs. Measure retrieval (recall@k, MRR, nDCG) and generation (faithfulness, answer relevance, correctness, citation accuracy) separately, using LLM-as-judge calibrated against human labels. Run it in CI on every change, and monitor sampled production traffic online with user feedback.
What is faithfulness? Whether every claim in the answer is supported by the retrieved context — the main hallucination metric for RAG. It is computed by splitting the answer into claims and verifying each against the context.
What is the difference between context precision and context recall? Context precision asks whether retrieved chunks are relevant and ranked at the top. Context recall asks whether the retrieved context contains all the information needed for the reference answer.
What are the risks of LLM-as-a-judge? Position, verbosity and self-preference biases, inconsistent scoring with vague rubrics, and cost. Mitigate with precise binary rubrics, explanations, order swapping, a different model family, and calibration against human labels.
How do you prevent regressions when changing a prompt or model? Version prompts and datasets, run the evaluation suite in CI against the current baseline, block on drops in key metrics, roll out with canaries, and monitor online.
Summary#
- Evaluate retrieval and generation separately, plus end-to-end correctness and business outcomes.
- Retrieval: recall@k, precision@k, MRR, nDCG. Generation: faithfulness, answer relevance, context precision/recall, citation accuracy.
- LLM-as-judge scales evaluation; calibrate it and control its biases.
- Run offline evaluation in CI and online evaluation on production samples; turn failures into test cases.
References#
- Es et al., RAGAS: Automated Evaluation of Retrieval Augmented Generation, 2023.
- Zheng et al., Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena, 2023.
- Järvelin & Kekäläinen, Cumulated gain-based evaluation of IR techniques (nDCG), 2002.