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.
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
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.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3 min: name LLM-as-judge claim grounding, specify both inputs (answer plus chunks), describe per-claim decomposition and judge prompt, give the three gating actions, and close with the sampling versus full coverage cost tradeoff.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes 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.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.