Skip to content
AgenticAIWiki
  1. Learn
  2. Part 1
  3. 7. Hallucination and grounding
  4. 7.1

Lesson 7.1 · 1 of 4 · 4 min read · Reviewed 24 Nov 2026

Why models hallucinate

By the end, you can

  • Explain why language models hallucinate.
  • Spot the situations where it's most likely.

Why it happens

The model’s job is to produce a likely continuation (chapter 2). Usually likely and true coincide. When the model doesn’t have the fact, it still produces something that looks right.

prompt: "The 2019 paper by Okafor and Lind on battery recycling found..."
model:  "...that hydrometallurgical methods recovered 92% of lithium."

If no such paper exists in what the model learned, the continuation is still fluent and specific. That’s a hallucinationHallucination: Output that sounds confident but is false or unsupported by the provided sources..

Where it’s most likely

Situation Why
Rare or niche facts Seen too rarely in training to be reliable
Exact numbers, dates, quotes Details blur even when the topic is known
Citations and URLs Easy to pattern-match a plausible one
Events after the cutoff Unknown, but the model may guess (5.5)
Your private data Never seen (5.5)
Leading questions “Why did X happen?” assumes X did

What doesn’t fix it

  • “Be accurate” or “don’t hallucinate” in the prompt helps only a little.
  • Lower temperatureTemperature: A sampling setting that controls randomness: lower values make output more predictable, higher values more varied. doesn’t add knowledge (6.1).

What does

Supply the facts (7.2), let the model say it doesn’t know (7.3), ask for quotes and citationsCitations: References in an answer that point to the exact source passages supporting each claim., and check outputs with evalsEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria. (7.4).

Key points

  • A model generates plausible text; when it lacks the facts, plausible can be wrong.
  • Hallucinations are likely for rare facts, specific numbers, citations, recent events and anything not in the prompt.
  • The fix is to supply facts, allow 'I don't know', and verify claims, not just to ask the model to be accurate.
Hands-on labRuns in the Playground

Flag the claims most likely to be invented

Scan a model answer for the kinds of claims that are most often hallucinated, so they can be checked before anyone trusts them.

You will have: A checker that splits an answer into sentences and flags each risky one with its reason.

Steps

  1. Run the starter: no sentence is flagged.
  2. Add a rule for exact numbers and percentages.
  3. Add rules for years, paper-style citations ('et al.') and URLs.
  4. Print each flagged sentence with its reasons and a final count.
  5. Paste in an answer of your own and check what it flags.

Done when

  • The sentence with '92%' is flagged for a number.
  • The 'Okafor et al.' sentence is flagged as a citation.
  • The script prints 'flagged 3 of 5 sentences'.

Open in the Playground No API key needed.

Starter code (Python)
import re
ANSWER = ("Battery recycling has grown quickly. A 2019 study by Okafor et al. found it recovers most lithium. "
          "Hydrometallurgy recovered 92% of lithium in that work. Many countries now fund recycling plants. "
          "See https://example.org/report for the full data.")

RULES = {
    # TODO: add regex rules, for example "number": r"\d+(\.\d+)?%"
}

sentences = re.split(r"(?<=\.)\s+", ANSWER.strip())
flagged = 0
for s in sentences:
    reasons = [name for name, pattern in RULES.items() if re.search(pattern, s)]
    if reasons:
        flagged += 1
        print("CHECK:", s, reasons)
print(f"flagged {flagged} of {len(sentences)} sentences")
Show a solution
import re
ANSWER = ("Battery recycling has grown quickly. A 2019 study by Okafor et al. found it recovers most lithium. "
          "Hydrometallurgy recovered 92% of lithium in that work. Many countries now fund recycling plants. "
          "See https://example.org/report for the full data.")

RULES = {
    "number": r"\b\d+(\.\d+)?%",
    "year": r"\b(19|20)\d{2}\b",
    "citation": r"\bet al\.",
    "url": r"https?://\S+",
}

sentences = re.split(r"(?<=[a-z0-9]\.)\s+(?=[A-Z])", ANSWER.strip())
flagged = 0
for s in sentences:
    reasons = [name for name, pattern in RULES.items() if re.search(pattern, s)]
    if reasons:
        flagged += 1
        print("CHECK:", s, reasons)
print(f"flagged {flagged} of {len(sentences)} sentences")
Quick checkWhy might a model invent a convincing but fake paper citation?
How sure are you?
Pick an answer and how sure you are