Learn · With · Examples
Ask a language model a question and it can answer with total confidence — complete with dates, citations, and code — while being completely wrong. This isn’t a bug that occasional updates will fix. It is a direct consequence of how these models work. This guide breaks down exactly why hallucinations happen, with interactive examples, real cases, and the techniques used to reduce them.
Contents
- What is an AI hallucination?
- Intrinsic vs extrinsic — the formal definition
- Interactive: next-token prediction explorer
- Why they happen — 6 root causes
- Interactive: hallucination gallery
- Interactive: temperature and sampling
- Types of hallucinations
- How hallucinations are measured
- Real-world consequences
- Mitigation techniques
- Worked example — grounding with RAG
- Code example — sampling parameters
- Knowledge quiz
Section 01
What is an AI Hallucination?
An AI hallucination is when a language model generates output that is fluent, confident, and completely wrong — a fabricated fact, a citation to a paper that doesn’t exist, a function that was never in the library, a quote nobody said. The term comes from human perception, where a hallucination is seeing something that isn’t there. The model isn’t “seeing” anything, but the analogy fits: it is generating something that has no basis in reality, with the same fluency as something true.
Think of a brilliant friend who has read almost everything but never admits when they don’t know something. Ask them a question and they will answer instantly and confidently — sometimes correctly, sometimes by smoothly inventing a plausible-sounding answer. They are not lying, because lying requires knowing the truth and hiding it. They genuinely cannot tell the difference between “I recall this” and “this sounds right.” That is a language model.
⚖️ Real Case — Fabricated Legal Citations
In 2023, lawyers in a U.S. federal case submitted a brief containing citations to six court cases — complete with quotes and docket numbers. None of the cases existed. A chatbot had fabricated them entirely, and they read as completely legitimate legal citations. The court sanctioned the lawyers involved.
Section 02
Intrinsic vs Extrinsic — The Formal Definition
Researchers split hallucinations into two categories, depending on what the output contradicts:
💡 The Core Insight
A language model is not a database with a retrieval system attached. It is a function that predicts the statistically most likely next word given everything before it. Truth is not a variable in that computation — plausibility is. Most of the time plausible and true are the same thing, because it learned from mostly-true text. But nothing in the mechanism enforces truth. When they diverge, you get a hallucination.
Section 03
Interactive: Next-Token Prediction Explorer
Every word a language model produces is chosen from a probability distribution over its entire vocabulary. Click a prompt below to see simplified next-token probabilities. Notice what happens when no single answer clearly dominates — that flat, spread-out distribution is exactly where hallucinations are born.
⚠️ The Fluency Trap
Notice that all three answers above would read equally confidently in a generated sentence — “The third-longest river in Paraguay by tributary count is the Río Apa.” Nothing in the grammar or tone signals that this answer had only 14% probability versus the capital-of-France answer’s 97%. Fluency is constant; certainty is invisible.
Section 04
Why They Happen — 6 Root Causes
1. Next-Token Prediction, Not Fact-Checking
The model’s only job during generation is “what word comes next.” There is no separate module that checks generated claims against a database of true facts.
2. Training Data Gaps and Noise
If a topic was rare, contradictory, or wrong across the training corpus, the model absorbs that gap or error as if it were reliable pattern.
3. Sampling Randomness
Generation involves randomness (temperature, top-p sampling) by design — to avoid robotic repetitive text. That same randomness can select a low-probability, incorrect token.
4. No Real-Time Grounding
Without a retrieval or search tool connected, the model only has frozen knowledge from training — it cannot look anything up to double-check itself mid-answer.
5. Miscalibrated Confidence
Models are trained to sound helpful and fluent, not to output calibrated uncertainty. There’s no built-in “I’m only 14% sure” phrasing — everything comes out sounding equally confident.
6. Lossy Compression of Knowledge
A model with billions of parameters still compresses trillions of words of training text. Like a JPEG at low quality, fine details blur — and the model fills gaps with its best statistical guess.
🧠 An Analogy: Compression, Not Storage
A language model does not store facts the way a database stores rows. It stores statistical patterns compressed from its training data. Recalling a fact is more like reconstructing a blurry JPEG than reading a row from a spreadsheet — usually accurate, but sometimes the reconstruction fills gaps with a plausible-looking artifact that was never actually there.
Section 05
Interactive: Hallucination Gallery
Click through five common categories of hallucination. Each shows a real-style prompt, what a model might confidently generate, and what is actually true.
Section 06
Interactive: Temperature and Sampling
“Temperature” controls how randomly a model samples from its next-token probability distribution. Lower temperature makes it pick the highest-probability token almost every time. Higher temperature flattens the distribution, giving low-probability (and potentially wrong) tokens a real chance of being chosen. Click through the same prompt at three different temperatures.
2. It grew into a major center of trade in salt, gold, and books.
3. The University of Sankore made it a hub of Islamic scholarship by the 14th century.
2. Its libraries once held an estimated 700,000 manuscripts.
3. By the 1300s it briefly rivaled Cairo as a center of Islamic learning in West Africa.
2. Its original name, “Tinbouctu,” is said to mean “the place of the hidden well of stars.”
3. Early manuscripts suggest the city briefly minted its own gold currency called the “sanu-tomo.”
Section 07
Types of Hallucinations
| Type | What happens | Example |
|---|---|---|
| Factual | States something false about the world | Wrong birth year for a historical figure |
| Faithfulness | Contradicts a document it was explicitly given | Summarizing a report and stating a conclusion the report actually rejects |
| Citation | Invents sources, papers, or links | A perfectly formatted but nonexistent journal reference |
| Logical | Reasoning steps don’t actually follow from each other | A multi-step math “proof” where step 3 doesn’t follow from step 2 |
| Entity | Merges or invents people, places, organizations | Attributing a real quote to the wrong (but plausible) person |
| Temporal | Gets timelines or sequences wrong | Saying an event happened before its actual cause |
Section 08
How Hallucinations Are Measured
Researchers use standardized benchmarks to compare how often different models hallucinate. These typically involve asking models factual questions where a subtly wrong but plausible answer is a common human misconception — deliberately testing the gap between “sounds right” and “is right.”
| Benchmark | What it tests | Approach |
|---|---|---|
| TruthfulQA | Common misconceptions | 817 questions designed so a plausible-sounding wrong answer mimics common false beliefs |
| HaluEval | General hallucination detection | 35,000 samples across QA, summarization, and dialogue tasks |
| FActScore | Factual precision of long-form text | Breaks generated biography-style text into atomic facts and verifies each one |
| SelfCheckGPT | Consistency-based detection | Samples the same prompt multiple times — inconsistent answers signal likely hallucination |
⚠️ No Benchmark is Perfect
Hallucination rates vary wildly depending on domain, question difficulty, and whether the model has retrieval tools available. A single “hallucination rate” percentage for a model is always a simplification — the real rate depends heavily on what you’re asking.
Section 09
Real-World Consequences
Legal Filings
Multiple documented cases of lawyers submitting briefs with fabricated case citations generated by chatbots, leading to court sanctions and case dismissals.
Medical Information
Confidently wrong drug interaction or dosage claims are especially dangerous because the fluent, authoritative tone makes errors hard to spot without independent verification.
Customer Service Bots
An airline’s chatbot invented a bereavement fare policy that didn’t exist. A tribunal ruled the airline was still bound by what its bot had promised the customer.
Software Supply Chain
Attackers have started publishing malicious packages under names that AI coding assistants commonly hallucinate as real library names — a technique called “slopsquatting.”
News Summarization
AI news summary tools have attributed claims to sources that never made them, misrepresenting the actual reporting underneath.
Academic Research
Students and researchers citing AI-fabricated sources have had papers rejected or retracted after reviewers discovered the citations don’t exist.
Section 10
Mitigation Techniques
| Technique | How it works | Effectiveness |
|---|---|---|
| RAG (Retrieval-Augmented Generation) | Retrieves real documents and grounds the answer in them before generating | High — directly addresses the “no grounding” root cause |
| Lower temperature | Reduces randomness in token selection for factual tasks | Moderate — reduces but doesn’t eliminate risk |
| Chain-of-thought prompting | Forces the model to show reasoning steps, making errors easier to catch | Moderate — helps with logical, not factual, hallucinations |
| Self-consistency checking | Generate multiple answers and check if they agree | Moderate — inconsistency signals uncertainty |
| RLHF fine-tuning | Trains the model to say “I don’t know” when uncertain | Moderate — helps but models remain overconfident on edge cases |
| Citation requirements | Forces the model to link every claim to a real retrievable source | High — makes fabrication immediately checkable |
| Human review | A person verifies high-stakes outputs before they’re used | Highest — the only currently reliable safeguard for critical use cases |
Section 11
Worked Example — Grounding with RAG
Retrieval-Augmented Generation (RAG) is the most effective widely-used mitigation. Instead of relying purely on the model’s compressed training memory, the system first retrieves real, current documents and feeds them into the prompt as context.
Model has no access to your company’s actual policy documents — it generates a plausible-sounding generic refund policy based on common patterns from training data, presented as if it were your specific company’s real policy.
System first retrieves your actual policy document, inserts its real text into the prompt, then asks the model to answer using only that text. The model now has real grounding — reducing (not eliminating) fabrication risk to whether the model accurately reads the provided text.
✅ Why Grounding Helps but Doesn’t Solve It
RAG dramatically reduces factual hallucinations because the model is now paraphrasing real retrieved text instead of reconstructing from compressed memory. But it doesn’t eliminate the risk entirely — the model can still misread, misquote, or blend the retrieved documents with its own memory (an intrinsic hallucination against the very source it was given).
Section 12
Code Example — Sampling Parameters
Here is how temperature and top-p (nucleus sampling) are actually set when calling a language model API — the same parameters demonstrated in the interactive Temperature demo above.
import anthropic client = anthropic.Anthropic() # Low temperature — for factual, deterministic tasks # Minimizes risk of low-probability token selection response_factual = client.messages.create( model="claude-sonnet-5", max_tokens=500, temperature=0.0, # deterministic, most-likely token every time messages=[{"role": "user", "content": "What year did WWII end?"}] ) # Higher temperature — for creative, exploratory tasks # Accepts more hallucination risk in exchange for variety response_creative = client.messages.create( model="claude-sonnet-5", max_tokens=500, temperature=1.0, messages=[{"role": "user", "content": "Write a whimsical short story about rain."}] ) # RAG pattern — ground the model in real retrieved text def answer_with_grounding(question, retrieved_docs): context = "\n\n".join(retrieved_docs) prompt = f"""Answer ONLY using the context below. If the answer is not in the context, say "I don't have that information." Context: {context} Question: {question}""" return client.messages.create( model="claude-sonnet-5", max_tokens=500, temperature=0.0, # low temp — stick close to retrieved facts messages=[{"role": "user", "content": prompt}] )
⚠️ Temperature Zero Isn’t a Cure
Setting temperature to 0.0 reduces randomness-driven hallucination, but does nothing for hallucinations that come from a genuinely wrong top-probability token — if the model’s single most confident answer is itself incorrect, temperature 0 will reproduce that error every single time, consistently.
Section 13
Knowledge Quiz
Click a question to expand it, then pick your answer.
