Why LLMs Hallucinate

Updated · 5 min read
mediumPython 3.13aillmhallucinationsverificationbeginnersinterview

What if the answer has page numbers, and the paper doesn't exist? Navika asks a chat assistant for a study showing that AI raises fruit sales, for a school project. She gets a confident answer with a title, a journal, a year and page numbers. She searches for it, and finds nothing. The assistant made it up.

That is a hallucination. This lesson explains why LLMs produce them, and how to catch them before they cause trouble.

What a Hallucination Is

A hallucination is an output that sounds fluent and confident but is false or unsupported: an invented fact, quote, source, number or event.

A confident, detailed, invented citation

The citation in the picture is an invented example, written to show the kind of answer a model can produce.

Why It Happens

CauseWhat it means
It predicts what fits, not what is trueNext-token prediction, from "How an LLM Generates Text", picks likely text; a realistic-looking citation is very likely text
Gaps in its training dataWhen it never saw the answer, it still produces something that fits the pattern
Knowledge cutoffIt doesn't know events after its training data ended
Pressure to answerModels are trained to be helpful, and may produce an answer rather than say "I don't know"
Misread contextIt may mix up details from your prompt or documents

Hallucinations are most likely for: specific sources and quotes, exact numbers and dates, niche or very recent topics, and details about people who aren't famous.

Protecting Yourself

HabitHow
Ask for sources, then open themA link or citation you haven't opened isn't evidence
Check numbers and datesAgainst an official or original source
Ask it to say when it isn't sure"If you don't know, say so" reduces, but doesn't remove, invented answers
Give it the sourcePaste the document and ask it to answer only from it; see "Grounding Prompts in Sources"
Match checking to the stakesHealth, money, law and anything about real people always need verifying

Common Mistakes

Trusting an answer because it is detailed. Page numbers and author names can be invented as easily as anything else.

Thinking a newer model never hallucinates. Hallucinations become rarer as models improve, but they still happen.

Asking the same model "Are you sure?" as the only check. It may confidently confirm its own mistake; check an independent source.

Interview Corner

What is a hallucination in an LLM?

A fluent, confident output that is false or unsupported, such as an invented fact, number, quote or citation.

Why do LLMs hallucinate, and how do you reduce it?

They generate text that fits learned patterns rather than retrieving verified facts, so gaps, cutoffs and pressure to answer lead to invented content. Reduce it by grounding the model in source documents, asking it to admit uncertainty, using search tools, and verifying important outputs against independent sources.

Practice

Exercise: catch a hallucination. Ask a chat assistant for three research papers, with authors and years, on a narrow topic you know. Look up every one. How many exist exactly as described? Then repeat with the instruction "If you are not sure a paper exists, say so" and compare.

Check your understanding:

  1. What makes something a hallucination rather than a typo?
  2. Why does next-token prediction lead to invented citations?
  3. Name three habits that protect you.

Quick check: hallucinations

4 questions
1What is a hallucination?
2Why can an LLM invent a citation with page numbers?
3Which habit best protects you?
4Which question is most at risk of a hallucination?

Key Takeaways

Quick reference

TermMeaning
HallucinationA confident output that is false or unsupported
GroundingGiving the model source material to answer from
VerificationChecking an output against independent sources

Was this tutorial helpful?

Lesson 33 of 186