Hallucinations: why they happen

In one sentence

LLMs predict plausible text, not true text, and training and benchmarks reward guessing over "I don't know". You can't remove hallucinations; you ground, verify, allow abstention and measure the residual rate.

Analogy: A student on a multiple-choice exam with no penalty for wrong answers: a blank scores zero, so they always guess, confidently. Change the grading and "I don't know" starts to appear.

The model fills gaps with plausible text; a deterministic claim check decides whether it ships or abstains.

How it works

  • Next-token loss can't tell a plausible fake citation from a real one; nothing in it checks truth.
  • Facts seen once or never in training can't be recalled reliably; anything after the cutoff is unknown.
  • Factuality errors contradict the world; faithfulness errors contradict or go beyond the given context.
  • Measure errors and abstentions: on SimpleQA, o4-mini errs 75% vs 26% for gpt-5-thinking-mini at similar accuracy.
  • Lowering temperature cuts variance, not falsehood.

Check yourself

Answer to mark it learned; your answers aren't scored.

Your RAG assistant answers with verbatim quotes as evidence. An audit finds some quotes appear nowhere in the retrieved documents. What is the most reliable fix?

Go deeper

Sampling playground Interactive lab
Full explanation

A hallucination is fluent, confident output that is false or not supported by the input. It isn't a bug in one component; it falls out of how LLMs are trained and graded.

Why it happens

  • Objective: pretraining maximizes next-token likelihood. A plausible fake citation and a real one look alike to that loss; nothing in it checks truth.
  • Incentives: OpenAI's 2025 paper argues that benchmarks and post-training grade right/wrong only, so a guess scores better than "I don't know". Models learn to bluff, like students on a test with no penalty for wrong answers.
  • Knowledge gaps: facts seen once or never in training can't be recalled reliably, and anything after the cutoff is unknown.
  • Inputs: ambiguous prompts, missing context, or long noisy context where the key passage gets lost (Lost in the middle & context rot).

Two kinds

  • Factuality: contradicts the world (a wrong date, an invented paper).
  • Faithfulness: contradicts or goes beyond the given context or instructions (a RAG answer adding a clause the document doesn't contain).

Mitigations stack; none reaches zero. Ground answers with RAG (Retrieval-Augmented Generation); require citations you can check (Citations & grounding); explicitly allow abstaining; verify claims with tools or a second pass; measure errors and abstentions (Measuring hallucination & groundedness). Lower temperature cuts variance, not falsehood.

Code Grounded answer with abstention and a quote check
pythonGrounded answer with abstention and a quote check
from openai import OpenAI
from pydantic import BaseModel

client = OpenAI()
MODEL = "gpt-4.1-mini"  # any chat model with structured outputs

class Answer(BaseModel):
    answer: str | None   # None = abstain
    quotes: list[str]    # verbatim evidence copied from the sources

SYSTEM = ("Answer ONLY from the sources. Copy the exact sentences you rely on into "
          "quotes. If the sources don't contain the answer, set answer to null. "
          "A wrong answer is worse than no answer.")

def grounded_answer(question: str, sources: str) -> str:
    resp = client.responses.parse(
        model=MODEL,
        temperature=0,
        text_format=Answer,
        input=[{"role": "system", "content": SYSTEM},
               {"role": "user", "content": f"<sources>\n{sources}\n</sources>\n\n{question}"}],
    )
    out = resp.output_parsed
    # Deterministic check: models fabricate citations too, so every quote must exist
    if out.answer is None or not out.quotes or any(q not in sources for q in out.quotes):
        return "I don't know based on the available documents."
    return out.answer
Real example

Two real failures, one of each kind. Factuality: in Mata v. Avianca (2023), lawyers filed a brief citing six court decisions that ChatGPT had invented, with plausible names and quotes; the judge fined them $5,000. Faithfulness: in 2024 a Canadian tribunal held Air Canada liable after its website chatbot told a grieving customer he could claim a bereavement fare retroactively, contradicting the airline's own policy page. Checking each claim against a source (a case-law database, the policy page) would have caught both.

Trade-offs +3 · −3

+ You gain

  • Grounding plus citations turns many factuality errors into checkable faithfulness questions.
  • Allowing abstention cuts confident errors sharply (SimpleQA: 75% → 26% error rate).
  • Deterministic checks (quote matching, schemas, tool lookups) catch errors without another LLM call.

− You pay

  • No mitigation reaches zero; you must design for the residual rate (review, reversibility, disclaimers).
  • More abstention means lower coverage: users see "I don't know" more often.
  • LLM verifiers and self-checks add latency and cost, and share the model's blind spots.

Use it when

Treat hallucination as a design constraint in every LLM feature. Invest most where outputs are facts people act on: legal, medical, finance, support policy, citations, code that ships.

Avoid it when

Skip heavy factual guards for brainstorming, fiction or drafts a human rewrites: there, invention is the feature. Spend the budget where an error has a cost.

Numbers to know 3
  • SimpleQA (OpenAI): gpt-5-thinking-mini abstains 52%, errs 26%; o4-mini abstains 1%, errs 75%, at similar accuracy (22% vs 24%).
  • Kalai et al.: if ~20% of facts like birthdays appear once in training, expect ≥~20% hallucination on them.
  • Mata v. Avianca (2023): 6 invented case citations, $5,000 sanction.
Common pitfalls 4
  • Believing lower temperature or a bigger model eliminates hallucination. It lowers some, never all.
  • Asking for citations without checking them: models fabricate citations too.
  • Prompts that say "always answer", or evals that score abstention as a failure.
  • Stuffing 100k tokens of loosely relevant context: more noise, more faithfulness errors.
Interview questions 2

Answer out loud first, then reveal.

Why do LLMs hallucinate? Give me the one-minute version.Show answer

Three layers. The objective: pretraining rewards plausible next tokens, and plausible isn't true; for facts seen once or never, there's nothing reliable to recall. The incentives: most post-training and benchmarks grade right or wrong, so guessing beats abstaining, which is the core argument of OpenAI's 2025 paper. And the inputs: ambiguous questions or long, noisy context. So I don't treat it as a bug to patch but as a rate to engineer down and design around.

How would you reduce hallucinations in an enterprise RAG assistant?Show answer

I separate factuality from faithfulness. For factuality, the model doesn't answer from memory: it retrieves, answers only from sources, and says it doesn't know otherwise. For faithfulness, I require quoted evidence per claim, verify quotes deterministically, and run an LLM-judge groundedness check on a sample. Then I measure on a golden set that includes unanswerable questions, tracking both error rate and abstention rate, because cutting one usually raises the other. For high-stakes actions, a human approves.