How to test RAG faithfulness in CI

John Ennis · · 4 min read

Problem

Your RAG assistant adds a detail the documents never said, and the answer still sounds confident and correct.

Outcome

Each case carries its retrieved passages, a judge scores how many of the answer's claims they support, and a drop fails the pull request.

To test RAG faithfulness, give each test case the passages your retriever would return, ask the model the question, and score how much of its answer those passages actually support. Drift-CI's faithfulness evaluator does this claim by claim on every pull request, so a prompt edit or model swap that makes the assistant start inventing details fails the check before it ships.

Why RAG answers drift from their sources

Retrieval-augmented generation is meant to keep answers grounded: the model reads your documents and answers from them. In practice it fills gaps. Asked about returns, it says "within 30 days, and we'll cover return shipping" when the policy says nothing about shipping. The answer reads well, and a similarity check against a reference answer may even pass it.

The risk grows with every change around the model. A shorter system prompt drops the "only use the context" instruction. A new model is more eager to be helpful. A prompt tweak that improves tone loosens grounding as a side effect. None of these show up unless something checks the answer against the passages it was given.

Test RAG faithfulness with the context field

Put the retrieved passages in the case's context and add the faithfulness evaluator:

- id: refund-window-grounded
  input: How long do I have to return an item?
  context:
    - Items can be returned within 30 days of delivery.
    - Refunds go back to the original payment method.
  evaluators: [faithfulness]

Drift-CI numbers the passages and puts them in front of the question, so the model receives:

Answer using the context below.
 
<context>
[1] Items can be returned within 30 days of delivery.
[2] Refunds go back to the original payment method.
</context>
 
How long do I have to return an item?

context can be a list of passages or a single string. With a messages conversation, the context goes in front of the last user message.

How the faithfulness score works

faithfulness sends the answer and the context to a judge model, which lists the factual claims in the answer and marks each one as supported by the context or not. The score is the share of claims that are supported:

  • An answer whose every claim appears in the passages scores 1.0.
  • "Items can be returned within 30 days, and return shipping is free" makes two claims, one unsupported, and scores 0.5.
  • An answer with no factual claims, such as "I don't have that information", scores 1.0.

The reason in the report lists up to three unsupported claims, so the PR comment shows what the model made up, not just that the score fell. A case that lists faithfulness without context fails with an evaluator error rather than scoring anything.

Configure the judge in .drift/config.yaml, and use a different model from the one under test:

judge:
  provider: anthropic
  model: claude-sonnet-4-6

Cases worth writing

A handful of well-chosen cases catches most grounding problems:

  • Answer is in the context. The basic case: the model should answer, using only what's there.
  • Answer is partly in the context. Ask something the passages cover only half of. This is where models fill gaps.
  • Answer isn't in the context. The model should say it doesn't know. Faithfulness scores that 1.0; add llm-judge with criteria such as "says the information isn't available" to make sure it doesn't dodge the questions it can answer.
  • Context contradicts common knowledge. If your policy differs from what's typical, put that in the passages. A model leaning on its training data instead of your documents will say the typical thing and lose the claim.

Faithfulness tells you the answer stays within the sources, not that it's a good answer. Combine it with llm-judge or cosine-similarity against an expected answer when completeness matters too; the case score combines the evaluators.

What it doesn't test

Each case carries fixed passages, so this tests generation, not retrieval. It won't tell you that your retriever found the wrong documents, ranked them badly or missed one. For retrieval metrics such as contextual precision and recall, use a dedicated tool alongside Drift-CI; the LLM evaluation tools comparison covers which ones do that well.

The passages are also wrapped in Drift-CI's own context format, not your application's prompt template. Keep your real system prompt in systemPrompt, so the model gets the same instructions it gets in production.

Running it in CI

Run the suite on every pull request with the Drift-CI GitHub Action and accept the baselines once the answers are right. Faithfulness scores come from a judge, so they move a little between runs; the guide's section on thresholds explains how to leave enough slack. The Drift-CI repository's RAG example combines similarity cases with faithfulness ones.

For agents that call tools before they answer, see testing LLM tool calls. Prompt regression testing shows the pull-request check these cases feed into.

FAQ

Does this test my retriever?
No. Each case carries fixed passages, so the test checks what the model does with the context it's given: whether it stays within it. Retrieval quality, such as whether the right passages were found, needs separate metrics; tools such as Ragas and DeepEval cover those.
What does an answer with no facts score?
1.0. Faithfulness only penalises unsupported claims, so "I don't have that information" is fully faithful. Use a case whose context doesn't contain the answer to check the model admits it, and add llm-judge if you also need the answer to be helpful.
Which model does the judging?
The judge configured under judge in .drift/config.yaml, or the model under test if none is set. Drift-CI warns when the judge and the tested model are the same provider, because models tend to grade their own answers generously.

Catch LLM regressions before they ship

Run your evals on every pull request and fail the build when behaviour drifts past your threshold.