Sub topic 8
Scoring faithfulness claim by claim
This chapter assumes you know the difference between faithfulness and correctness from the previous chapters. It also assumes you can read Python and know what a retrieved context is. A faithful answer is one the retrieved context supports, whether or not it is true in the world.
Why score per claim
A single yes/no judgment on a whole answer hides partial failures. An answer with five statements, one of them invented, would pass a "does the context support this answer?" check about as often as it fails. Splitting the answer into claims turns faithfulness into a fraction you can average, compare, and trace back to the exact unsupported sentence.
The score for one answer is:
faithfulness = supported claims / total claims
The pipeline
There are three steps:
- Extract claims. Ask an LLM to rewrite the answer as short, self-contained statements. Each claim must make sense alone, so "It covers defects" becomes "The warranty covers manufacturing defects." Resolve pronouns during extraction. Otherwise the verifier cannot tell what "it" refers to.
- Verify each claim. Check one claim at a time against the context the generator actually saw. Use an NLI model (natural language inference, which classifies whether a text entails a statement) or an LLM judge told to answer only from the given context.
- Aggregate. Compute the fraction supported. Average across questions for a system-level number.
A working implementation
The verifier is passed in as a function, so you can replace the toy judge with a real one without touching the scoring logic.
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 second claim, "it covers water damage", is not in the context, so the score is 0.5. The substring judge only works because the example is built to match. A real judge must handle paraphrase, so the toy version would mark many faithful claims as unsupported.
Common errors
- Scoring empty answers as 1.0. An answer such as "I don't know" has no claims. The function returns
Noneso you can count refusals separately instead of inflating the average. - Verifying against the wrong text. Use the exact passages the generator received, after truncation and reranking. Checking against the full document hides cases where the supporting text never reached the prompt.
- Claims that are too coarse. A claim like "The warranty lasts 24 months and covers water damage" mixes a supported and an unsupported fact. Extract atomic claims.
- Treating world knowledge as support. A claim can be true and still unsupported. Mark it unsupported. That is what faithfulness measures.
- Trusting extraction blindly. The extractor can drop a bad claim or rephrase it into a supported one. Spot-check extracted claims on a sample of answers.
Reading the result
Keep the per-claim labels, not only the fraction. A score of 0.5 across a set could mean every answer has one bad claim or half the answers are entirely invented. The unsupported claims are also your debugging list. They show what the generator added beyond the context.
Exercise
An answer is split into 4 claims. The verifier marks 3 as supported. A second answer to another question produces no claims because the system said "The documents do not say." What faithfulness score does each answer get, and how should the second be handled in the average?
Show answer
The first answer scores 3/4 = 0.75. The second returns None because there is nothing to verify. Exclude it from the faithfulness average and report refusals as a separate rate, so they neither inflate nor deflate the score.