Velamuvi AIAcademy

Lesson 8 of 12 · How It Works

Why Models Hallucinate

Why AI models confidently state false things: plausible isn't true, rare facts blur and guessing is rewarded. See a tiny model hallucinate, and how to catch it.

  • 8 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • What a hallucination is, with real examples
  • The main reasons models hallucinate
  • The kinds of output most likely to be made up
  • Practical ways to reduce hallucinations and catch them

Fluent, confident and wrong

Hallucinations are not lies; the model has no intention to deceive. They are a side effect of how it works. Remember the loop: the model picks a likely next token, over and over. Nothing in that loop checks whether the result is true.

Watch a tiny model hallucinate

This model learns from four true sentences. Then we ask it to complete "the capital of france is".

Try it yourselfPython
training_text = (
    "the capital of france is paris . "
    "the capital of italy is rome . "
    "the capital of spain is madrid . "
    "the capital of japan is tokyo ."
)
words = training_text.split()

# This model only remembers which word followed each single word
follows = {}
for current, following in zip(words, words[1:]):
    if current not in follows:
        follows[current] = []
    follows[current].append(following)

print("Words that followed 'is':", follows["is"])
print()

prompt = "the capital of france is"
last_word = prompt.split()[-1]
options = follows[last_word]
for city in ["paris", "rome", "madrid", "tokyo"]:
    chance = options.count(city) / len(options)
    print(f"{prompt} {city}  ({chance:.0%})")

Every training sentence was true, yet three times out of four the model says something false. Each wrong answer is perfectly fluent: it is the right kind of word (a capital city) in the right place. The model learned the pattern "after is comes a capital" much better than the fact "France goes with Paris".

Real models are vastly better than this, and they get common facts like capitals right. But the same thing happens at the edges of their knowledge: for rare facts, the pattern is learned better than the detail, so details get blended.

Why it happens

  1. Plausible is not the same as true. The model is trained to produce text that looks like its training data. A made-up reference that looks real scores just as well, to the model, as a real one.
  2. Rare facts blur. A fact seen thousands of times is learned firmly. A fact seen once or twice, like a small company's founding year or a minor author's book titles, is learned weakly and gets mixed with similar facts.
  3. Guessing is rewarded. In training and in many tests, a confident guess that is sometimes right scores better than "I don't know", which is never counted as right. So models learn to guess.
  4. No library to check. A model's knowledge is squeezed into its parameters. It cannot look up the original page unless it has a search or document tool.
  5. False premises. Ask "Why did Einstein win the Nobel Prize for relativity?" and a model may explain, fluently, something that never happened. (He won it for explaining the photoelectric effect.) The question pushes it toward an answer that fits the question.
  6. One slip leads to another. Each new token is predicted from the text so far, including the model's own mistakes. An early wrong detail gets built upon.

What is most likely to be made up

High riskWhy
References, links, citationsThey have a very regular shape that is easy to imitate.
Exact quotationsWording is rarely memorised exactly.
Numbers, dates, statisticsMany similar numbers blur together.
Details about less famous peopleLittle training data, and it is mixed with others of the same name.
Software functions and settingsA plausible but non-existent function name looks just like a real one.
Very recent eventsBeyond the knowledge cutoff.

Lower risk: rewriting text you supplied, summarising a document you pasted in, and explaining well-known ideas.

How to reduce hallucinations

  • Give it the source. Paste the document and say "Answer only from the text below."
  • Ask for quotes. "Quote the sentence that supports each point." Then check that the quote really is there.
  • Allow "I don't know". Say "If the answer is not in the text, or you are not sure, say so."
  • Use search, then check citations. An assistant with web search can link to sources. Open the important ones.
  • Run the code. For programming, running the code and tests catches invented functions immediately.
  • Lower the temperature for factual work when you have the setting (see the earlier lesson).
  • Cross-check what matters with an independent source, especially numbers, names, dates and anything legal, medical or financial.

The program below does a very simple version of quote checking: is each claim actually in the source?

Try it yourselfPython
source = "The museum opens at 9am and closes at 5pm. Entry costs 12 euros. It is closed on Mondays."

answer_claims = [
    "opens at 9am",
    "Entry costs 15 euros",
    "free entry for students",
]

for claim in answer_claims:
    if claim.lower() in source.lower():
        print("found in source:", claim)
    else:
        print("CHECK THIS:     ", claim)

Real checking is harder, because a correct answer may use different words from the source. But the habit is the same: every important claim should trace back to a source you trust.

Common mistakes

Practice exercise

Check an AI answer's claims against the source it was supposed to use.

Your solutionPython
source = "The museum opens at 9am and closes at 5pm. Entry costs 12 euros. It is closed on Mondays."
claims = ["closes at 5pm", "Entry costs 15 euros", "closed on Mondays", "free on Sundays"]

# For each claim print "SUPPORTED: claim" if it appears in the source
# (ignoring capital letters), otherwise "NOT FOUND: claim".
Knowledge check

Which request is MOST likely to produce a hallucination?

Summary

  • A hallucination is false or invented content stated as if true.
  • It happens because models produce likely text, rare facts blur, guessing is rewarded, and there is no built-in fact check.
  • References, quotations, numbers, niche details and code functions are the riskiest outputs.
  • Grounding the model in trusted sources, asking for quotes, allowing "I don't know" and checking what matters are the best defences.

Next: how to choose between the many models available.

Finished reading? Tick it off to track your progress.