Prompt
How do I test groundedness in RAG responses?
Latest observation
Testing groundedness in RAG means checking whether the answer is supported by the retrieved context and does not introduce unsupported claims.
Here’s a practical way to do it.
1) Define what “grounded” means for your app
Usually a response is grounded if:
- Every factual claim is supported by the retrieved passages
- The answer does not rely on outside knowledge when it shouldn’t
- If the context is insufficient, the model says so instead of guessing
You may also want to distinguish:
- Supported: directly stated in context
- Entailed: can be reasonably inferred from context
- Unsupported / hallucinated: not found in context
2) Test at the claim level, not just the full answer
Split the answer into individual claims and judge each one against the retrieved context.
Example answer:
“The policy applies to full-time employees, requires manager approval, and excludes contractors.”
Check each claim:
- applies to full-time employees
- requires manager approval
- excludes contractors
This is much better than scoring the whole paragraph as one unit.
3) Use a reference-free evaluation prompt
A common method is to ask an LLM to judge whether each claim is supported by the retrieved context.
Example judge prompt:
Given the retrieved context and the answer, determine whether each factual claim in the answer is supported by the context only.
Return JSON with:
- claim
- verdict: supported / unsupported / ambiguous
- evidence: quoted text from context
- reason
This works well for automated tests.
4) Measure groundedness with simple metrics
Useful metrics include:
- Claim support rate = supported claims / total claims
- Hallucination rate = unsupported claims / total claims
- Abstention correctness = when context is insufficient, did the model refuse to answer?
- Citation precision = of cited passages, how many actually support the claim?
- Citation coverage = how many claims have valid citations?
A simple score could be:
groundedness = supported claims / all factual claims
5) Build test cases that target common failure modes
Create a dataset with:
- Questions answerable from context
- Questions with missing context
- Questions with conflicting context
- Questions with irrelevant retrieved docs
- Questions where the answer must say “I don’t know”
This helps catch:
- Overconfident guessing
- Misleading retrieval
- Citation laundering
- Partial support presented as full support
6) Use synthetic adversarial cases
Add tests where the retrieved context contains:
- Similar but incorrect facts
- Old versions of policies
- Negations
- Numbers, dates, names, and thresholds
These are where RAG systems often hallucinate.
Example:
- Context says: “The refund window is 14 days.”
- Answer says: “The refund window is 30 days.” This should fail groundedness.
7) Combine retrieval quality and generation quality
A bad answer may be due to:
- Retrieval failure
- Context selection failure
- Generation failure
So test separately:
- Retrieval recall: did you fetch the right evidence?
- Grounded generation: did the answer stick to that evidence?
If retrieval is poor, groundedness will often fail even with a good model.
8) Add citation validation if your RAG uses citations
If the model cites sources, verify that:
- The citation exists
- The cited span contains the claim
- The citation is not fabricated
- The cited source is among retrieved docs
A response with citations is not automatically grounded; the citations must actually support the statement.
9) Human review for a gold set
LLM judges are useful, but create a small human-labeled set for calibration:
- Supported
- Partially supported
- Unsupported
- Ambiguous
Then compare automatic scores against human labels.
10) A practical evaluation pipeline
A simple production pipeline:
- Retrieve top-k passages
- Generate answer
- Extract factual claims from answer
- For each claim, judge support from context
- Compute groundedness score
- Fail the test if score is below threshold
Example pass/fail rule:
- 100% of critical claims must be supported
- At least 90% of factual claims supported overall
- No unsupported numeric/date claims allowed
11) Tools and frameworks
You can implement this yourself or use evaluation frameworks such as:
- RAGAS
- TruLens
- DeepEval
- LangSmith evals
- promptfoo for test automation
These often provide groundedness, faithfulness, and answer relevance metrics.
12) A good minimal test prompt
If you want a quick starting point, use this judge instruction:
Evaluate whether the answer is fully supported by the provided context.
Mark any sentence that contains information not explicitly supported by the context.
If the answer includes speculation, external knowledge, or unsupported numbers/names/dates, mark it ungrounded.
13) Best practice: define thresholds per use case
Not all applications need the same standard:
- Medical/legal/financial: very strict
- Internal FAQ: moderate
- Creative summarization: more lenient
For high-stakes use cases, prefer:
- no unsupported claims
- explicit refusal when context is insufficient
If you want, I can also give you:
- a groundedness eval prompt,
- a Python test harness, or
- a RAGAS/TruLens setup example.