Deep Dives11 min read25 May 2026

Why AI Hallucinates: The Real Reason

AI hallucinations aren't bugs or lies — they're an inevitable consequence of how language models work. Understanding why helps you build prompts that minimise them.

The term "hallucination" was borrowed from psychology, where it describes perceiving something that is not there. In AI, it describes generating information that is not grounded in reality — a structurally similar failure, even though the mechanisms are completely different.
python
import numpy as np

# Simplified illustration of next-token sampling
# The model outputs a logit score for each token in the vocabulary

vocabulary = ["Lincoln", "Bell", "Edison", "Tesla", "Morse"]
logits = np.array([2.1, 4.8, 1.3, 0.9, 0.7])  # raw scores before softmax

def softmax(logits, temperature=1.0):
    """Convert logits to probabilities with temperature scaling."""
    scaled = logits / temperature
    exp_scores = np.exp(scaled - np.max(scaled))  # numerical stability
    return exp_scores / exp_scores.sum()

# At temperature 1.0 — proportional sampling
probs_t1 = softmax(logits, temperature=1.0)
print("Temp 1.0:", dict(zip(vocabulary, probs_t1.round(3))))
# Bell gets ~83% probability — but other tokens still possible

# At temperature 0.1 — near-deterministic, top token almost always picked
probs_t01 = softmax(logits, temperature=0.1)
print("Temp 0.1:", dict(zip(vocabulary, probs_t01.round(3))))
# Bell gets ~99.9% probability

# At temperature 1.5 — more exploration, lower-probability tokens more likely
probs_t15 = softmax(logits, temperature=1.5)
print("Temp 1.5:", dict(zip(vocabulary, probs_t15.round(3))))
# Distribution flattens — Tesla or Morse more likely to be picked
The model has no ground truth. It learns from the distribution of text on the internet — and the internet is wrong a lot. If 70% of sources say X and 30% say Y, the model will predominantly output X even if Y is correct.
Ask Claude or GPT-4 to cite studies on a moderately obscure topic. Some citations will be real. Some will have real authors but wrong papers. Some will have plausible-sounding but entirely invented paper titles. Some URLs will 404. The model generates statistically plausible citation-shaped text, not verified database lookups.
The key phrase is "based ONLY on the following" combined with "do not add information from outside this excerpt." Without explicit constraint, models blend retrieved context with training-data knowledge — producing confident outputs that mix true retrieved facts with hallucinated additions.
python
import anthropic

client = anthropic.Anthropic()

def extract_from_source(source_text: str, question: str) -> str:
    """
    Extract information from provided text only.
    Uses explicit grounding instructions to prevent hallucination.
    """
    response = client.messages.create(
        model="claude-haiku-4-5",
        max_tokens=800,
        temperature=0,  # deterministic — reduces creative "gap-filling"
        system="""You are a precise information extractor.
Rules:
1. Answer ONLY using information explicitly stated in the provided source text.
2. If the answer is not in the source text, say: "This information is not in the provided text."
3. Do not infer, extrapolate, or add context from outside knowledge.
4. Quote directly when exact wording matters.
5. Never fabricate statistics, names, dates, or citations.""",
        messages=[{
            "role": "user",
            "content": f"""Source text:
---
{source_text}
---

Question: {question}"""
        }]
    )
    return response.content[0].text


# Example usage
report_excerpt = """
The Q3 2025 earnings report showed revenue of $4.2M, up 34% year-over-year.
Customer count reached 1,847, with enterprise customers (>$10k ACV) comprising 23% of
the total but 61% of revenue. Net Revenue Retention was 118%.
The company reported a net loss of $890k, improved from a $1.4M loss in Q3 2024.
"""

result = extract_from_source(
    report_excerpt,
    "What was the net revenue retention and how did the loss compare to last year?"
)
print(result)
# Output: NRR was 118%. Net loss was $890k in Q3 2025, compared to $1.4M in Q3 2024 — an improvement of $510k.

result2 = extract_from_source(
    report_excerpt,
    "What was the customer acquisition cost?"
)
print(result2)
# Output: "This information is not in the provided text." — correctly refuses to hallucinate
The goal is not zero hallucinations but appropriate mitigation for the stakes of the task. AI-generated first drafts, brainstormed options, or creative content can tolerate some hallucination. Medical diagnoses, legal briefs, and financial reports cannot. Match your verification effort to the consequences of getting it wrong.
hallucinationsLLMsdeep divesaccuracyprompting
🎓Interactive Courses

Ready to go further?

Take the interactive course — daily lessons, real exercises, XP and streaks. Turn reading into lasting skills.

Daily streaksXP & levels
Start a course