Module 5 · Evaluation & production
RAGAS metrics: faithfulness, context precision/recall
6 min read
Recall and MRR measure retrieval. But the user only sees the answer: is it faithful to the sources, complete, relevant? These qualities cannot be measured by computing over identifiers; the answer must be read — a job given to an LLM judge. The RAGAS library popularized a set of metrics of this kind. This lesson defines them plainly, reimplements them in Prism, and shows how to run RAGAS itself on Claude.
Four metrics, four questions
| Metric | Question | Compares | Failure addressed |
|---|---|---|---|
| Faithfulness | Does the answer invent things? | Answer ↔ passages | Hallucination |
| Context recall | Did retrieval find everything needed? | Reference answer ↔ passages | Not retrieved (2) |
| Context precision | Are the useful passages at the top? | Passages ↔ reference | Badly ranked (3) |
| Answer relevancy | Does the answer address the question asked? | Answer ↔ question | Specificity (6) |
The two context metrics require a reference answer written by a human; faithfulness and relevancy do without one, which makes it possible to compute them on production traffic.
Faithfulness: split, then verify
The principle has two steps: split the answer into atomic claims, then ask the judge whether each one appears in the passages. The score is the share of supported claims.
def faithfulness(answer: str, contexts: list[str]) -> float:
# une citation [ret-01] n'est pas une affirmation
statements = _statements(CITATION.sub("", answer))
verdicts = _supported(statements, contexts)
return sum(verdicts) / len(verdicts) if verdicts else 1.0
Splitting into claims makes the judgment more reliable than a global score ("is this answer faithful, from 1 to 5?"): the judge checks precise facts one by one, and the score can be explained. Removing citations is a detail learned through use (the code comment: a citation like [ret-01] is not a claim): without it, the judge treats "[liv-02]" as an unsupported claim and penalizes answers that cite their sources.
The judge returns its verdicts as structured output; the code guards against a list of the wrong length:
verdicts = llm.structured("judge_support", Verdicts, prompt).supported
return (verdicts + [False] * len(statements))[: len(statements)]
Context recall and context precision
Context recall applies the same mechanism to the reference answer: does each of its claims appear in the retrieved passages? A low score means retrieval missed a needed piece of information — even if article-level recall@5 was perfect (the right article, but not the right passage).
Context precision asks the judge whether each passage is useful, then computes the average precision at the useful ranks:
precisions = [sum(useful[: i + 1]) / (i + 1) for i, u in enumerate(useful) if u]
return sum(precisions) / len(precisions)
A useful passage in first position counts more than a useful passage in fifth position: it is the LLM-judged equivalent of lesson 1.2's MRR and nDCG.
Can you trust the judge?
An LLM judge is itself a model that makes mistakes. Four rules:
- A judge at least as capable as the model being evaluated. Prism uses the large model to judge, even when generation uses the small one.
- Simple, verifiable judging tasks: one claim, yes or no; not a 1-to-10 score on "quality".
- Calibrate against humans: have 50 answers rated by a domain expert and measure agreement with the judge. Without that calibration, a score of 0.85 means nothing.
- Compare rather than read in absolute terms: a faithfulness score is mostly useful to compare two versions of the pipeline on the same question set.
RAGAS on Claude
RAGAS is the reference library for these metrics. Running it on Claude required working around two obstacles, verified with ragas 0.4.3:
- RAGAS imports a module removed from
langchain-community0.4: you must installlangchain-community<0.4. - Its Anthropic connector sends
temperature=0.01andtop_p=0.1. Yet Claude Opus 5.5 rejects anytemperatureparameter, Claude Haiku 5.5 only acceptstemperature=1, and neither accepts both parameters together: every call fails.
The solution is an adapter. RAGAS's recent metrics ask the LLM for a Pydantic object; Prism produces it with Claude's native structured outputs, with no sampling parameter:
class ClaudeRagasLLM(InstructorBaseRagasLLM):
def __init__(self, fast: bool = False) -> None:
# le juge doit être au moins aussi capable que le modèle évalué : grand modèle par défaut
self.fast = fast
def generate(self, prompt: str, response_model: type[T]) -> T:
return llm.structured("ragas", response_model, prompt, fast=self.fast)
async def agenerate(self, prompt: str, response_model: type[T]) -> T:
return await asyncio.to_thread(self.generate, prompt, response_model)
(The comment: the judge must be at least as capable as the model being evaluated, hence the large model by default.) The tests/test_ragas.py test checks that RAGAS's Faithfulness metric runs end to end with this adapter. These workarounds depend on versions: an evaluation library evolves quickly, so pin its version and check again with every upgrade.
RAGAS or homemade metrics? RAGAS brings proven prompts and metrics comparable from one project to another; homemade metrics (about fifty lines in prism/judge.py) are transparent, adaptable to your language and criteria, with no fragile dependency. Both approaches are equally valid if they are calibrated.
The cost of evaluation
Each metric costs several calls per question: splitting, verifying, one call per passage for precision. For 200 questions and four metrics, expect several thousand calls. Use the Batch API (half price, results within a few hours) for full evaluations, and a subsample for quick iterations.
Key takeaways
- Faithfulness (does it invent?), context recall (was everything retrieved?), context precision (well ranked?), answer relevancy (does it address the question?).
- Split into atomic claims, then verify each one: more reliable and more explainable than a global score.
- A capable judge, simple judgments, calibration against humans, comparisons rather than absolutes.
- RAGAS 0.4.3 on Claude:
langchain-community<0.4and an adapter with no sampling parameters.- Evaluation costs money: Batch API for full campaigns, a subsample to iterate.
Practice
- Run
examples/06_evaluation_and_ops.pyand reconstruct the context precision computation from the judge's verdicts. - With an API key, have 20 answers rated by yourself and by the judge (faithfulness): what is their agreement rate?
- Add an
answer_relevancymetric toprism/judge.py: the judge generates the questions the answer addresses, and you measure their similarity to the question asked.