0
Generative AI Foundations
Phase 1Module 13 of 15

Hallucination

LLMs are trained to predict likely text, not to verify truth. They generate fluent, confident responses even when they don't know the answer. This is dangerous in production: fake citations, invented statistics, incorrect medical/legal advice.

An LLM hallucinating is like a confident storyteller who fills gaps in their memory with plausible fiction — they don't realize they're making things up, and the story sounds convincing until you fact-check it.

Visual Workflows

Start here — study each diagram, then use + / to zoom if needed.

100%
Loading diagram...

Scroll inside the frame · use + / − to zoom

Key Takeaways

  • 1.Hallucination is when an LLM generates confident, plausible-sounding but factually incorrect or fabricated information — one of the most critical challenges in AI engineering.
  • 2.Hallucination types: factual (wrong dates, names, statistics), fabricated (invented citations, URLs, products), inconsistent (contradicts earlier context), and reasoning errors (flawed logic presented confidently).
  • 3.Mitigations: RAG (ground in retrieved documents), prompt instructions ('only use provided context', 'say I don't know if unsure'), lower temperature, structured outputs, citation requirements, human review, and evaluation suites that catch hallucinated content.

Real Example

Scenario

A legal assistant hallucinates a case citation that doesn't exist. Mitigation: RAG retrieves actual case documents, system prompt requires citing only provided sources, and a validation step checks that cited cases appear in the retrieved context.

What you would do

In Generative AI Foundations, apply Hallucination to this scenario: A legal assistant hallucinates a case citation that doesn't exist. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

Practice Task

Open the Code Walkthrough below and run it locally. Change one parameter related to Hallucination (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

Code Walkthrough

Highlighted lines show where Hallucination happens in the code.

Hallucination
1SYSTEM_PROMPT = """Answer ONLY based on the provided context.2If the context doesn't contain the answer, say "I don't have enough information."3Always cite the source document for each claim.4Never invent facts, statistics, or citations."""  # key line for Hallucination5
6def generate_grounded_answer(question: str, context: str) -> str:  # define a reusable function7    response = client.chat.completions.create(  # call the API8        model="gpt-4o-mini",9        messages=[10            {"role": "system", "content": SYSTEM_PROMPT},11            {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}"},12        ],13        temperature=0,  # key line for Hallucination14    )15    return response.choices[0].message.content  # return the result

Cheat Sheet

Quick recap

quick ref
  • RAG = primary defense
  • temp=0 for facts
  • 'Only use provided context'
  • 'Say I don't know if unsure'
  • Validate citations exist
  • Measure with eval datasets

Common Mistakes

  • Trusting LLM output without verification for critical decisions
  • Not using RAG when factual accuracy is required
  • Assuming lower temperature eliminates hallucination
  • No evaluation to measure hallucination rate in production