Describe a concrete, production runnable mechanism to detect hallucinations in a RAG answer (claims unsupported by the retrieved chunks), as the answer is generated or just after.
You are running a production RAG system. Describe a concrete mechanism (not 'just have humans review them') that flags when an answer contains claims unsupported by the retrieved chunks. Cover the inputs, the check, and what happens on a positive detection.
Split the answer into atomic claims, ask a smaller judge LLM whether each claim follows from the retrieved chunks, aggregate to a faithfulness score, and refuse, retry, or log when the score is below threshold.
Imagine a student wrote a five-sentence answer and you want to check whether each sentence is actually backed by the textbook pages you handed them. You go sentence by sentence and ask 'is this in the pages or not?'. If most sentences check out, you accept the answer. If a sentence is invented, you flag it. A production RAG system does exactly this with a smaller AI playing the role of the checker. The big AI writes the answer; the small AI compares each claim against the retrieved chunks. If the small AI flags too many unsupported claims, the system either refuses, retries with stricter instructions, or saves the case for a human reviewer.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Hallucination detection is the safety check that catches what retrieval and prompt grounding miss. A well designed RAG pipeline still produces answers that contain claims not supported by the retrieved chunks: subtle paraphrases that drift from the source, generator side pretraining knowledge that bleeds into the answer, confident assertions about facts the chunks did not actually contain. Without an explicit detector, these slip into production silently.
This deep dive walks through the canonical mechanism (LLM-as-judge claim grounding), what makes it work, the cost tradeoffs that shape deployment patterns, how it relates to offline evaluation frameworks like RAGAS and TruLens, and the senior nuances around judge selection, sampling strategy, and the gap between hallucination detection and answer relevance.
Why you cannot detect hallucinations from the answer alone
A common naive approach is to look at the answer's surface features: confidence language, hedging, length, perplexity, or self-reported uncertainty. None of these correlates reliably with hallucination.
Generator LLMs sound confident regardless of whether they are right. Hedging language ('I think', 'possibly') is a function of prompt style and temperature, not truth. Perplexity measures how surprising the output was to the model, which is uncorrelated with whether the output matches the source.
Hallucination is a relational property between two things: the answer and the source. The answer says X. The source either does or does not contain X. Detecting hallucination means comparing those two artifacts. Any check that looks at only one of them is solving a different problem.
The mechanism that works is a comparison check. Inputs: the generated answer and the retrieved chunks. Optionally also the question, to assess whether the chunks were relevant in the first place. Output: per-claim verdicts on whether each piece of the answer follows from the chunks. This shape is invariant across LLM-as-judge, NLI-based, and human evaluation approaches; only the verifier model changes.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
import re
JUDGE_PROMPT = """You are a grounding judge.
Claim: {claim}
Retrieved chunks:
{chunks}
Does the claim follow from these chunks?
Answer one of: yes / no / partial.
If yes or partial, quote the supporting span.
If no, explain briefly."""
def split_claims(answer):
return [s.strip() for s in re.split(r"(?<=[.!?])\s+", answer) if s.strip()]
def judge_claim(claim, chunks, judge_llm):
out = judge_llm.complete(JUDGE_PROMPT.format(claim=claim, chunks=chunks))
return out.split()[0].lower() # yes / no / partial
def faithfulness_score(answer, chunks, judge_llm="claude-haiku-4.7"):
claims = split_claims(answer)
verdicts = [judge_claim(c, chunks, judge_llm) for c in claims]
supported = sum(1 for v in verdicts if v in ("yes", "partial"))
return supported / max(1, len(claims))
def gated_answer(answer, chunks, threshold=0.7):
score = faithfulness_score(answer, chunks)
if score < threshold:
return {"action": "refuse", "score": score,
"answer": "I don't have enough info to answer that confidently."}
return {"action": "accept", "score": score, "answer": answer}| Approach | Cost | Strength | Weakness |
|---|---|---|---|
| LLM-as-judge per claim | Extra LLM call per answer | Flexible, handles nuance | Latency and dollar cost |
| NLI model (DeBERTa, AlignScore) | Tens of ms per claim | Cheap, fast | Less nuanced on complex claims |
| Answer-level vibe check | One LLM call | Cheapest LLM-judge variant | Misses isolated hallucinations |
| No detection (trust generator) | Zero | No latency | Hallucinations reach users |
| Sampled detection (1-5% traffic) | Bounded extra cost | Catches trends, monitors drift | Misses per-user hallucinations |
| Full-coverage detection | Doubles per-answer cost | Catches every case | Expensive at scale |
Real products, models, and research that use this idea.
- RAGAS is the leading open source RAG evaluation framework; its faithfulness metric implements exactly this pattern (claim decomposition plus per-claim grounding check by a judge LLM).
- TruLens by TruEra offers online and offline hallucination detection on RAG pipelines with hooks for popular LLM judges.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you detect that your judge model is itself unreliable?
Build a human labeled set of (answer, chunks, ground-truth verdict) tuples and measure judge accuracy quarterly. Compare against a second judge from a different model family for agreement. If agreement drops below a threshold, swap or retrain the judge.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Trying to detect hallucinations by looking at the answer alone or by measuring answer confidence. Hallucinations are answer versus source mismatches; both inputs are required.
60 second bullets to scan on the way to the call.
Why hallucination detection requires both the answer and the retrieved chunks as inputs
How per-claim decomposition catches isolated hallucinations an answer level check misses
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.