Sub topic 8
Scoring Faithfulness Claim by Claim
Prerequisites and scope
This chapter assumes you can read Python and know how a RAG pipeline passes retrieved passages to a generator. It also assumes you know the earlier distinction in this tutorial: faithfulness asks whether the answer is supported by the retrieved context, not whether it is true in the world.
By the end you should be able to turn a free-text answer into a faithfulness score, say what each number means, and avoid the usual scoring mistakes.
Why score claim by claim
A single yes/no verdict on a whole answer hides the information you need. Take a four-sentence answer in which one sentence invents a detail. A whole-answer judge often says "faithful" because most of the text matches the context. Or it says "unfaithful" and gives you no sign of how bad the answer is.
Claim-level scoring fixes this. You break the answer into small factual statements, check each one against the context, and report the fraction that the context supports. That gives you three things:
- A graded score between 0 and 1 instead of a coarse pass/fail.
- A list of the exact unsupported claims, which you can read and fix.
- A number that behaves consistently across short and long answers, because it is a ratio.
The score is not a measure of correctness. A claim can be true and still unsupported, for example when the model answers from its training data. For a RAG system that is usually still a failure, because you cannot trace the claim to a source.
The procedure
Step 1: Extract claims
A claim is a short statement that can be checked on its own. The key word is self-contained. "It covers manufacturing defects" cannot be verified without knowing what "it" is. The extractor should rewrite it as "The warranty covers manufacturing defects."
You normally use an LLM for extraction. A good extraction prompt does four things:
- Asks for one fact per claim.
- Asks the model to replace pronouns and vague references with the noun they refer to.
- Tells it to skip greetings, opinions, and statements such as "I could not find that in the documents".
- Demands machine-readable output, so you can parse it without guessing.
import json
EXTRACT_PROMPT = """Split the answer into short, self-contained factual claims.
Replace pronouns with the noun they refer to. Skip opinions, greetings, and
statements about what the context does not say.
Return a JSON list of strings and nothing else.
Question: {question}
Answer: {answer}"""
def parse_claims(raw):
"""Return a list of claims, or None if the model output is unusable."""
try:
data = json.loads(raw)
except json.JSONDecodeError:
return None
if not isinstance(data, list) or not all(isinstance(c, str) for c in data):
return None
return [c.strip() for c in data if c.strip()]
good = '["The warranty lasts 24 months.", "The warranty covers manufacturing defects."]'
bad = "Sure! Here are the claims: the warranty lasts 24 months."
print(parse_claims(good))
print(parse_claims(bad)) # None: the caller must retry or flag this caseReturning None for unparseable output matters. If you silently treat a parse failure as "no claims", the example drops out of your average or scores as perfect. Count parse failures and report them.
Step 2: Verify each claim
For each claim, ask a verifier whether the retrieved context supports it. The verifier can be a natural language inference (NLI) model, which classifies a claim as entailed, contradicted, or neutral given a passage. It can also be an LLM judge prompted to answer using only the context.
Give the verifier the same context the generator saw, with nothing added. If you hand it extra passages, or fewer, you are scoring a different system.
Prefer three verdicts over two:
| Verdict | Meaning | Typical cause |
|---|---|---|
| supported | The context states or clearly implies the claim. | Normal grounded answer. |
| contradicted | The context says something incompatible with the claim. | The generator misread or overrode the source. |
| not_found | The context neither supports nor contradicts the claim. | The model added outside knowledge or guessed. |
Both contradicted and not_found count as unfaithful in the score. Keeping them separate still helps diagnosis. Contradictions point to misreading, and not_found points to the model filling gaps, often because retrieval did not supply what the question needed.
Step 3: Aggregate
The per-answer score is the fraction of claims with the verdict supported. The next example makes the verifier a parameter, so you can replace the toy judge with an NLI model or LLM call without changing the scoring code.
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 substring judge is only a stand-in. Real claims are paraphrases of the context, so exact matching would score almost every real answer near zero.
A richer version keeps the three-way verdict and returns the offending claims, which is what you will actually read when debugging:
CONTRADICTED = {"the warranty lasts 12 months"} # toy knowledge for the demo
def toy_verdict(claim, context):
if claim.lower() in context.lower():
return "supported"
if claim.lower() in CONTRADICTED:
return "contradicted"
return "not_found"
def faithfulness_report(claims, context, verdict_fn):
if not claims:
return None
verdicts = [verdict_fn(c, context) for c in claims]
n = len(verdicts)
return {
"score": verdicts.count("supported") / n,
"contradicted": verdicts.count("contradicted") / n,
"not_found": verdicts.count("not_found") / n,
"unsupported_claims": [
c for c, v in zip(claims, verdicts) if v != "supported"
],
}
context = "The warranty lasts 24 months. It covers manufacturing defects."
claims = [
"the warranty lasts 24 months",
"the warranty lasts 12 months",
"it covers water damage",
]
print(faithfulness_report(claims, context, toy_verdict))This prints a score of one third, with one contradicted claim, one not_found claim, and the two unsupported claims listed by text.
Averaging across a dataset
Once you have a score for each question, you must choose how to combine them. There are two common options.
- Macro average: compute each answer's score, then average the scores. Every question counts equally.
- Micro average: pool all claims from all answers and compute one ratio. Long answers count more.
Neither is wrong, but they answer different questions. Macro tells you how often a typical user gets a well-grounded answer. Micro tells you what fraction of all generated statements are grounded. Pick one, name it in your report, and keep it fixed between runs. Whichever you choose, also report the share of answers with a score of 1.0, since a mean of 0.9 can hide a few completely ungrounded answers.
Common errors
The mistakes below come up often in practice.
- Non-atomic claims. "The warranty lasts 24 months and covers water damage" is one claim with two facts. If half of it is true, a verifier may pass or fail the whole statement, depending on mood. Ask the extractor for one fact each, and spot-check its output.
- Unresolved references. Claims such as "this applies to them" cannot be verified. Check a sample of extracted claims by eye for pronouns.
- Verifying against the wrong text. Use the exact context the generator received, including any truncation your pipeline applied. If the context was cut at the token limit, a claim from the cut part is unsupported, and that is a real finding.
- Confusing faithfulness with correctness. A faithful answer can be wrong if the source is wrong or outdated. An unfaithful answer can be right. Report the two separately.
- Letting the extractor drop the bad claims. An LLM extractor tends to normalise or omit odd statements, which can make the answer look cleaner than it is. Compare claim counts against a hand-labelled sample.
- Ignoring partial support. A claim like "the warranty covers all defects" is only partly supported if the context says "manufacturing defects". Tell the verifier to mark such overgeneralisations as not supported, and include an example of one in its prompt.
Edge cases
- Implied claims. If the context says "24 months" and the answer says "two years", a good verifier accepts the claim. Simple string matching and weak NLI models often reject it. Test your verifier on a few paraphrases and unit conversions before trusting it.
- Multi-passage claims. Some claims need two passages together. Give the verifier all the context at once, not one passage at a time, or you will mark valid inferences as unsupported.
- Long context. Verifiers lose accuracy when the context is very long. If you see this, verify each claim against the top few passages most similar to it, and confirm on a sample that this does not change verdicts.
- Cost. The procedure needs one extraction call and one verification call per claim, or per batch of claims. Batch the claims into a single verifier prompt if the judge handles it reliably, and compare batched and individual verdicts on a sample.
The score is only as trustworthy as the verifier behind it. The later chapters on judge bias and on checking a judge against human labels cover how to test that, and how to attach a confidence interval to the dataset-level mean.
Exercise
An answer produces five claims. The verifier marks three as supported, one as contradicted, and one as not_found. (a) What is the faithfulness score? (b) A second answer is "I couldn't find that in the provided documents." What should the score be, and why? (c) Why is a claim like "It covers manufacturing defects" a problem for the verifier?
Show answer
(a) 3/5 = 0.6. Both the contradicted and the not_found claim count as unsupported. (b) It should be None (undefined), because the answer contains no factual claims. Scoring it 1.0 would reward refusing to answer, so evaluate refusals with a separate metric. (c) The pronoun "It" has no referent on its own, so the verifier cannot check the claim. The extraction step should rewrite it as a self-contained claim, such as "The warranty covers manufacturing defects."