How would you automatically detect and reduce LLM hallucinations?
Written by the mlprep team · Updated
Your team ships an LLM-powered product and users report the model confidently stating false information. How would you detect hallucinations automatically at scale, measure the problem, and reduce their frequency? Cover both detection methods and mitigation strategies.
Taxonomy: what kind of hallucination?#
Factual hallucination: model states something false about the real world ("The Eiffel Tower is 450 meters tall").
Grounding hallucination (intrinsic): model contradicts or ignores the provided context/documents (RAG systems saying something the retrieved docs don't support).
Confabulation: model fabricates plausible-sounding but nonexistent specifics — paper citations, API method names, historical dates.
The detection strategy differs by type. Factual hallucination requires external knowledge; grounding hallucination only requires comparing output to context.
Detection methods#
Grounding check (NLI-based): for RAG systems, run a Natural Language Inference classifier to check whether the generated answer is entailed by the retrieved documents.
premise = retrieved_context
hypothesis = generated_answer
label = entailment / neutral / contradiction
Models: MiniCheck, TRUE, FactScore. Fast, no external API needed, interpretable failure signal.
Self-consistency sampling: sample K responses to the same question with temperature > 0. If answers disagree significantly, the model is uncertain — likely hallucinating. Consistent responses are more reliable (though a model can hallucinate consistently).
if variance(answers) > threshold: flag as uncertain
LLM-as-judge: prompt a separate (or same) LLM to evaluate whether the answer is supported by the provided context. Scales well, but inherits the judge model's own biases and may hallucinate its judgments.
Token-level uncertainty via logit entropy: high entropy in the output distribution at a given position suggests the model is uncertain. Average token-level entropy over the generated span correlates with hallucination rate.
entropy = -Σ p_i · log(p_i) over vocab at each step
Can be computed without ground truth, but noisy. Works better as a relative signal than an absolute threshold.
Retrieval verification: for factual claims, retrieve evidence from a trusted corpus and check whether the claim is supported. Pipeline: extract claims (using LLM or NER) → retrieve → NLI score. FactScore (Min et al., 2023) operationalizes this.
Mitigation strategies#
RAG with strict grounding instructions: explicit prompt: "Answer only from the provided context. If the answer is not in the context, say 'I don't know.'" Reduces confabulation but doesn't eliminate it.
Abstention training: fine-tune the model to output uncertainty signals ("I'm not sure about this") or refuse when confidence is low. Requires labeled examples of uncertain vs confident scenarios.
Constrained decoding: restrict output tokens to those that appear in the source context (lexically grounded). Extreme but effective for extractive tasks. Kills fluency for generative tasks.
RLHF with hallucination penalty: label hallucinated outputs as negative in preference data. Trains the model to prefer grounded answers. Works at scale but requires high-quality labeling.
Chain-of-thought + self-verification: ask the model to reason step-by-step and then verify its own answer before outputting. Reduces hallucination on reasoning tasks; less effective on pure factual recall.
Measuring hallucination rate#
- For RAG: grounding rate (% answers entailed by context), measured with NLI classifier on held-out queries
- For factual: FactScore (precision of atomic claims supported by Wikipedia)
- For user experience: thumbs-down rate, edit distance between model output and human correction
- Baseline: always compare to human annotation on a stratified sample — automated metrics drift from human perception
Senior/staff considerations#
Hallucination is a spectrum, not a binary. The goal is not zero hallucination (unachievable) but calibration: the model should be uncertain on uncertain topics and confident on well-supported ones. High-stakes domains (medical, legal) need different thresholds than casual use cases.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Hallucination detection: NLI-based grounding checks (best for RAG — is output entailed by context?), self-consistency sampling (disagreement across samples signals uncertainty), logit entropy (model's own uncertainty), and retrieval verification (extract claims, verify against corpus). Mitigation: strict grounding prompts, abstention training, RLHF with hallucination penalties. Goal is calibration — confident when correct, uncertain when uncertain — not zero hallucination.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How would you set up an automated eval pipeline for hallucination rate that runs on every model release?
- What is FactScore and how does it decompose factual accuracy into atomic claims?
- How does calibration differ from accuracy, and why does a well-calibrated model still hallucinate?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
How would you design a RAG system that answers questions over a company's internal documents?
How do you put confidence intervals on ML metrics?
Walk me through classification evaluation metrics — precision, recall, F1, AUC
Explain NDCG — why use it over AUC for ranking?