Sub topic 1
How a model writes: predicting the next token
Prerequisites
This chapter assumes three things:
- You have used a chat-based LLM assistant.
- You can read short Python code. The one example is optional.
- You know that models are trained on large collections of text.
The core idea
A large language model (LLM) writes by repeating one step: given the text so far, choose the next token. A token is a chunk of text, often a short word or part of a word. "Unbelievable" might be split into "un", "believ", and "able".
The model does not plan a full answer and then write it down. It produces one token, adds it to the text, and predicts again. A reply of 200 words comes from several hundred of these steps.
What the model computes at each step
At each step the model outputs one raw score, called a logit, for every token in its vocabulary. A function called softmax turns those scores into probabilities that add up to 1. The next token is then picked from that distribution.
Here is softmax in Python for three candidate tokens. The temperature setting divides the scores before softmax. Later chapters cover temperature in detail. For now, notice that the probabilities come entirely from the scores.
import math
def softmax(logits, temperature=1.0):
scaled = [x / temperature for x in logits]
m = max(scaled)
exps = [math.exp(x - m) for x in scaled]
total = sum(exps)
return [e / total for e in exps]
logits = [2.0, 1.0, 0.5]
print(softmax(logits, temperature=1.0))
print(softmax(logits, temperature=0.5))The first line prints roughly [0.63, 0.23, 0.14]. The second prints roughly [0.82, 0.11, 0.04]. The lower temperature concentrates probability on the top candidate, but the ranking stays the same.
Where the scores come from
During training, the model reads huge amounts of text. It repeatedly tries to predict the next token and is adjusted when it is wrong. Over time it learns patterns: grammar, common facts, writing styles, and how topics relate.
Take the prompt "The capital of France is". The token " Paris" gets a high score because that continuation appeared constantly in training text. The model has not looked anything up. It learned that this pattern is very likely.
What is missing from the loop
Look again at the diagram. Nothing in the loop checks the chosen token against the world. No step asks "Is this true?" or "Do I have a source?" The only question the model answers is "Which token is likely to come next, given this text?"
Often, likely and true line up, because the training text was mostly accurate on common topics. When they diverge, the model still produces confident, well-formed text. That gap is the root of hallucination, and the next chapters build on it.
Consequences you can observe
- Fluent errors. A wrong answer reads as smoothly as a right one, because fluency is what the training rewarded.
- Early tokens constrain later ones. Once the model writes "The study by Dr.", it must continue with some name. Stopping to say "I'm not sure" is now less likely than finishing the pattern.
- Variation between runs. Because the next token is sampled from a distribution, asking the same question twice can give different text.
Exercise
A model is given "The first person to walk on the moon was" and the token " Neil" gets the highest probability. Does that probability show the model verified the fact? Explain in one or two sentences.
Show answer
No. The probability reflects how often similar text followed such a phrase in training data. The model has no step that checks facts against a source. In this case the likely token also happens to be correct.
Exercise
Using the softmax code above, what happens to the probability of the top token if you raise the temperature to 2.0? Does the order of candidates change?
Show answer
The top token's probability falls, because dividing the scores by a larger number makes them closer together and the distribution flatter. The order of candidates does not change.