0% completed
Why Do LLMs Hallucinate?
On This Page
Part 1: Prediction Is Not Lookup
Part 2: Four Situations That Produce a Guess
Part 3: Why the Tone Does Not Change
Part 4: What Lowers the Rate
Part 5: Why the Rate Does Not Reach Zero
Common Wrong Beliefs
What This Means for Your Work
Try It Yourself
Check Your Understanding
Key Takeaways
Short answer: A large language model (LLM) always produces the likely next pieces of text (tokens), and it has no step that checks them against a source. When the model does not have a fact, the likely text is a well-written guess. The guess is written in the same confident tone as a true statement, and it is called a hallucination. Giving the model the source text lowers the rate of such guesses, but no method brings the rate to zero.
Look at the box in the middle. Both questions pass through the same loop, and nothing in that loop separates the true answer from the invented one.
This lesson follows the second question. An engineer wants an HTTP client that waits a random extra time (jitter) before each retry, and asks a chat assistant:
How do I use the retry_with_jitter function of the Python requests library?
The requests library has no function with this name. A model can still answer as follows (an illustrative reply, which means that it was invented for this lesson):
Call
requests.retry_with_jitter(url, max_retries=3, base_delay=0.5, jitter=0.2). Themax_retriesparameter sets the number of attempts, andjitteradds a random delay between them.
Part 1: Prediction Is Not Lookup
What Happens Inside an LLM When I Ask a Question? describes the loop that produces every answer. In each run of the model (a pass), the model gives every possible next token a chance between 0 and 1, and the program chooses one token. No step in this loop opens a document or compares the answer with a document.
A database behaves differently. A query for a record that does not exist returns zero rows, and the application can report "not found". A model has no empty result, because its list of chances always contains a most likely token. The model writes "this function does not exist" only when those words are the likely next text (the likely continuation).
After the text requests.retry_with_jitter(, the likely next tokens are parameter names such as url and max_retries, because retry functions in many libraries have such parameters.
Notice the error on the right side. Python reports that the module requests has no attribute retry_with_jitter, but the answer on the left contains no word of doubt.
The invented name is not random. The real way to configure retries in requests uses a class named Retry from the urllib3 library, and that class has a parameter named backoff_jitter. The words "retry" and "jitter" therefore appear near requests in real code, and the invented function is a likely combination of real pieces.
Part 2: Four Situations That Produce a Guess
Four situations make a hallucination likely, and the example contains three of them.
- A rare or absent fact. The model learned little or nothing about the subject from the text it was trained on (its training text). The function
retry_with_jitterappears in no documentation of the library, so the model learned no true description of it. - A fact newer than the training text. A model learns nothing after its training ends, which Why Does a Model Have a Knowledge Cutoff? explains. A question about a function that was released last month receives an answer based on older versions.
- A question that assumes something false. The question states that the function exists. In ordinary text, a question of this form is usually followed by an answer and rarely by a correction, so an answer is the likely continuation.
- A request for exact details. A quotation, a version number, or a URL must be correct in every character. If the engineer asks which release added the function, the model can produce a version number that has the right format but that no source confirms.
Researchers at OpenAI describe the first situation with a comparison. Spelling and parentheses follow patterns that a model can learn, but some rare facts, such as the birthday of a pet, follow no pattern that helps a model predict them.
Part 3: Why the Tone Does Not Change
A person who guesses usually sounds less sure. A model usually does not, and there are three reasons.
First, the tone is also predicted text. Documentation and tutorials state things with confidence, so confident sentences are the likely continuation of a question about requests. The tone comes from the style of the training text and not from the amount of evidence for the answer.
Second, the reader receives only the chosen tokens. In one pass, several possible tokens can have nearly equal chances, but the answer contains only the chosen one. The reader therefore cannot see how close the other chances were.
Third, training often rewards a guess. Researchers at OpenAI argued that many common tests give a point for a correct guess and nothing for the answer "I do not know". A model that is trained to score well on such tests learns to guess.
Researchers at Anthropic examined one of their models and reported a related finding. Inside that model, refusing to answer was the default behavior, and an internal signal for a familiar name stopped this behavior. A false answer could appear when a name was familiar and the model had no facts about it.
Part 4: What Lowers the Rate
Four methods make a guess less likely. Each one changes what the model receives or what it is allowed to answer.
Look at the four cards from left to right. They apply to the example as follows:
- Put the source text in the input. Paste the documentation page of
requestsabove the question. The likely next tokens then come from text that the model is reading, and that text does not contain the invented function. An application can fetch such text automatically, which What Is RAG and When Do I Need It? covers. - Allow "I do not know". Add one sentence such as "If you are not sure that this function exists, say so." The sentence makes an answer such as "I am not sure" a likely continuation.
- Ask for a quotation from the source. Ask the model to quote the exact line of the documentation that supports each claim. A claim without a quotation is unsupported. Can I Trust a Source That the Model Cites? explains how to check such citations.
- Use a search tool. Many assistants can search the web, and the application places the results in the input. The answer is then based on current pages and not only on training text.
Part 5: Why the Rate Does Not Reach Zero
All four methods change the input, but the same loop still produces the answer. The model can use the source incorrectly, mix it with something from its training text, or write a quotation that the source does not contain. A search can also return a page that is wrong.
Allowing the answer "I do not know" has a disadvantage as well, because the model can then refuse a question that it would have answered correctly. The documentation of the companies that run models (providers) states the limit plainly: these techniques reduce hallucinations and do not remove them. Newer models usually hallucinate less often than older ones, but the cause remains, so the facts that matter in an answer still need a check.
Common Wrong Beliefs
| Belief | What actually happens |
|---|---|
| A hallucination is a bug that the next model version will remove. | A hallucination is a result of the way every answer is produced. Newer models usually hallucinate less often, but the cause is still present. |
| A model sounds less sure when it is guessing. | The tone is predicted text, like every other part of the answer. A guess and a true statement usually have the same tone. |
| A model hallucinates only about rare subjects. | A model also invents details about very popular libraries, as the example with requests shows. |
| An instruction such as "do not invent anything" stops hallucinations. | The instruction lowers the rate. The answer still comes from the same loop, so some invented details remain. |
What This Means for Your Work
- Run the code before you trust it. A call to a function that does not exist fails when that call runs. This is the cheapest check that you have.
- Check that a package exists before you install it. Researchers at three universities generated 576,000 code samples with 16 models. On average, at least 5.2 percent of the packages recommended by commercial models did not exist. For open models, which anyone can download, the number was at least 21.7 percent.
- Give the model the documentation. For a private API, an internal service, or a new library, paste the relevant page or file into the input.
- Treat exact details as unverified. Check every version number, URL, quotation, and configuration key against its source.
Try It Yourself
The box below sends its text to a real LLM. Run it once and read the answer. The model may describe parameters for the function. It may also answer that the function does not exist, because newer models make this mistake less often.
Now add this sentence to the end of the text and run the box again: "If you are not sure that this function exists, say that you do not know." The answer should now say that the function does not exist, or that the model cannot confirm it.
You can check without a model that the function does not exist. Run import requests and then requests.retry_with_jitter in a Python shell, and Python should report an AttributeError.
Check Your Understanding
A company wrote an internal deployment tool last month. An engineer asks a chat assistant which configuration key sets the timeout of that tool, and pastes no file. The assistant names a key and explains it in a confident tone. What most likely happened?
Key Takeaways
- A model predicts text and does not search a source for facts. Its list of chances always contains a most likely token, so it can always produce an answer.
- A guess has the same tone as a fact. The tone is predicted from the style of the training text and does not measure evidence.
- Four situations produce most hallucinations. They are a rare or absent fact, a fact newer than the training text, a false assumption in the question, and a request for exact details.
- Source text in the input lowers the rate and does not remove it. Verify the names, numbers, quotations, and links that matter.
The next lesson, Why Does the Same Question Give a Different Answer Each Time?, explains why two runs of one question can produce two different answers.
Reading Progress
0%
On This Page
Part 1: Prediction Is Not Lookup
Part 2: Four Situations That Produce a Guess
Part 3: Why the Tone Does Not Change
Part 4: What Lowers the Rate
Part 5: Why the Rate Does Not Reach Zero
Common Wrong Beliefs
What This Means for Your Work
Try It Yourself
Check Your Understanding
Key Takeaways