How an LLM Generates Text Token by Token
What next-token prediction means and what happens between submitting a message and receiving a response.
Contents
When you type Hi! into a chat, a response such as Hello! How are you? looks like a single piece of writing. The model does not return a complete draft and then read it out from beginning to end: on each call, it receives the tokens that already exist and calculates a score for every possible continuation. A decoding rule chooses one, appends it to the context, and calls the model again with the longer sequence.
Calling this next-token prediction is accurate, but it does not explain much until we pin down what a token is and what “prediction” means here. A token can be a word, a fragment, or a punctuation mark; for now, we can treat it as a unit of text the model knows how to represent. The next article explains how those units are constructed.
Why Language Models Predict the Next Token
People were studying text prediction long before LLMs. In 1951, Claude Shannon asked people to guess the following letters in a passage and used their answers to estimate the entropy of written English [1]. He was not building a chatbot. He was measuring how much uncertainty remains after seeing part of a message. Even then, prediction offered a way to study the structure of language.
N-gram models turned that idea into a straightforward statistical method. To predict a word, they looked at the preceding words and counted continuations in a corpus. They worked well with enough examples, but longer contexts exposed a data problem: many perfectly natural sequences never appear verbatim in the training corpus.
In 2003, Bengio and his collaborators proposed learning word representations and the sequence probability function together with a neural network [2]. Two sentences could share what the model had learned without being identical, because their words occupied nearby regions of a distributed representation. Transformers later changed how the model processes context, but the basic question remained: given the preceding tokens, what is the distribution over the next one?
Why construct a sequence this way? Think of Hello! How. We can calculate its probability in parts: the probability of starting with Hello, the probability of ! following Hello, and the probability of getting How once we already have Hello!. Multiplying them gives the probability of the complete sequence. This decomposition is the chain rule:
Each term accounts for one token given everything to its left. The rule does not require forgetting the past: every factor can depend on all preceding tokens. Nor does it prove that left-to-right is the only possible order or explain the model’s behavior by itself. It is a practical choice: left-to-right matches the way we read and lets us generate by extending what already exists.
It also turns ordinary text into training material. Given Hello! How, the prefix Hello! is the input and How is the answer the model must learn. Nobody needs to write that label by hand; it is already in the document. This self-supervised setup lets us reuse books, code, and web pages at a scale that would be impossible if every example required manual annotation.
How a Response Is Built Token by Token
The model does not return only its favorite word. It assigns a score to every token in the vocabulary, including fragments, punctuation, and special markers. After Hi!, Hello might rank highly and Hey slightly lower. A decoding rule then uses those scores to select one. Scoring candidates and choosing a continuation are separate operations; the second does not add a truth check to the first.
Suppose a toy model produces Hello! How are you?. Leaving chat formatting aside for a moment, the system repeats this loop:
| Pass | Available context | Selected token |
|---|---|---|
| 1 | Hi! | Hello |
| 2 | Hi! Hello | ! |
| 3 | Hi! Hello! | How |
| 4 | Hi! Hello! How | are |
| 5 | Hi! Hello! How are | you |
| 6 | Hi! Hello! How are you | ? |
To keep the table simple, the context appears as text rather than the ids a tokenizer would produce. Each selected token is added to the context for the next pass, so replacing Hello with Hey may have little effect, while a name, a negation, or a different format can redirect the rest of the answer.
The system stops when an end marker is selected or when it reaches a limit configured by the application. The model does not infer that limit. It will keep scoring continuations for as long as the system keeps calling it.
Appending tokens does not change the weights either. The sequence changes, along with the activations the network computes from it, but the parameters stay fixed during ordinary inference. A long conversation can substantially alter what the model says next without making it learn in the same way it did during training.
How a Conversation Is Represented as Tokens
A chat application presents user messages, system instructions, and assistant responses as separate elements. That is not what the model receives. Before the call, the application uses a chat template to turn the conversation into one token sequence.
A simplified example:
<|system|>
You are a helpful assistant.
<|user|>
Hi!
<|assistant|>
The actual names and delimiters depend on the model. Some templates use special tokens; others represent roles with text markers. Their job is the same: preserve who wrote each message, mark the end of a turn, and show where the answer should begin.
The final marker does not open a different module inside the network. After <|assistant|>, the model keeps predicting the next token in the sequence. If the system selects Hello, that text becomes part of the context:
<|system|>
You are a helpful assistant.
<|user|>
Hi!
<|assistant|>
Hello
On the next call, the model can produce ! and, later, the marker that closes the turn. Role labels enter the context like any other tokens; their usefulness depends on the patterns the model learned to associate with them.
This helps separate a base model from an assistant. A model trained only to continue documents might answer a question with another plausible question, or even invent a new user turn. Post-training shapes its behavior so it responds as an assistant. The template marks the conversation’s structure, but it does not replace that learning.
The serialized context can contain much more than the latest message: system instructions, previous turns, assistant responses, and tool results. Everything arrives through the same interface. Once again, the model predicts a continuation, now conditioned on that complete conversation.
What Separates a Language Model from an Agent
When we say what a model “did”, two things often blur together. The LLM computes distributions over continuations. An agent surrounds it with a program that can read files, run commands, consult tools, retain results, and call it again.
Ask that agent to complete a backlog, and the work is not one giant prediction. The system can open a task, inspect the project, propose a change, run tests, and feed the errors back to the model. The LLM participates in decisions such as which file to modify or which code to write, while the surrounding program controls permissions, available tools, and the order of some operations.
The model remains central, but it does not work alone: the agent’s behavior emerges from combining its predictions with external memory, tools, and successive calls.
Summary: From Hi! to the First Token
With this picture in place, we can describe generation more precisely. To produce Hello after Hi!, the system follows five steps:
- The tokenizer splits the serialized text and assigns an id to each token.
- Embedding turns those ids into vectors.
- The Transformer blocks transform the vectors using the context.
- LM head scores every token in the vocabulary.
- A decoding rule selects
Hello.
The following articles examine each component separately. We start with tokenization: before there are vectors, context, or scores, text must be converted into tokens.