Avoiding Hallucinations & Fact-Checking
Reviewed & published by Brayan K
AI models can state false things with total confidence — this is called a “hallucination”. They predict plausible-sounding text, not verified truth, so a wrong answer can arrive as smoothly as a right one. Knowing this changes how you use AI.
Part of the free Prompt Engineering course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
This lesson explains why models make things up, the prompts that reduce it, and the habit that matters most: verifying anything that counts.
What You'll Learn in This Lesson
- What a hallucination is and why models make things up
- Giving the AI permission to say 'I don't know'
- Which facts are riskiest: dates, stats, recent events
- Asking for sources — and why you must verify them
- Checking high-stakes claims with authoritative sources
- Treating AI answers as a draft to verify, not gospel
1 Why AI makes things up
An AI model generates the most plausible next words — not checked facts. If a wrong answer looks likely, it can come out just as confidently as a true one. There’s no built-in “am I sure?” alarm.
Give me the exact population of
this small town in 2023 and the
name of its mayor.Obscure, precise, and recent — the perfect storm for a confident made-up answer.
What's the population of this town?
If you're not confident, say so and
tell me where I could verify it.
Don't guess at exact numbers.You invite honesty and a path to verify, instead of forcing a guess.
2 Which answers to double-check
| Risk | Examples |
|---|---|
| High | Exact dates, statistics, quotes, citations, very recent events, legal/medical/financial claims. |
| Medium | Obscure names, niche technical details, “who said this” attributions. |
| Low | Common knowledge, definitions, general explanations, brainstorming. |
The more specific, recent, or consequential the fact, the more it deserves a second source.
3 Ask for sources — then verify them
Asking “what’s your source?” is good practice — but models can invent realistic-looking citations and URLs. Treat sources as leads to check, not proof:
- Click through — does the source exist and actually say that?
- Cross-check — does a second, independent source agree?
- Ask for confidence — “How confident are you, and what might make this wrong?”
- Go to a pro — for medical, legal, or financial decisions, confirm with an authoritative source or a professional.
📋 Copy-paste fact-checking template
Answer my question, but:
- If you're not confident, say so plainly.
- Don't invent numbers, names, or citations.
- List any sources I can check myself.
- Note how confident you are and what could
make this answer wrong.Try it for real
You cannot stop a model inventing things, but you can refuse to ship a claim no source supports — and that check is about fifteen lines. Watch the fourth claim especially: it cites a real policy, confidently, and that policy says nothing of the kind. Asking for citations is half the job; verifying them is the half that catches this.
# You cannot stop a model inventing things. You CAN refuse to ship a claim that
# no source supports. That check is code, and it is short.
sources = {
"policy-1": "Refunds are available within 30 days of delivery.",
"policy-2": "Faulty goods are collected free of charge.",
}
# A reply where every sentence carries the source it came from.
claims = [
("Refunds are available within 30 days.", "policy-1"),
("Collection of faulty goods is free.", "policy-2"),
("Store credit never expires.", "policy-3"), # invented source
("Refunds take 24 hours to process.", "policy-1"), # real source, absent fact
]
def key_terms(text):
stop = {"are", "is", "the", "of", "to", "a", "within", "take", "never"}
return {w.strip(".,").lower() for w in text.split()} - stop
for text, src in claims:
if src not in sources:
print(f"REJECT {text!r}\n cites {src}, which does not exist")
continue
overlap = key_terms(text) & key_terms(sources[src])
if len(overlap) >= 2:
print(f"OK {text!r}\n supported by {src} (shared: {sorted(overlap)})")
else:
print(f"REVIEW {text!r}\n cites {src} but shares little with it: {sorted(overlap)}")
print()
print("The third claim invents a source, which is trivially catchable. The fourth")
print("is the dangerous one: a real source, confidently cited, that does not")
print("actually contain the fact. Requiring citations is only half the job —")
print("checking them is the other half.")# 🎯 YOUR TURN — finish the citation checker.
sources = {
"faq-1": "Delivery takes 3 to 5 working days.",
"faq-2": "We ship to mainland UK only.",
}
claims = [
("Delivery takes 3 to 5 working days.", "faq-1"),
("We ship worldwide.", "faq-2"),
("Next day delivery is free.", "faq-9"),
]
def check(text, src):
# 1) A citation to a source you do not have is an automatic reject.
if src ___ sources:
# 👉 replace ___ with not in
return "REJECT (no such source)"
words_claim = {w.strip(".,").lower() for w in text.split()}
words_source = {w.strip(".,").lower() for w in sources[src].split()}
# 2) Support means the claim and its source share real wording.
overlap = words_claim ___ words_source
# 👉 replace ___ with &
return "OK" if len(overlap) >= 3 else "REVIEW (weak support)"
for text, src in claims:
print(f"{check(text, src):<24} {text}")
# ✅ Expected output:
# OK Delivery takes 3 to 5 working days.
# REVIEW (weak support) We ship worldwide.
# REJECT (no such source) Next day delivery is free.Common Mistakes (and the fix)
- Trusting a confident tone. — Sounding sure isn’t being right; verify the facts.
- Accepting citations at face value. — Click through; models can invent sources.
- Forcing an exact answer. — Let the AI say “I don’t know” instead of guessing.
- Not verifying high-stakes facts. — Medical, legal, and financial claims need an authoritative check.
⏱ Test Yourself — Timed Quiz
10 quick questions, 12 seconds each. Instant feedback — beat the clock!
🎉 Lesson Complete
- ✅ A hallucination is a confident but false answer
- ✅ Models predict plausible text, not verified truth
- ✅ Add “say you don’t know rather than guessing”
- ✅ Verify dates, stats, quotes, and recent events
- ✅ Ask for sources — then check they’re real and say what’s claimed
Practice quiz
What is an AI 'hallucination'?
- A visual glitch
- A type of prompt
- When the model states something false but sounds confident
- A login error
Answer: When the model states something false but sounds confident. A hallucination is a confident-sounding answer that is actually wrong or made up.
Why do AI models sometimes make things up?
- They predict plausible-sounding text, which isn't always true
- They lie on purpose
- They are broken
- They run out of memory
Answer: They predict plausible-sounding text, which isn't always true. Models generate likely text, so a plausible-but-false answer can slip through.
Which prompt reduces made-up facts?
- 'tell me anything'
- 'be creative'
- 'make it up'
- 'If you're not sure, say you don't know rather than guessing'
Answer: 'If you're not sure, say you don't know rather than guessing'. Inviting 'I don't know' gives the model permission not to fabricate.
How should you treat names, dates, and statistics from an AI?
- Trust them fully
- Verify them against a reliable source
- Ignore them
- Assume they are jokes
Answer: Verify them against a reliable source. Specific facts are the most likely to be wrong — always verify them.
Asking the AI for sources is useful because…
- You can check the claim — but verify the sources are real, too
- It looks nice
- It makes answers longer
- It changes nothing
Answer: You can check the claim — but verify the sources are real, too. Sources help you verify, but models can invent citations, so check them.
Can an AI invent a fake citation or URL?
- Never
- Only on purpose
- Yes — made-up references are a known failure
- Only for code
Answer: Yes — made-up references are a known failure. Models can fabricate realistic-looking but nonexistent sources.
For a high-stakes fact (medical, legal, financial), you should…
- Trust the AI alone
- Double-check with an authoritative source or a professional
- Skip checking
- Ask once and move on
Answer: Double-check with an authoritative source or a professional. High-stakes facts warrant verification with an authoritative source.
Which question helps you gauge confidence?
- 'are you a robot'
- 'be louder'
- 'hurry up'
- 'How confident are you, and what might make this wrong?'
Answer: 'How confident are you, and what might make this wrong?'. Asking about confidence and failure modes surfaces shaky answers.
A model is most likely to hallucinate about…
- Common, well-known facts
- Obscure details, very recent events, or precise numbers
- Simple greetings
- Basic math like 2+2
Answer: Obscure details, very recent events, or precise numbers. Niche, recent, or highly specific facts are the riskiest.
Best mindset toward AI answers on facts?
- Blind trust
- Total dismissal
- Treat them as a confident first draft to verify
- They are always wrong
Answer: Treat them as a confident first draft to verify. Useful starting point, but verify before you rely on it.
Continue this course
- Previous: Iterating & Refining Prompts
- Next: Reusable Prompt Templates & Cheat Sheet — Copy-paste templates and a one-page recap of every technique
Frequently asked questions
What is an AI 'hallucination' and why does it happen?
A hallucination is when the AI states something false while sounding completely confident. It happens because these models generate the most plausible-sounding text, not verified truth. If a wrong answer looks likely given the words around it, the model may produce it just as smoothly as a correct one — there's no built-in 'am I sure?' check.
How do I get the AI to admit when it doesn't know?
Give it permission explicitly: add 'If you're not certain, say so instead of guessing' or 'Only answer if you're confident; otherwise tell me you don't know.' Without that nudge, models tend to fill the silence with a confident-sounding guess.
Can I trust the sources an AI gives me?
Treat them as leads, not proof. Asking for sources is good practice, but models can invent realistic-looking citations, authors, and URLs that don't exist. Always click through and confirm the source is real and actually says what the AI claims.
Which answers most need fact-checking?
Specific names, dates, statistics, quotes, legal or medical or financial claims, and anything about very recent events. Those are exactly where hallucinations cluster. Common, well-known facts are usually safe; precise or obscure details deserve a second source.