Why AI makes things up, and how to catch it
AI hallucinations explained simply: why chatbots invent facts, functions and references, and five habits that help you catch them.
On this page
Sometimes an AI chatbot gives an answer that sounds perfect and is completely wrong: a library function that does not exist, a research paper nobody wrote, or a date that is off by ten years. This is called an AI hallucination. Here is why it happens and how to catch it.
Why hallucinations happen
Remember the core skill of an LLM from our simple guide to how LLMs work: it predicts the most likely next piece of text. It does not look facts up, and it has no built-in sense of "I don't know".
So when it does not have the right information, it still produces text that looks like a correct answer. A made-up reference looks like a real one, because the model has seen thousands of real references and knows their shape.
Everyday comparison: a student in a viva who doesn't know the answer but speaks confidently anyway, using all the right-sounding words.
When it happens most
Hallucinations are more likely when:
- the question is about something rare, like a small library or a local event;
- the question is about recent events after the model's training data ends;
- you ask for exact details: numbers, dates, quotes, links, citations, version numbers;
- the question assumes something false, for example "Why did India win the 1990 Cricket World Cup?" (there was no 1990 World Cup);
- the conversation is very long and the important detail has fallen out of the context window.
What hallucinations look like in code
For students learning to code, the common ones are:
- a function or method that does not exist in the library;
- a correct function from an older or newer version of the library;
- code that runs but quietly does the wrong thing;
- a confident explanation of an error that is not the real cause.
Running the code is the fastest truth test. If AI-written code has not been run and tested, treat it as a draft, not a solution.
Five habits to catch them
- Check exact details at the source. Dates, numbers, quotes and links go straight to the official documentation, textbook or website.
- Ask for sources, then open them. A link that does not work, or a page that says something different, is a red flag.
- Run and test code. Write a small test with an answer you already know.
- Ask the question a different way. If two phrasings give two different "facts", neither is reliable yet.
- Give it the material. Paste the relevant notes or documentation into the prompt and ask it to answer only from that text.
How developers reduce hallucinations
When you build AI apps, you can design for fewer hallucinations:
- Ground the answer in real documents. This is what RAG does: look up the right information first, then answer from it.
- Allow "I don't know". Tell the model it may say it does not know when the answer is not in the provided text.
- Ask for citations that point to the exact document used, so a person can check.
- Use a lower temperature for factual tasks. Temperature does not fix hallucinations, but it makes answers more consistent and easier to test.
Quick recap
- A hallucination is a confident answer that is false or invented.
- It happens because models predict likely text; they do not verify it.
- It is most common for rare topics, recent events and exact details.
- Check details at the source, run code, and give the model the material it needs.