Hallucinations
Understand why a language model sometimes gives confident answers that are wrong or made up, and the practical ways to reduce the risk with clear prompts, context, RAG, tool calling and verification.
An LLM does not search the internet each time you ask something. It does not look up the right answer in a database. As the transformer page showed, it generates a response one token at a time, from the patterns it learned in training.
That raises an obvious question. If these models are so capable, why are they sometimes completely wrong?
What a hallucination is
A hallucination is when an AI model produces information that looks confident and convincing and is in fact incorrect or entirely made up.
Two examples from the video:
| You ask | What can happen |
|---|---|
| "Who won the FIFA World Cup in 2038?" | The tournament has not taken place. The model may name a winner anyway |
| "Give me the built-in Java method that does this" | The model may answer with a method name that sounds right and exists in no Java API |
The second kind is the one developers meet most often. A method that does not exist, a configuration property nobody defined, a library version that was never released. All of them look exactly like real ones.
That is what makes hallucinations dangerous. A wrong answer does not come with a warning. It has the same tone, the same formatting and the same confidence as a correct one.
Why it happens
The reason is short:
An LLM predicts the next token. It does not verify facts.
Producing text is the model's whole job. When it writes an answer, it is choosing likely tokens, one after another. No step checks whether the sentence it has built is true.
So when the model does not have enough information, or the question is ambiguous, it does not stop. It continues with the most plausible-looking text, and the result can be an invention.
A hallucination is therefore not a rare malfunction. It follows directly from how a language model works.
Ways to reduce it
Hallucinations cannot be switched off. They can be made much less likely, and the rest can be caught. The video's slide groups the measures in two blocks.
Reduce the chance
| Measure | What it does | Where you learn it in this course |
|---|---|---|
| Clear, specific prompts | Removes ambiguity. A precise question leaves less room for guessing | Prompt Templates |
| Provide context | Gives the model the facts up front. A system prompt can also set how it should behave, including what to do when it does not know | ChatModel Configuration |
| RAG | Retrieves the relevant parts of trusted documents and hands them to the model with the question | Covered in the separate LangChain4j RAG lesson |
| Tool calling | Lets the model fetch live data from APIs and databases, so that it does not depend on what it remembers from training | Tool Calling and MCP Explained |
The last two rows change what a model can do. With RAG, tools and MCP, a model is no longer limited to its training data. It can work with your documents and with the current state of real systems.
Catch what still gets through
The second block of the slide has one line:
Verify important output, every time.
No measure in the first block is perfect. For anything that matters, a number in a report, a medical or legal statement, code that goes to production, a person or another check has to confirm the result.
The slide closes with a sentence that sets the right expectation:
None of these eliminate hallucination. They shrink it, and catch the rest.
What this means when you write code
Three habits follow from this page, and the later lessons use all of them.
Tell the model it is allowed not to know. A system message such as "If you do not know the answer, say that you do not know" gives the model a correct way out. Without it, the most likely continuation of a question is an answer.
Give the model facts, do not test its memory. If the answer depends on your data or on something that changes, supply it through context, a tool or a document. Asking a model to recall your order status or today's price is asking for an invention.
Treat the output as a draft. Check a method name in the documentation before you use it. Compile the code. Run the query. In this course, every program is run, and you will see more than once that a confident answer was wrong.
Summary
- A hallucination is incorrect or made-up information that looks convincing.
- It happens because an LLM predicts the next token and does not verify facts.
- Clear prompts, context, RAG and tool calling reduce the chance.
- Important output still has to be verified.
This is the end of the theory. From the next page on, everything is code.
Try It Yourself
- Ask a chat assistant about an event in the far future, as if it had already happened. Does it correct you, or does it play along?
- Ask for "the Java method in the String class that reverses a string". Check the answer in the official Java documentation. Does a method with that exact name exist?
- Ask the same factual question with and without the sentence "If you are not sure, say so". Compare the answers.
- Ask a question about your own company or school that the model cannot know. Then paste the relevant text into the prompt and ask again.
- Think of an application you would like to build. List three kinds of output in it that would need verification before a user sees them.
Official references
Written By: Shramik Masti
How is this guide?
Last updated on
Open and Closed Models
Compare closed models that you reach through a provider's API with open-weight models that you download and run yourself, and learn how to choose between them by data, cost, control and convenience.
Setup for Java AI Applications
Create an OpenAI API key on the API platform, give it a short expiry, add prepaid credit, and learn the habits that keep the key and your bill safe before you write any Java code.
