Next-token prediction
Next-token prediction is the task of giving each token of the vocabulary (see Section 5.1) the probability that it comes next in a text. A language model is trained on this task, and it writes a text by repeating it, one token at a time. A written token never changes, so each later token has to continue from it.
7.1Logits
The logits of a prediction are one number for each token of the vocabulary, and a token with a larger logit gets a larger probability. For example, after the text The ride of 3 km cost $, a model can give the logits 2, 1.5 and 0 to the tokens 9, 8 and 10.
The model calculates the logits as follows. First, each token id becomes its embedding, a vector of numbers (see Section 6.2). After this, the network turns the embeddings into new vectors of numbers, one for each position (see Chapter 8). Finally, the logit of a token is the dot product of the last vector with the row of the token in an output matrix (see Section 3.1).
The following picture shows this path for the above text, where the last vector is shaded:
The vector at a position depends only on the tokens up to that position, and its logits predict the token after it. Thus, the shaded vector predicts the token after ␣$, and the picture shows its logits for 9, 8 and 10.
The output matrix has one row for each token of the vocabulary. For example, with 128,256 tokens and , the output matrix has 128,256 rows of 4096 numbers, the same size as the embedding matrix (see Section 6.2)1.
7.2Softmax
The softmax2 turns the logits into probabilities. Let denote the logit of the token . The softmax gives the following probability: where the sum goes over all tokens of the vocabulary. Each is positive, so each probability is positive, and the probabilities add up to 1. A larger logit gives a larger probability.
For example, suppose that we leave out all tokens except 9, 8 and 10. Their logits 2, 1.5 and 0 give the sum and the following probabilities:
| token | 9 |
8 |
10 |
|---|---|---|---|
| logit | 2 | 1.5 | 0 |
| probability | 0.57 | 0.35 | 0.08 |
For example, the probability of 9 is .
The training uses the same probabilities. At each position of a training text, the loss is , where is the token that actually comes next (see Section 1.3). The training lowers this loss by making the logit of larger compared with the others. The network gives a vector for every position, so one pass through a training text gives the loss at all of its positions (see Chapter 9).
7.3Writing a text
The prompt is the text that a model reads before it writes. The program that runs the model turns the prompt into token ids. After this, it repeats one step: it gets the probabilities of the next token from the model, chooses one token and adds it to the text.
The end token is a special token that marks the end of a text or of a turn in a conversation (see Section 5.1). The program stops when the model writes it3.
In greedy decoding, the program always chooses the token with the highest probability. As an example, consider the prompt The ride of 3 km cost $. The following picture shows the three steps, where the new token is shaded, its probability follows the row, and <|end|> stands for the end token:
After the third step, the text is The ride of 3 km cost $9., and the program stops. The model gives this answer the probability , the product of the probabilities of its tokens.
Each step goes once through every layer of the network, so an answer of 100 tokens takes 100 passes. Chapter 10 shows what a pass costs, and why it does not have to calculate the vectors of the prompt again.
The program also stops after a maximum number of tokens, which the request sets. A text that stops there has no end token, and it can end in the middle of a sentence4.
7.4Sampling
Greedy decoding writes the same text each time for the same prompt. In practice, even greedy decoding can write different texts, because the logits of a request change a little with the number of other requests that the program runs at the same time (see Chapter 10)5.
Long texts written by greedy decoding often repeat a phrase again and again6. Sampling chooses each token at random, with the probabilities of the model. For example, after the above prompt, sampling writes 9 in about 57 of 100 runs, 8 in about 35 and 10 in about 8. When it writes 8, the next steps continue from $8.
The temperature is a positive number by which the program divides every logit before the softmax. For example, the logits 2, 1.5 and 0 give the following probabilities at three temperatures:
| temperature | 9 |
8 |
10 |
|---|---|---|---|
| 0.5 | 0.72 | 0.27 | 0.01 |
| 1 | 0.57 | 0.35 | 0.08 |
| 2 | 0.47 | 0.36 | 0.17 |
When , the logits become 4, 3 and 0. A temperature below 1 moves probability to the tokens with the largest logits, and a temperature above 1 spreads it more evenly. A lower temperature makes the texts better but less varied7.
As goes toward 0, the probability of 9 goes toward 1. Thus, when one token has the largest logit, sampling becomes greedy decoding. A program that runs models can take the temperature 0 to mean greedy decoding8, and the APIs of two large providers use the temperature 1 by default9.
Top-p sampling10 samples only among the most probable tokens. It keeps the fewest of them whose probabilities add up to at least a given value, and it scales their probabilities to add up to 1. For example, with the value 0.9, it keeps 9 and 8, whose probabilities add up to 0.92, and it writes them with the probabilities 0.62 and 0.38. A smaller value removes more unlikely tokens, and it also makes the texts less varied.
Without this limit, tens of thousands of unlikely tokens together get too much probability, and sampling often writes text that makes no sense.
7.5Restricting the format
A program that reads the output of a model often needs a fixed format, for example JSON with given fields. We can describe the format in the prompt, but the model can still write a token that breaks it. Constrained decoding11 removes such tokens. In each step, the program that runs the model sets the probability of every token that would break the format to 0, and it scales the other probabilities to add up to 1.
As an example, suppose that the format allows only {"refund": true} and {"refund": false}, and that the text so far is {"refund":. The model gives the following probabilities to the next token:
| token | ␣yes |
␣true |
␣false |
|---|---|---|---|
| probability | 0.5 | 0.3 | 0.2 |
| after the restriction | 0 | 0.6 | 0.4 |
The token ␣yes would break the format, so its probability becomes 0, and the other two probabilities are doubled to add up to 1. Thus, a text that ends with the end token is always valid JSON.
Note that the probability of ␣yes does not go to ␣true, which means the same here. Before the restriction, the model gave a refund the probability 0.5 + 0.3 = 0.8, and after it, only 0.6. The restriction changes the answer less when the model already gives little probability to the tokens that break the format.
A straightforward way to find the allowed tokens is to check every token of the vocabulary in each step. With the vocabulary of Section 5.1, this takes 200,000 checks for each new token.
However, when a regular expression can describe the format, the allowed tokens depend only on the place in the format, such as inside a string or after a colon. The idea is to find the allowed tokens of each place once, before the generation starts12. A step then needs one lookup instead of 200,000 checks.
A format that nests to any depth, such as JSON objects inside objects, also needs a stack of the open brackets. In practice, an API can take longer for the first request with a new format, because it first turns the format into a grammar and prepares it for the steps13.
Note that a text that stops at the maximum number of tokens can still be incomplete JSON. The restriction only removes tokens, and it cannot add the missing end.
The numbers are those of Llama 3 8B. Some models use the same numbers in both matrices, so that the row of a token in the output matrix is its column in the embedding matrix. For example, GPT-2 and Llama 3.2 1B do, and Llama 3 8B has two different matrices, as the setting
tie_word_embeddingsin the configuration files of the Llama models shows. This use of one matrix was recommended in 2016 by O. Press and L. Wolf: Using the Output Embedding to Improve Language Models.↩︎The name was given in 1989 by J. S. Bridle, as a soft form of picking the largest value: Training Stochastic Model Recognition Algorithms as Networks can lead to Maximum Mutual Information Estimation of Parameters.↩︎
The names differ between models. The end token of o200k_base is
<|endoftext|>, with the id 199999. Llama 3.1 base models end a text with<|end_of_text|>, and its chat models end a turn with<|eot_id|>: Llama 3.1 prompt format.↩︎OpenAI’s API reports this case with the finish reason
length, and Anthropic’s API with the stop reasonmax_tokens: OpenAI API specification, Messages API.↩︎In a test of 2025, 1,000 answers of Qwen3-235B-A22B-Instruct-2507 to one prompt with greedy decoding gave 80 different texts. With calculations that do not depend on the other requests, all 1,000 answers were the same: Defeating Nondeterminism in LLM Inference.↩︎
With GPT-2 Large, 73.66% of 5,000 texts of up to 200 tokens written by greedy decoding ended with a phrase repeated at least three times, against 0.28% of the texts written by people: The Curious Case of Neural Text Degeneration, published in 2019.↩︎
Section 3.2 of the nucleus sampling paper reports earlier studies: “while lowering the temperature improves generation quality, it comes at the cost of decreasing diversity”: The Curious Case of Neural Text Degeneration.↩︎
For example, the documentation of vLLM says of the temperature: “Zero means greedy sampling.” SamplingParams.↩︎
OpenAI’s API takes temperatures from 0 to 2 with the default 1: OpenAI API specification. Anthropic’s API takes temperatures from 0 to 1 with the default 1, and models released after Claude Opus 4.6 accept only 1: Messages API.↩︎
The method was published in 2019 as nucleus sampling, and the paper describes the unlikely tokens as an “unreliable tail” that is “over-represented in the aggregate”: The Curious Case of Neural Text Degeneration. An older method, top-k sampling, keeps a fixed number of the most probable tokens, for example 10 in Hierarchical Neural Story Generation, published in 2018.↩︎
In 2024, OpenAI added Structured Outputs to its API, which allows only the tokens that are valid under a given JSON schema. On its test of complex JSON formats, the same model scored 93% without the restriction and 100% with it: Introducing Structured Outputs in the API.↩︎
The method was published in 2023 by B. T. Willard and R. Louf, and the same paper handles formats that nest with pushdown automata: Efficient Guided Generation for Large Language Models.↩︎
OpenAI gives this reason in the announcement of Structured Outputs: it turns the JSON schema into a context-free grammar and prepares it before the first request with the schema.↩︎