Sub topic 6
Faithfulness: is the answer supported by the context?
This chapter assumes you know how a RAG pipeline works (chunking, embedding, vector search, prompting an LLM). It also assumes you can read Python and that you know precision and recall from classification.
What faithfulness means
Faithfulness, also called groundedness, asks one question: is every statement in the answer supported by the retrieved context? The context is the text the generator actually received in its prompt.
Faithfulness is not correctness. Correctness compares the answer with the real world or with a reference answer. Faithfulness compares the answer only with the retrieved passages. The two can disagree in both directions.
| Case | Faithful? | Correct? | Example |
|---|---|---|---|
| Context is outdated, answer repeats it | Yes | No | Context says "24 months", the warranty is now 36 |
| Model answers from memory, context is silent | No | Yes | The answer is true, but nothing retrieved says it |
| Model contradicts the context | No | Usually no | Context says "24 months", answer says "12 months" |
| Context and answer agree with the facts | Yes | Yes | The goal case |
Faithfulness matters because it isolates the generator. If an answer is unfaithful, the generator added or distorted information, whatever the retriever did.
Where unfaithful answers come from
- Fabrication: the model states a detail that appears nowhere in the context.
- Distortion: the model changes a number, date, or condition.
- Outside knowledge: the model fills a gap from its training data instead of saying the context does not cover it.
- Over-generalizing: the context says "some models" and the answer says "all models".
Measuring it claim by claim
Scoring a whole answer as "faithful" or "not faithful" hides detail. A long answer with one invented sentence is mostly good, and a single yes/no label loses that. The standard approach works in three steps:
- Split the answer into short, self-contained claims.
- Check each claim against the retrieved context.
- Report the fraction of claims that are supported.
The code below implements step 3. The verifier is a parameter, so you can replace the toy judge with an NLI (natural language inference) model or an LLM judge.
def faithfulness_score(claims, context, is_supported):
"""Fraction of claims in an answer that the context supports.
claims: list of short, self-contained statements extracted from the answer.
context: the retrieved passages given to the generator, as one string.
is_supported: function (claim, context) -> bool, for example an NLI model
or an LLM judge prompted to answer only from the given context.
"""
if not claims:
return None # nothing to verify; do not report this as 1.0
supported = sum(1 for c in claims if is_supported(c, context))
return supported / len(claims)
# Toy judge for illustration only: exact substring match.
def toy_judge(claim, context):
return claim.lower() in context.lower()
context = "The warranty lasts 24 months. It covers manufacturing defects."
claims = ["the warranty lasts 24 months", "it covers water damage"]
print(faithfulness_score(claims, context, toy_judge)) # 0.5The first claim appears in the context, and the second does not, so the score is 1/2. An answer with no claims, such as a refusal, returns None. Averaging it in as 1.0 would reward answers that say nothing.
Common errors
- Using the wrong context. Verify against the passages the generator saw, not the ones you wish it had seen. If you re-retrieve for scoring, the result can differ from the original run.
- Claims that are not self-contained. A claim like "it lasts 24 months" cannot be checked without knowing what "it" is. Ask the extractor to resolve pronouns.
- Substring matching in real use. The toy judge fails on paraphrase. Real answers rephrase the context, so you need an NLI model or an LLM judge.
- Treating the score as correctness. A score of 1.0 means the answer matches the context, nothing more.
Exercise
A user asks about a return window. The retrieved passage says "Returns are accepted within 14 days." The answer says "Returns are accepted within 30 days." The company's real policy is 30 days, and the passage is out of date. Is the answer faithful? Is it correct?
Show answer
The answer is not faithful, because the claim "30 days" contradicts the retrieved context, which says 14. It is correct, because it matches the real policy. This shows that faithfulness and correctness are separate measures. The generator used knowledge beyond the context, and the retrieval corpus needs updating.
Exercise
An answer contains four claims. The verifier marks three as supported. What is the faithfulness score, and what should the function return if the answer contained zero claims?
Show answer
The score is 3/4 = 0.75. With zero claims the function should return None (undefined), so the case can be reviewed separately and not counted as a perfect score.