LLM Hallucinations: Causes, Detection, and Mitigation Strategies
Why the Next-Token Objective Doesn't Care About Truth
LLMs hallucinate because they are trained to predict the next plausible token β not the next true token.
π― System Scenario & Requirements
System Goal
LLMs hallucinate because they are trained to predict the next plausible token β not the next true token.
Actors & Components
- Application or API tier
- Core service under design
- Operator or SRE observer
Functional Requirements
- TLDR: LLMs hallucinate because they are trained to predict the next plausible token β not the next true token.
- Understanding the three hallucination types (factual, faithfulness, open-domain) plus the five root causes lets you choose the right mitigation: RAG for knowledge gaps, consistency sampling for detection, system-promptβ¦
- <!-- To understand why hallucinations are structural rather than incidental, you have to understand what language models are actually trained to do.
Constraints & Assumptions
- No mitigation strategy is free.
- Here is what you actually pay for each: The key insight engineers miss: These strategies are not mutually exclusive.
β‘ Execution Stages & Decision Trace
Stage 1 β The $500,000 Legal Brief That Cited Cases That Never Existed
In May 2023, New York attorney Steven Schwartz filed a legal brief in federal court on behalf of his client Roberto Mata in a case against Avianca airlines.
The brief cited no fewer than six legal cases as precedents β Varghese v.
Deep Dive
Scenario context
LLMs hallucinate because they are trained to predict the next plausible token β not the next true token. Understanding the three hallucination types (factual, faithfulness, open-domain) plus the five root causes lets you choose the right mitigation: RAG for knowledge gaps, consistency sampling for detection, system-prompt grounding for faithfulness, and NLI-based pipelines for automated verification. <!--
To undersβ¦
This walkthrough focuses on Why the Next-Token Objective Doesn't Care About Truth.
Before making a design choice, clarify:
- To understand why hallucinations are structural rather than incidental, you have to understand what language models are actually trained to do.
- There are five interlocking root causes.
- The Training Objective Is Prediction, Not Verification A transformer language model is trained to minimize cross-entropy loss over a corpus of text β in plain language, it learns to predict what word comes next given alβ¦
Concepts in play: llm-prompt-engineering, vector-databases-rag, consistency-replication, llm-agents-and-langgraph
Stage 1 β The $500,000 Legal Brief That Cited Cases That Never Existed
- In May 2023, New York attorney Steven Schwartz filed a legal brief in federal court on behalf of his client Roberto Mata in a case against Avianca airlines.
- The brief cited no fewer than six legal cases as precedents β Varghese v.
- China Southern Airlines, Martinez v.
- Delta Air Lines, and four others β complete with specific case numbers, court names, dates, and detailed legal reasoning excerpted from each ruling.
Observable outcome: Delta Air Lines, and four others β complete with specific case numbers, court names, dates, and detailed legal reasoning excerpted from each ruling.
Stage 2 β Not All Hallucinations Are Equal: A Taxonomy That Actually Matters
- The word "hallucination" gets applied loosely to any LLM error, which makes it nearly impossible to fix because the root causes differ.
- Research in NLP has converged on three distinct types, and each needs a different remedy.
- Factual Hallucinations (Extrinsic) The model asserts something that is verifiably false β a case that doesn't exist, a drug dosage that's wrong, a statute that was never enacted.
- The model's output contradicts world knowledge .
Observable outcome: The model's output contradicts world knowledge .
Stage 3 β How Hallucinations Show Up Before You Know to Look for Them
- Before you can detect and mitigate hallucinations, you need to recognize them in the wild.
- The problem is that hallucinated outputs look identical to correct outputs β same fluency, same confident tone, same well-formed prose.
- Here are the three observable patterns that should trigger a closer look.
- Pattern A: Specificity without a source.
Observable outcome: Pattern A: Specificity without a source.
Stage 4 β Deep Dive: Inside the LLM's Confabulation Machinery
- Understanding why hallucinations are structurally inevitable at the architecture level β not just conceptually β gives you a much stronger foundation for choosing where and how to intervene.
- The Internals: Attention, Memorization, and the Embedding Space A transformer's attention mechanism learns to weight tokens in the context window by their relevance to the current token being generated.
- For well-memorized facts, the model has essentially encoded the fact as a high-probability association between key tokens.
- When you ask "What is the boiling point of water?" the model activates strong, consistent associations from millions of training examples.
Observable outcome: When you ask "What is the boiling point of water?" the model activates strong, consistent associations from millions of training examples.
Stage 5 β Visualizing the Three-Layer Defense Against Hallucinations
- The following diagram maps the three hallucination types from the taxonomy to their corresponding detection strategy and mitigation layer, giving a single reference view of how all the pieces fit together.
- This diagram shows how each hallucination type routes to its most effective detection layer.
- Factual hallucinations are best caught by checking whether a retrieved authoritative document supports the claim (RAG layer).
- Faithfulness hallucinations β where the answer contradicts the provided context β are best caught by NLI entailment scoring.
flowchart TD
A[LLM Output] --> B{Hallucination Type}
B -->|Factual - contradicts world knowledge| C[RAG Grounding Layer]
B -->|Faithfulness - contradicts provided context| D[NLI Entailment Check]
B -->|Open-Domain - extrapolated fabrication| E[Consistency Sampling]
C --> F{Retrieved docs support the claim?}
F -->|Yes| G[Serve grounded answer]
F -->|No| H[Return: I cannot verify this]
D --> I{Entailment score above threshold?}
I -->|Yes - entailed| G
I -->|No - contradiction or neutral| H
E --> J{Consistency score above threshold?}
J -->|Yes - stable across samples| G
J -->|No - divergent outputs| H
Observable outcome: Faithfulness hallucinations β where the answer contradicts the provided context β are best caught by NLI entailment scoring.
Stage 6 β Detecting and Mitigating Hallucinations: Three Runnable Python Patterns
- Let's move from theory to code.
- The three patterns below cover the main engineering approaches: cross-encoder-based similarity detection, RAG-based mitigation with a simple vector store, and SelfCheckGPT-style consistency sampling.
- Pattern 1: Cross-Encoder Similarity Detection This approach compares an LLM's answer against a known-good reference document.
- A cross-encoder (rather than a bi-encoder) jointly encodes both texts and produces a calibrated similarity score, making it more sensitive to semantic contradiction than cosine similarity.
"""
Pattern 1: Detect potential hallucinations using a cross-encoder.
Requires: pip install sentence-transformers
"""
from sentence_transformers import CrossEncoder
# Load a cross-encoder fine-tuned on NLI (Natural Language Inference)
# This model classifies: contradiction / neutral / entailment
model = CrossEncoder("cross-encoder/nli-deberta-v3-small")
def detect_hallucination(reference_text: str, model_answer: str) -> dict:
"""
Returns a dict with NLI label and confidence scores.
'contradiction' indicates likely hallucination.
'entailment' indicates the answer is grounded in the reference.
"""
scores = model.predict([(reference_text, model_answer)])
# Labels: 0=contradiction, 1=entailment, 2=neutral
label_map = {0: "contradiction", 1: "entailment", 2: "neutral"}
label_idx = int(scores[0].argmax())
return {
"label": label_map[label_idx],
"scores": {
"contradiction": float(scores[0][0]),
"entailment": float(scores[0][1]),
"neutral": float(scores[0][2]),
},
"hallucination_risk": "HIGH" if label_idx == 0 else "LOW" if label_idx == 1 else "MEDIUM"
}
# Example usage
reference = (
"Air Canada's bereavement fare discount policy was discontinued in 2014. "
"The airline no longer offers reduced fares for customers traveling due to a family death."
)
hallucinated_answer = (
"Air Canada offers a bereavement discount of up to 50% for passengers "
"who need to travel urgently due to a family member's death."
)
grounded_answer = (
"Air Canada does not currently offer bereavement fare discounts. "
"This policy was discontinued in 2014."
)
print("=== Hallucinated Answer ===")
print(detect_hallucination(reference, hallucinated_answer))
print("\n=== Grounded Answer ===")
print(detect_hallucination(reference, grounded_answer))
"""
Pattern 2: RAG mitigation β ground the LLM answer in retrieved documents.
Requires: pip install chromadb sentence-transformers openai
"""
import chromadb
from chromadb.utils import embedding_functions
import openai
# Set up a local ChromaDB collection
client = chromadb.Client()
ef = embedding_functions.SentenceTransformerEmbeddingFunction(
model_name="all-MiniLM-L6-v2"
)
collection = client.get_or_create_collection(
name="policy_docs",
embedding_function=ef
)
# Index your ground-truth documents
documents = [
"Air Canada's bereavement fare discount policy was discontinued in 2014. "
"The airline does not offer reduced fares for bereavement travel.",
"Air Canada's refund policy allows full refunds within 24 hours of booking "
"for tickets purchased at least 7 days before departure.",
"Air Canada Altitude status can be earned on flights operated by Air Canada, "
"Air Canada Rouge, and select Star Alliance partners.",
]
collection.add(
documents=documents,
ids=["doc1", "doc2", "doc3"]
)
def rag_answer(user_query: str, n_results: int = 2) -> str:
"""Retrieve relevant docs and generate a grounded answer."""
# Step 1: Retrieve
results = collection.query(query_texts=[user_query], n_results=n_results)
context_chunks = "\n".join(results["documents"][0])
# Step 2: Augment and generate
prompt = f"""You are a helpful airline assistant. Answer the user's question
ONLY using the information in the provided context.
If the context does not contain enough information to answer,
say "I don't have that information β please contact Air Canada directly."
Context:
{context_chunks}
Question: {user_query}
Answer:"""
response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
temperature=0.0, # Low temperature reduces creative extrapolation
)
return response.choices[0].message.content
# Example
query = "Does Air Canada offer a discount for bereavement travel?"
print(rag_answer(query))
# Expected: Grounded answer stating the policy was discontinued
Observable outcome: A cross-encoder (rather than a bi-encoder) jointly encodes both texts and produces a calibrated similarity score, making it more sensitive to semantic contradiction than cosine similarity.
End-to-end outcome
To understand why hallucinations are structural rather than incidental, you have to understand what language models are actually trained to do.
Alternatives considered
Choosing Your Mitigation Strategy: A Use-Case Decision Guide
Choosing Your Mitigation Strategy: A Use-Case Decision Guide
Failure and degraded modes
Trade-offs: The Honest Engineering Calculus
- Detection: No mitigation strategy is free.
- User or operator impact: No mitigation strategy is free.
