Hallucinations: causes and concrete mitigations
A confidently wrong AI answer can lose a customer or a court case. This lesson is the practical defense.
A friend who never says "I do not know". Charming, dangerous, eventually fired from any serious job.
Hallucinations happen because LLMs sample probable tokens, not retrieve facts. Causes:
- Asked about post-training events
- Asked about your private data the model never saw
- Long context with the answer in the middle (lost)
- Vague prompts that leave too much to the model
Mitigations:
- RAG with strict refusal rules
- Tool use: search, calculators, DB lookups for facts
- Citations: require sources
- Temperature 0 for factual tasks
- Verifier model: a second LLM that fact-checks against sources
- Human in the loop for high-stakes use cases
A verifier pattern:
def answer_then_verify(question):
ans = generate_with_rag(question)
verdict = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": "Reply YES if the answer is fully grounded in the sources, otherwise NO with reason."},
{"role": "user", "content": f"Sources:\n{ans['sources']}\n\nAnswer:\n{ans['text']}"}]
)
return ans, verdict.choices[0].message.content
Quick recall
3 prompts · think before you flip
Prompt 1 of 3
Why do LLMs hallucinate?
Quiz time
1 question · tap an answer to check it
1. The strongest single defense against hallucination is
Finished lesson 11.1?
Mark complete to update your module progress and unlock the streak.
Loading