Gen AI Tutorial menu

AI Hallucinations

A hallucination is when an AI gives an answer that sounds correct but is false or made up.

Why Do Hallucinations Happen?

Remember: a language model predicts likely text, not checked text. If it does not know something, the most "likely sounding" answer may still be invented. Reasons include:

  • The fact was missing or rare in its training data.
  • The question is about events after its knowledge cutoff.
  • The prompt is vague, so the model guesses what you mean.
  • It tries to be helpful and fills gaps instead of saying "I don't know".

Modern models hallucinate less than early ones, especially when they can search the web, but the problem has not disappeared.

Common Types

TypeExample
Fake factsWrong year for an event, wrong capital, wrong formula
Fake sourcesA book, research paper or court case that does not exist
Fake quotesWords a famous person never said
Wrong numbersA sum or percentage that is slightly off
Fake codeA function or library option that does not exist
Wrong details about youMixing you up with someone with a similar name

A Simple Way to Spot Risky Answers

Any answer with specific names, numbers, dates, links or quotes needs checking. This small checker flags such details in a reply:

Example

python
import re

reply = "The bridge opened in 1984 and cost Rs 250 million, says Dr Khan."

numbers = re.findall(r"\d+", reply)
has_quote_source = "says" in reply or "said" in reply

print("Numbers to verify:", numbers)
print("Named source to verify:", has_quote_source)
Output
Numbers to verify: ['1984', '250']
Named source to verify: True

The tool cannot tell if the facts are true. It only shows you what to check.

How to Reduce Hallucinations

  1. Give the source: paste the document and say "Answer only from this text."
  2. Allow "I don't know": tell the model it is fine to say it is unsure.
  3. Ask for sources, then open and check them yourself.
  4. Use web search or research modes for recent or factual questions.
  5. Ask for low creativity for factual tasks.
  6. Break big questions into smaller ones.

Here is a prompt that uses several of these ideas:

Example

text
Answer using ONLY the notice below. If the answer is not in
the notice, reply "Not mentioned in the notice."
Quote the exact sentence you used.

Notice:
"Admissions for the evening batch close on 30 November.
Classes are held Monday to Thursday, 5 pm to 7 pm."

Question: What is the fee for the evening batch?

A good model should reply "Not mentioned in the notice." instead of inventing a fee.

Checking Code from AI

AI-written code can call functions that do not exist or have hidden bugs. Always run it and test it:

Example

python
def is_even(n):
    return n % 2 == 0

for n in [0, 3, 10]:
    print(n, is_even(n))
Output
0 True
3 False
10 True

Testing with a few known answers is the fastest way to catch a mistake.

Never use AI answers alone for medical, legal or financial decisions. Use them to prepare questions, then confirm with a qualified person or an official source.

Exercise

Exercise

Which prompt is most likely to reduce hallucinations?

Learn Gen AI with a teacher

Join the AI & Prompt Engineering course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp