What Is Next-Token Prediction? How LLMs Generate Text

By LatestLLM Editorial · Published · Facts verified · 9 min read

Also called: autoregressive generation, language modeling, logits, softmax, next word prediction, text generation

Before this: Tokenizer, Transformer

In one line

Next-token prediction is the task a language model performs: given the text so far, it scores every token in its vocabulary and one token is chosen as the next.

Next-token prediction, simply and technically

In plain English

Everything a chatbot writes comes from one repeated question: given the text so far, what comes next? The model does not look up an answer or write a draft first. It looks at the words already there and gives every word it knows a score for how well it would fit as the next one. One word is picked, added to the text, and the question is asked again with the longer text.

That is the whole loop. A paragraph is hundreds of repetitions of it. The model rarely has a single certain answer, so what it produces is a spread of possibilities, some likely and some unlikely, and the way the next word is picked from that spread shapes whether the output is cautious or creative.

Think of it like this: Next-token prediction is like the suggestion bar above a phone keyboard, with two differences: the bar shows only three suggestions and the model scores every word it knows, and the model reads the entire conversation instead of the last word or two. The comparison understates how much the model can take into account, but it is right about the process: choose a word, append it, ask again.

Everyday example: Finishing a sentence

A model is asked to continue “The capital of France is”.

Input:

The capital of France is

Output:

“ the” 8.5%, “ now” 4.8%, “ a” 4.6%, “ France” 3.2%, “ Paris” 3.2%, …

What it shows: A small, old model such as GPT-2 does not confidently say “Paris”. Measured with the real model, its top choice is “the” with 8.5%, and “Paris” has 3.2%. Together its eight best guesses cover only about a third of the probability; the other 50,249 tokens share the rest. Larger, better-trained models usually put much more probability on the right answer, but the mechanism is the same.

Technically

A decoder-only language model maps a token sequence x_1 … x_t to a vector of logits z with one entry per vocabulary token, the output of the last layer's hidden state multiplied by an output matrix. Softmax turns logits into probabilities, p_i = exp(z_i) / Σ_j exp(z_j), and one token x_(t+1) is chosen by a decoding rule. The token is appended and the model is run again, so a text of n tokens takes n forward passes, which is why generation cost is measured per token and why caching keys and values matters.

Vocabulary size sets the width of the output: 50,257 for GPT-2, 129,280 for DeepSeek-V3, 201,088 for gpt-oss and 262,144 for Gemma 4. Some models bound the scores first: Gemma 4 soft-caps logits at 30 with z' = 30 · tanh(z / 30), which leaves small scores almost unchanged and squashes extreme ones. Training uses the same quantity: the loss for a text is the average of −ln p over the true next tokens. DeepSeek-V3 also trains a module that predicts one extra token ahead.

Technical example: Reading a real next-token distribution

The eight most likely next tokens after “The capital of France is” in GPT-2 small (vocabulary 50,257), as computed with Hugging Face transformers. The probabilities are analysed in plain Python.

Code (python):

import math

top = [(" the", 0.0846), (" now", 0.0479), (" a", 0.0462), (" France", 0.0324),
       (" Paris", 0.0322), (" in", 0.0266), (" also", 0.0264), (" not", 0.0238)]
vocab = 50257

mass = sum(p for _, p in top)
print("probability held by the top 8:", round(mass, 4))
print("shared by the other", vocab - 8, "tokens:", round(1 - mass, 4))

best, paris = top[0][1], dict(top)[" Paris"]
print("logit gap 'the' minus 'Paris':", round(math.log(best / paris), 3), "-> odds", round(best / paris, 2), "to 1")
print("loss if the true token is ' Paris':", round(-math.log(paris), 3), "nats")
print("loss of a uniform guess over the vocabulary:", round(math.log(vocab), 3), "nats")

Output:

probability held by the top 8: 0.3201
shared by the other 50249 tokens: 0.6799
logit gap 'the' minus 'Paris': 0.966 -> odds 2.63 to 1
loss if the true token is ' Paris': 3.436 nats
loss of a uniform guess over the vocabulary: 10.825 nats

What it shows: Logits are only defined up to a constant, so differences between them are what matter: ' the' is about 2.6 times as likely as ' Paris'. The model is far better than a uniform guess (3.44 against 10.83 nats of loss on the true token) yet still assigns the correct answer a low probability, a reminder that GPT-2 small is a 124-million-parameter model from 2019.

What GPT-2 predicts after “The capital of France is”. Bars show the probability GPT-2 small gives each of its eight most likely next tokens and the single lump of probability shared by the other 50,249 tokens. Press the button to draw 100 next tokens from this real distribution and watch how often each is chosen; the readout compares the top token with “Paris”. Probabilities computed with openai-community/gpt2 (Hugging Face transformers); draws use a seeded random number generator.

The generation loop

In plain English: To answer a question, the model works through a loop. It reads the prompt, produces a list of scores for what could come next, picks one token, writes it down, and then reads the prompt plus the new token. The loop stops when the model picks a special “end” token or reaches a length limit.

Technically:

Formally the model factorises the probability of a text as a product p(x_1) · p(x_2 | x_1) · … · p(x_n | x_1 … x_(n−1)). Generation draws from each conditional in turn. Because token t+1 depends on the token that was just drawn, text cannot be generated in parallel at inference time, even though training can evaluate all positions at once using the causal mask.

Chat models add structure to the same loop. The conversation is turned into a single token sequence with markers for roles, and the model continues it until it emits an end-of-turn token. Tools, reasoning and formatting are all learned patterns of next tokens.

What is an autoregressive model?

A model that generates a sequence one element at a time, with each new element conditioned on all the earlier ones.

What is an end-of-sequence token?

A special token the model learns to produce when the answer is finished, which tells the software to stop generating.

From scores to probabilities

In plain English: The model's raw scores can be any number, positive or negative, and only their differences matter. A step called softmax converts them into percentages that add up to 100. A token with a slightly higher score gets a clearly larger share, and a much lower score gets almost nothing.

Technically:

Softmax exponentiates the logits and divides by their sum. Adding the same constant to every logit changes nothing, so implementations subtract the maximum for numerical stability, and the probability ratio of two tokens is exp of their logit difference. The output layer is a matrix of vocabulary size × hidden size: 201,088 × 2,880 in gpt-oss, which is untied from the input embedding, whereas GPT-2 and Gemma 4 tie the two.

A large vocabulary makes sequences shorter but the output layer wider. DeepSeek-V3's embedding table and output matrix are each about 0.93B parameters, together 0.28% of the model, so the cost of scoring 129,280 tokens is small next to the layers below.

Go deeper: details for practitioners
  • Soft-capping as in Gemma 4 prevents any logit from exceeding ±30, which keeps softmax outputs from becoming extreme late in training.
What is a logit?

The raw score the model gives a token before softmax. Higher means more likely.

A distribution, not an answer

In plain English: Models are often described as if they know the answer. What they really have is a spread of possibilities. For an easy question one word has nearly all the probability. For an open one, such as the start of a story, thousands of words have a small share each.

Technically:

The measured distribution for GPT-2 small after “The capital of France is” is very flat: the top token has 8.46% and the top eight together 32.0%, with 68.0% spread over the rest of the vocabulary. Larger and better-trained models usually concentrate more probability on likely continuations, but entropy remains high wherever many continuations are legitimate.

This is also why decoding settings matter. Taking the single most probable token every step (greedy decoding) is repeatable but can be dull or repetitive; drawing from the distribution gives variety; and the shape of the distribution is what temperature and top-p change. The next concept pages explain sampling in detail.

Everyday example: Easy and open prompts

Two prompts are given to the same model.

What it shows: After “2 + 2 =” almost all the probability sits on one token. After “Once upon a” a handful of words share it. The model is equally confident in how it computes both, but the world offers many valid continuations in the second case.

One pass per token

In plain English: Writing a long answer takes many repetitions of the loop, and each repetition runs the entire model. That is why long answers take longer than short ones and why services price output tokens separately from input tokens.

Technically:

Each step runs the full stack of layers once for the newest token, reusing cached keys and values for the earlier ones. The weights must be read from memory on every step, so decoding is usually limited by memory bandwidth rather than arithmetic. Several techniques try to produce more than one token per pass: speculative decoding drafts tokens with a cheap model and verifies them in one pass, and DeepSeek-V3 trains a module that predicts a second token, reporting 85 to 90% acceptance of it.

The first stage, processing the prompt, is different: all prompt tokens go through in parallel, which is faster per token. Latency and throughput therefore depend on prompt length and output length separately.

What is greedy decoding?

Always choosing the single highest-probability token. It is deterministic but can loop or sound repetitive.

Common misconceptions

  • Myth: The model writes the whole answer in its head and then types it out.

    Reality: Text is produced one token per forward pass. Any plan exists only in the model's internal activations, and each next token is still drawn from a fresh distribution.

  • Myth: The most probable token is always the correct one.

    Reality: Probability measures fit with the training data, not truth. In the GPT-2 example the most probable token is “the” and “Paris” ranks fifth.

  • Myth: A model looks up facts in a database before answering.

    Reality: A plain language model has no lookup step. Facts are encoded in its weights and enter the answer only through the probabilities of tokens.

Where you'll see this

Key takeaways

  • A language model writes by repeating one step: score every token in the vocabulary, pick one, append it and run again.
  • Scores are called logits; softmax turns them into probabilities that add up to 1 over the whole vocabulary.
  • The result is a distribution, not an answer: GPT-2 small gives its top token only 8.5% after “The capital of France is”, and “Paris” 3.2%.
  • Training uses the same step: the loss is the average of −ln p of the true next token over a huge amount of text.

Frequently asked questions

What does next-token prediction mean?

It means that a language model, given a sequence of tokens, assigns a probability to every possible next token; generating text is repeating this and picking a token each time.

Is ChatGPT just predicting the next word?

Its core step is next-token prediction, but the model is first trained on enormous text and then fine-tuned to follow instructions, so the predictions encode skills far beyond autocomplete.

What are logits?

Logits are the raw scores a model gives every token before softmax converts them to probabilities.

Why is the output different each time?

Most services sample from the probability distribution instead of always taking the top token, so repeated runs can differ.

How many tokens does a model choose from?

The size of its vocabulary: 50,257 for GPT-2, 129,280 for DeepSeek-V3, 201,088 for gpt-oss and 262,144 for Gemma 4.

Sources

Facts last verified November 23, 2026.

Next concept: Context window

Learn next

  • Sampling and temperature (Beginner): Sampling is how a language model picks the next token from its probabilities, and temperature, top-k and top-p are the dials that make the choice safer or more adventurous.
  • Pre-training and loss (Intermediate): Pre-training is the first and largest stage of building an LLM, in which the model learns to predict the next token on trillions of tokens of text and is scored by cross-entropy loss.
  • Context window (Beginner): A context window is the maximum number of tokens, prompt and reply together, that a language model can take into account at one time.