Open the WaytoAGI knowledge base on Feishu · 100M+ visits, open to everyone →
WaytoAGI

Knowledge Base · Learning paths

Why Do Large Language Models Hallucinate? A Token-Level Explanation

Large language models generate answers token by token. Because they must always choose the most likely next token—even when they do not know the correct one—they can produce confident but fabricated guesses, and errors can cascade while the final text hides the model’s uncertainty.

LLM hallucinationstoken-level explanationlarge language model hallucinationAI hallucination causestoken prediction

Translated from Chinese by AI. Spotted an issue? Use the feedback button.

🔗 Original link: Why Do LLMs Hallucinate? The Token-Level Explanation

Amit Shekhar · Outcome School · 2026-10-14 08:00

📌 This article is a repost. Copyright belongs to the original author. The full text is governed by the original link.

LLMs hallucinate because they generate answers by choosing one token (a small piece of text) at a time — that is, by picking the most likely next token. When they don't know the correct token, they still have to choose one, so a confident-sounding guess comes out.

In this blog, we'll look at why LLMs hallucinate at the token level. We'll also learn what a token is, how LLMs choose the next token, the difference between a sharp guess and a flat guess, why a model can't skip or retract a token, how one wrong token leads to an entirely wrong answer, and how to reduce hallucinations in the real world.

We'll cover the following:

  • What is a hallucination?

  • What is a token?

  • How do LLMs choose the next token?

  • Simple tokens vs factual tokens

  • When the model knows: a sharp guess

  • When the model doesn't know: a flat guess

  • Choosing tokens in code

  • Reason 1: There is no "skip" option

  • Reason 2: Randomness picks a token from the flat guess

  • Reason 3: Tokens cannot be retracted

  • Reason 4: One wrong token leads to more wrong tokens

  • Reason 5: Possible token combinations can produce content that doesn't exist

  • Reason 6: The final text hides the uncertainty

  • How can we reduce hallucinations at the token level?

I'm Amit Shekhar, founder of Outcome School. I've taught and mentored many developers whose efforts have earned them high-paying tech jobs, helped many tech companies solve their unique problems, and created many open-source libraries used by top companies. I'm passionate about sharing knowledge through open source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

What is a hallucination?

A hallucination is an answer from an AI that sounds correct and confident but is actually wrong or fabricated.

The AI isn't lying on purpose. It's just generating content that looks like a genuine answer, and it does so with confidence.

In this blog, "AI" refers to an LLM (Large Language Model).

Suppose we ask the AI: "In what year was the author Meera Joshi born?"

For example, suppose Meera Joshi is a lesser-known author and the AI has never read anything about her. A careful human would say, "I don't know." But the AI replies, "Meera Joshi was born in 1962."

This sentence sounds confident, but the year is a guess. That's a hallucination.

We'll go through the exact moment the wrong answer is generated, token by token. To do that, we first need to understand what a token is.

What is a token?

A token is a small chunk of text that an LLM reads and writes.

LLMs don't see whole words the way we do. They break text into small pieces. A token can be a whole word, part of a word, a number, or even a space with a word attached.

Let's look at how our sentence gets broken into tokens, just to understand:

"Meera Joshi was born in 1962"

→ "Me" | "era" | " Joshi" | " was" | " born" | " in" | " 19" | "62"

Here, we can see:

  • "Meera" is broken into two tokens: "Me" and "era".

  • Common words like " was", " born", and " in" are each a single token.

  • The year "1962" is broken into two tokens: " 19" and "62".

The exact breakdown depends on the model, but the idea is the same everywhere. The model generates answers token by token.

We have a detailed blog post on Tokenization in LLMs that explains step by step how this breakdown works.

Note: For the rest of this blog, for ease of understanding, we'll treat a full year like "1962" as a single token. The idea is exactly the same.

Now, let's see how the model decides the next token.

How does an LLM pick the next token?

An LLM does only one thing, over and over: it looks at all the text so far and predicts the next token.

But it doesn't just predict one token. It gives a score to every token it knows, and those scores become probabilities. That is, the model creates a list of guesses, where each guess has a percentage.

Let's take a simple example. Suppose the text so far is:

"The capital of France is"

The model creates a list of guesses for the next token, like this:

Next token Probability
" Paris" 92%
" a" 3%
" the" 2%
" located" 1%
All other tokens 2%

Then, the model picks one token from this list. Here, it picked " Paris". Now, " Paris" is added to the text, and the whole process repeats for the next token.

So, the process of writing an answer looks like this:

  • Step 1: The model looks at the text so far.

  • Step 2: It creates a list of guesses with probabilities for the next token.

  • Step 3: It picks one token from the list.

  • Step 4: It adds that token to the text.

  • Step 5: It goes back to Step 1.

This loop runs until the answer is complete.

Very important: At no point in this loop is there a step that says "check whether this is true". There is only "pick the most likely next token". That is the root of the problem.

To learn LLM fundamentals, LLM internals, and tokenization, and to build a large language model (LLM) from scratch, check out our AI and Machine Learning course at Outcome School.

Easy tokens and fact tokens

Now, let's look closely at the sentence we hallucinated:

"Meera Joshi was born in 1962."

Not every token in this sentence is equally hard.

  • " was", " born", and " in" are easy tokens. They follow the rules of language. Any sentence about a birth year will have them. The model gets them almost always right.

  • " 1962" is a fact token. It carries actual information. To predict it correctly, the model must genuinely know the fact.

Most tokens in any answer are easy tokens. Only a few tokens are fact tokens. But those few tokens decide whether the answer is right or wrong.

The model uses the exact same loop for both kinds of tokens. It doesn't slow down when it hits a fact token. It doesn't switch to "careful mode". It just creates a list of guesses and picks one, the same way it picks " was".

So, the confident grammar around a fact makes a wrong fact look just as confident.

When the model knows: a sharp guess

When a model has seen a fact many times during training, its list of guesses becomes sharp. In other words, one token gets almost all of the probability.

Suppose the text so far is:

"Albert Einstein was born in"

The list of guesses for the next token looks like this:

Next token Probability
" 1879" 95%
" 1878" 2%
" 1880" 1%
All other tokens 2%

Here, we can see that one token is far ahead of all the others. The model has read about Einstein thousands of times. As a result, this fact is strongly stored inside it.

When the guess is very clear, the model picks the correct token almost every time.

Planning to step into AI?

No matter what technical field you work in today, with effort, you can transition into roles such as:

  • AI engineer

  • Forward deployed engineer

  • AI agent engineer

  • Harness engineer

  • Voice AI engineer

  • AI architect

  • LLM engineer

  • LLM inference engineer

  • Generative AI engineer

  • MLOps engineer

  • Machine learning engineer

  • Deep learning engineer

Our AI and Machine Learning program at Outcome School will help you get there.

When the model does not know: a flat guess

Now let’s return to that little-known author. The text so far is:

"Meera Joshi was born in"

The model has never read anything about her. So its list of guesses looks like this:

Next token Probability
" 1962" 14%
" 1958" 13%
" 1971" 13%
" 1965" 12%
" 1960" 12%
" 1968" 12%
" 1955" 12%
" 1974" 12%

Here, we can see that the probabilities are almost evenly distributed. No token is clearly ahead. This is called a flat guess.

A flat guess is how the model says “I’m not sure.” The model knows the next token must be a year, because sentences like this usually are. But it doesn’t know which year.

Let’s understand this with a simple analogy. Imagine a class of 100 students who have all read about Einstein many times. We ask them, “What year was Einstein born?” Almost all of them shout “1879” in unison. That’s a sharp guess. Now we ask the same class, “What year was Meera Joshi born?” No one knows, so each student shouts a different year. That’s a flat guess.

So, at the token level:

  • Sharp guess = the model knows.

  • Flat guess = the model does not know.

The model has a sense of its uncertainty. It is hidden in these probabilities. But the question now is: how does the model handle a flat guess?

The answer is: it still picks a token. Let’s see how it behaves in practice.

Picking a token in code

The best way to learn this is to look at an example. Let's write Python code that picks a token from a set of guesses, just like an LLM does.

We can write code like this:

import random

sharp_guess = {"1879": 0.95, "1878": 0.02, "1880": 0.01, "other": 0.02}

flat_guess = {"1962": 0.14, "1958": 0.13, "1971": 0.13, "1965": 0.12,
              "1960": 0.12, "1968": 0.12, "1955": 0.12, "1974": 0.12}

def pick_next_token(guesses):
    tokens = list(guesses.keys())
    chances = list(guesses.values())
    # pick one token, where a higher chance means picked more often
    return random.choices(tokens, weights=chances)[0]

print(pick_next_token(sharp_guess))
print(pick_next_token(flat_guess))

Here we have:

  • sharp_guess: Einstein's list of guesses. One token has a 95% probability, and "other" represents the sum of all other tokens.

  • flat_guess: Meera Joshi's list of guesses. The probabilities are spread out.

  • pick_next_token: a function that picks a token, where tokens with higher probabilities are selected more often.

It will print something like:

1879
1971

If we run this code many times, the first print shows "1879" almost every time. But the answer from the second print keeps changing—one time it's "1962", the next time "1971", and the next time "1955".

Here we can see that the function always returns a token. It never returns "I don't know." Even with the flat guesses, it still gives us a year. When that year is written into a sentence, it looks just as confident as "1879".

This is how hallucinations are generated at the single-token level.

Now let's look at each reason in detail.

Reason 1: There is no "skip" option

At every step, the model must choose a token from its list. The list contains tokens like " 1962", " 1958", and so on. There is no special button that says "stop, I don't know this."

Imagine a student taking a fill-in-the-blank test where the rule is that they must write something in every blank. Even if they don't know the answer, they have to write a word. Many of their answers look correct but are actually wrong.

The LLM is in the same situation for every single token.

The question now is: the words "I don't know" are also made up of tokens, so why doesn't the model choose them?

The model will only write them if they are possible next tokens. But there are two problems:

  • During training, the model sees far more text where "a question is followed by an answer" than text where "a question is followed by 'I don't know'."

  • Many training and testing methods often give confident guesses a higher score than an honest "I don't know."

So the tokens for "I don't know" usually get a very small probability.

Therefore, when the guesses for a factual token are flat, the model still picks a year, a name, or a number. It fills in the blank.

Quick tip for you

No matter what technical field you work in, please get familiar with these topics:

  • LLM

  • RAG

  • MCP

  • AI agent

  • Fine-tuning

  • Quantization

We've put all of these together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading—bookmark it and watch it when you have time. Your future self will thank you.

Now, let's get back to the topic.

Reason 2: Randomness picks a token from a flat set of guesses

When a large language model (LLM) picks the next token, there is usually some randomness involved. It doesn't always choose the token with the highest probability. A setting called temperature controls the degree of randomness.

So:

  • Low temperature = the model almost always chooses the highest-probability token.

  • High temperature = the model chooses other tokens more often.

When the guess distribution is steep, randomness doesn't matter. "1879" has a 95% probability, so it wins almost every time.

But when the guess distribution is flat, randomness decides the answer. Each year has roughly a 12% to 14% probability. So if we ask the same question five times, we may get a different year each time, as shown below:

Run 1: Meera Joshi was born in 1962.
Run 2: Meera Joshi was born in 1971.
Run 3: Meera Joshi was born in 1955.
Run 4: Meera Joshi was born in 1962.
Run 5: Meera Joshi was born in 1968.

Here, we can see that each answer is written confidently, but the answers contradict one another. This inconsistency is a strong signal that the model is guessing.

Now, someone might ask: "Then let's set the temperature to zero. The model will always choose the highest-probability token."

Yes, it will now always choose "1962". But "1962" only has a 14% probability. It's still a guess. It's just guessing the same answer every time.

So low temperature makes the answer the same every time, but it doesn't make it correct.

Reason 3: A token cannot be taken back

When we write something by hand, we use an eraser if we find a mistake.

LLMs don't have erasers. Once a token is chosen and added to the text, it stays there forever. The model can only move forward and add the next token.

Let's take an example. Suppose we ask the model:

"Is 3827 a prime number?"

The first token of the answer is a major decision. To illustrate, suppose the list of guesses looks like this:

Next token Probability
"Yes" 52%
"No" 48%

The two probabilities are almost equal. Suppose the model chooses "Yes".

But 3827 is not a prime number. It's 43 times 89.

The model can't go back and change "Yes" to "No". The wrong token has already been written. Everything after it is built on this "Yes".

Reason 4: One wrong token leads to more wrong tokens

Remember, at every step, the model looks at all the text so far, including the tokens it wrote itself. It doesn't go back and doubt its own tokens. It simply treats them as part of the text it must continue.

So, after writing "Yes", the text so far is:

"Is 3827 a prime number? Yes"

Now the model predicts the next token based on this. And the most likely continuation of "Yes" is a reason supporting "Yes". So it writes something like this:

"Yes, 3827 is a prime number because it cannot be divided by any number other than 1 and itself."

Each new token is a reasonable and likely token based on the preceding text. The grammar is correct. The explanation sounds logical. But the entire answer is built on one wrong token at the beginning.

The same thing happens with our author:

"Meera Joshi was born in 1962. She grew up in the 1970s and published her first book in the late 1980s."

Here we can see that the model didn't stop at a wrong year. It used "1962" to build more details: she grew up in the 1970s and published in the late 1980s. All of this flows nicely from 1962, and all of it is fabricated.

One wrong factual token becomes the foundation for subsequent tokens, and a small guess turns into a complete fabricated story.

If you want to dive deeper into the LLM internals and Transformer architecture that determine how each new token is built on the previous ones, check out our AI and Machine Learning course at Outcome School to learn these topics in depth.

Reason 5: Plausible tokens combined can produce things that don't exist

Now, let's look at a special kind of hallucination: fake names, fake titles, and fake references.

Suppose we ask the model to recommend a research paper on a certain topic. The model writes something like the following:

"Sharma, A. and Patel, R. (2019). Using AI to Detect Crop Diseases. Journal of Agricultural AI."

Let's look at this token by token:

  • "Sharma" is a common author name. Very likely a single token.

  • "Patel" is also a common author name. Very likely a single token.

  • "2019" is a common year for such papers. Very likely a single token.

  • "Using AI to Detect Crop Diseases" is a natural title for this topic. Each of its tokens is very likely.

  • "Journal of Agricultural AI" sounds like a real journal name. Very likely tokens.

Each token, taken individually, is a reasonable choice. But suppose this paper — with these exact authors, this exact year, and this exact journal — does not exist anywhere.

The only thing the model focuses on is whether each token fits with the previous tokens. There is no check in the loop for whether the complete combination exists in the real world. So it combines plausible tokens into a nonexistent citation.

This is why LLMs are so good at generating references that look real but don't actually exist.

Stay updated: Subscribe to our newsletter to get our latest AI and machine learning blog posts delivered straight to your inbox.

Reason 6: The final text hides the uncertainty

We've learned that the model has some awareness of its uncertainty. A flat guess means "I'm not sure."

After the model picks a token, only that token remains in the text. The probability behind it is discarded.

So when we read the final answer, these two look exactly the same:

  • "1879," with a 95% probability.

  • "1962," with a 14% probability.

Both are just numbers in a sentence. Both are written in the same confident tone. There is no small "14%" written next to the second number.

This means the model is uncertain on the inside, but its text looks certain on the outside. As readers, we only see the text. So just by reading, we cannot tell the certain parts from the guessed parts.

How can we reduce hallucinations at the token level?

Now let's look at how to reduce hallucinations. Each fix targets one of the causes we identified.

Fix 1: Give the model the facts.

If we put the correct information into the question itself—for example, by pasting a document—then the fact becomes part of the text so far. The model can see it. So a flat guess becomes a sharp guess.

This is where RAG comes in. RAG stands for Retrieval-Augmented Generation. Simply put, first find the right information, then provide it to the model along with the question.

Fix 2: Make "I don't know" a possible token.

The companies building these models can train them to say "I don't know" when they're uncertain, and give an honest "I don't know" a higher score than a confident wrong guess. Then, when the guesses for a fact token are flat, the "I don't know" tokens get a higher probability. This addresses cause 1.

Fix 3: Look at the probabilities behind the tokens.

Many AI tools can show the probability behind each token. If a fact token (such as a year or a name) is chosen with low probability, we can flag it as "possibly a guess". This brings back the uncertainty that the final text hides.

Similarly, we can ask the same question multiple times, as we saw in cause 2. If the fact token keeps changing between answers, the model is guessing. This addresses cause 6.

Fix 4: Make the model think before answering.

If the model first writes down its reasoning step by step, then the key decision doesn't depend on a single uncertain token at the start. For example, the model can first try dividing 3,827 by smaller numbers, find 43 * 89, and then write "No". This doesn't fully solve the problem, but it gives the model a better chance of reaching the correct token. This addresses causes 3 and 4.

Fix 5: Use a lower temperature for facts.

For factual tasks, a lower temperature reduces random jumps toward less likely tokens. But remember, it only makes the answer the same every time, not correct. So use it together with the other fixes, not on its own. This addresses cause 2.

Fix 6: Always verify fact tokens.

As users, we now know that names, numbers, dates and citations are fact tokens. Hallucinations mainly occur in these tokens. So before trusting an answer, these are the places we must check carefully.

Note: None of these fixes completely eliminates hallucinations. As long as the model writes by choosing the next likely token, there will always be the possibility of a confident guess.

By now we should understand why LLMs hallucinate at the token level: the model chooses one likely token at a time, and when it doesn't know the answer, it still has to choose one.

Frequently Asked Questions

Is an LLM lying when it hallucinates?

No. An LLM is not deliberately lying when it hallucinates. At each step it selects a likely next token, and when it doesn't know the correct token, it still has to pick one. The result is a guess that sounds correct and confident but is actually wrong or made up.

Which parts of an LLM answer are most likely to be hallucinated?

Factual tokens such as names, numbers, dates and citations are the most likely to be hallucinated. Simple tokens like "was" and "in" follow language rules, so the model is almost always right about them. Factual tokens require the model to actually know the fact, so these are the places we must check carefully.

How can we tell when an LLM is guessing?

We can look at the probability behind each token, since many AI tools can display this. If a factual token such as a year or a name was selected with low probability, it is very likely a guess. We can also ask the same question multiple times: if the factual tokens keep changing between answers, then the model is guessing.

Does setting temperature to zero stop hallucinations?

No. Setting the temperature to zero makes the model always choose the highest-probability token, so the answer is the same every time. But if the highest-probability token has a very small probability, it is still a guess — just the same guess every time. A low temperature makes answers consistent, but it does not guarantee correctness.

Can we remove hallucinations completely?

No. Fixes such as RAG, training the model to say "I don't know", and checking the probabilities behind tokens can reduce hallucinations, but none of them can eliminate them completely. As long as a model generates text by picking the next likely token, there will always be the possibility of a confident guess.

That's all for now.

Thank you

Amit Shekhar Founder @ Outcome School

You can connect with me on:

Follow Outcome School: