AI Hallucinations
Table of Contents + β
In the last lesson, you learned AI Inference and Generation Settings. Now let us talk about hallucinations. In AI, this means the model gives a wrong answer that sounds correct.
Here is why hallucinations can happen:
β οΈ What Is an AI Hallucination?
An AI hallucination is when a model gives incorrect information but presents it confidently.
Confidently means it does not sound unsure. It may write the wrong answer in a very polished way.
Let us understand with clearer examples:
- Invented book title: You ask for books by an author. The model gives a title that sounds real, but that book does not exist.
- Wrong date: You ask when a product launched. The model gives a confident date, but the actual launch date is different.
- Fake source: You ask for proof. The model writes a source name or link that looks official, but the source is not real.
- Fake code API: You ask how to use a library. The model suggests a method name that looks correct, but that method is not available in the real library.
- Guessing from memory: You ask about your companyβs refund policy, but the policy was never provided. The model still answers instead of saying it does not have the document.
The dangerous part is not only that it is wrong. The dangerous part is that it can sound very sure.
Wrong fact
The answer may include a name, number, date, or rule that is not true.
Confident tone
The answer may sound sure, even when the information is missing.
Better app behavior
A good app shows the source it used or says "I do not have enough information" instead of guessing.
For beginners, this is the main lesson:
- Good English does not mean correct information: The model can write smoothly even when the fact is wrong.
- A detailed answer does not mean a true answer: Extra details can still be invented.
- A confident answer does not mean a verified answer: The model may not show doubt even when it should.
π Hallucinations in Real Chat Apps
Fake source
ChatGPT, Claude, Gemini, or any LLM can mention a source or link that sounds real, but it may not exist.
Fake code API
A coding assistant may suggest a method name that looks valid but is not in the library.
Wrong policy answer
A support bot may guess a refund rule if the real policy was not provided.
π¨ Why Hallucinations Are a Production Problem
In a demo, a wrong answer may look like a small mistake.
In a real app, it can create serious issues.
- Support bot: A customer asks for a refund. If the bot guesses and promises a refund that company policy does not allow, the business has a real problem.
- Coding assistant: A developer copies a fake method suggested by the model. The code fails because the method does not exist.
- Legal assistant: A user asks for legal references. If the model invents a case name, the user may trust a fake source.
- Medical or financial assistant: A user asks a serious question. A wrong answer can harm the user, so the app must be extra careful.
- Document bot: A user asks from a PDF. If the bot cites the wrong section, the answer looks grounded but is still wrong.
So the goal is not βmake the answer sound niceβ. The goal is to make the answer supported.
π€ Why Do Hallucinations Happen?
LLMs generate likely text. They do not guarantee truth.
Likely text means text that fits the pattern of the prompt and training.
Truth needs checking against reliable sources.
Common causes:
- Missing information: The model was not given the document, database result, or live source needed for the answer.
- Unclear prompt: The user asks a vague question, so the model guesses what they meant.
- Recent or private data: The question depends on information the model does not automatically know.
- Weak context: The app sends irrelevant or conflicting text, so the model builds an answer from poor input.
- Over-helpful behavior: The model may try to answer instead of saying it does not have enough evidence.
The model is optimized to continue
This is the mental model:
- The model receives text: It only sees the prompt and context the app sends.
- It predicts likely next tokens: It chooses text that looks likely based on input and training.
- It continues until the answer is complete: It keeps generating more tokens until it stops.
- Missing evidence can still produce output: If the prompt asks for something missing, the model may generate a likely-sounding answer anyway.
That is why βdo not hallucinateβ alone is weak. You need better context and checks.
So the fix is not just one magic prompt. The app must provide evidence.
Missing context causes guessing
If the model does not have the source, it may still produce a likely answer.
Question:What does our company's refund policy say about digital products?
Bad setup:No refund policy document is provided.
Better setup:Retrieve the refund policy section and ask the model to answer from it.The fix is not just βbetter wordingβ. The model needs the right information.
π οΈ How Developers Reduce Hallucinations
You cannot remove hallucinations completely, but you can reduce them.
Better prompting |RAG |Tool calling |Grounding |EvaluationHere is what each part does:
- Better prompting tells the model what to do and what not to do.
- RAG gives the model relevant documents, so the answer can come from real text instead of guesswork.
- Tool calling lets the app fetch live or private data.
- Grounding ties answers to trusted sources, like a policy page, database row, or search result.
- Evaluation tests output quality before and after changes.
β A Practical Hallucination Checklist
Before showing an important answer, ask these questions:
- Did the answer use trusted context? Check whether the app actually provided the needed document or tool result.
- Does the answer point to the source? For document answers, show where the answer came from.
- Did the model avoid unsupported claims? The answer should not add facts that are not in the source.
- Are numbers, dates, names, and links checked? These are common places where hallucinations become dangerous.
- Is there a fallback? If evidence is missing, the app should say it cannot answer safely.
This checklist is simple, but it prevents many real mistakes.
π§ͺ How to Evaluate AI Answers
Evaluation means checking whether the AI answer is good enough.
Do not only test one question manually.
For example, keep a small test set:
- Question: What is the refund period?
- Expected source: Refund policy page.
- Good answer should include: 7 days and original payment method.
- Bad answer examples: Invented refund period, missing source, or extra policy not present in the document.
This helps when you change:
- The prompt.
- The model.
- The retrieved documents.
- The temperature or max output tokens.
After each change, run the same test questions again.
If the answers improve, keep the change. If important answers get worse, fix the prompt, retrieval, or app logic before release.
Do not hide uncertainty
For serious answers, it is better for the app to say βI do not have enough informationβ than to generate a confident guess.
π§© What Youβve Learned
- β A hallucination is a wrong answer written confidently.
- β Better context, RAG, tools, source checks, and evaluation reduce hallucination risk.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is an AI hallucination?
Why: Hallucination means the model produces something false but sounds confident.
- 2
What is a common cause of hallucinations?
Why: If the model lacks the required information, it may guess.
- 3
What does RAG help with?
Why: RAG retrieves relevant information so the model can answer from it.
- 4
What is a good production behavior when evidence is missing?
Why: A safe app should refuse or ask for more information instead of guessing.
π Whatβs Next?
When we ask todayβs stock price, the model should not guess from old training data. It needs fresh data from somewhere else. Next, let us understand model knowledge and cutoff.