How a Large Language Model Produces a Sentence, One Token at a Time
Follow a chatbot reply from your typed words to the finished sentence, with a worked example of how each next token is chosen.
Key takeaways
- A language model generates text by repeatedly choosing one next token based on everything that came before it.
- At each step the model produces probabilities over many possible tokens, and a selection method picks one.
- Chosen tokens become part of the input, so an early choice shapes everything that follows.
- The process is aimed at likely, fitting text, which often lines up with truth but does not guarantee it.
A large language model writes a reply in small steps. It looks at all the text so far, calculates how likely each possible next token is, picks one, adds it to the text and repeats. A token is a word or a piece of a word. The sentence you read is the result of that loop running many times in quick succession.
The model does not first compose the whole paragraph and then reveal it. That simple loop explains a good deal of what you see in chatbots: why text appears gradually, why two replies to the same question can differ and why a fluent sentence can still be wrong.
The loop in outline
Each turn of the cycle involves a very large amount of arithmetic, usually on specialized chips. It happens fast enough that you see words streaming onto the screen.
Step one: your words become tokens
Before the model sees anything, your message is cut into tokens. Common short words are often a single token. Longer or rarer words may be split into several. Spaces and punctuation are handled too. Each token has an identification number in the model's fixed vocabulary.
Your message is also rarely alone. A chat product usually places standing instructions before it, along with the earlier turns of the conversation, and marks who said what. To the model this is one long sequence that it is going to continue. The article on tokens and why they matter covers the splitting in more detail.
Step two: tokens gain meaning from context
Each token number is turned into a list of numbers called an embedding. Then the model passes these lists through many layers of computation. Most current language models use a design called the transformer, introduced by researchers at Google in 2017.
Its central idea is attention: for each position, the model works out how much information to draw from the other positions. Consider "The trophy did not fit in the suitcase because it was too big." To continue sensibly, something in the computation has to connect "it" with the trophy. Change "big" to "small" and the better link is the suitcase.
By the top layers, the numbers at the last position summarize what matters for predicting what comes next. Nobody wrote rules for pronouns or grammar. These behaviors arose from training. The idea of turning words into comparable numbers is explored in embeddings in plain English.
A reply is a continuation of a transcript
It can help to picture what the model is actually continuing. A chat looks like two parties taking turns, but underneath it is closer to a single script. Special tokens mark where the product's instructions end, where your message begins and where the assistant's reply should start.
[instructions] You are a helpful assistant. Answer briefly.
[user] How do I keep cut basil fresh?
[assistant] (the model continues from here) The labels are simplified. The point is that the model has no separate channel for "the question". It produces the text that fits after the assistant marker, given everything above it. This is why standing instructions can change tone and behavior so much, and why text pasted from a stranger's document can sometimes be mistaken for an instruction.
Step three: probabilities over the next token
The model's raw output is a score for every token in its vocabulary, which typically runs to many thousands of entries. These scores are converted into probabilities that add up to 1.
Suppose the text so far is "On a rainy day, remember to take your". The numbers below are made up for illustration, and a real model would spread small probabilities over many more options.
Hypothetical example: the numbers are made up to show the method, not a quote or an average.
These probabilities describe which continuation fits the pattern of the text. They are not a measure of whether a statement is true.
Step four: choosing
The system now has to commit to one token. The simplest method always takes the most likely one. That gives steady output, but it can sound flat and sometimes falls into loops.
Many chat products instead sample, which means choosing at random in proportion to the probabilities, usually after trimming very unlikely options. In the example, "umbrella" would be chosen most of the time and "coat" now and then. A setting called temperature makes the choice more adventurous or more cautious. See temperature and randomness.
Building the sentence
Now watch the loop continue, again with invented choices.
Text so far Chosen next piece
On a rainy day, remember to take your umbrella
... take your umbrella and
... take your umbrella and wear
... take your umbrella and wear shoes
... take your umbrella and wear shoes with
... take your umbrella and wear shoes with good
... and wear shoes with good grip
... and wear shoes with good grip . Whole words are shown here to keep it readable. Real tokens may be fragments.
If the second step had picked "because" instead of "and", the rest would have gone in a different direction. The model cannot go back and quietly erase a token it has already emitted in the ordinary loop. It can only continue, which sometimes means building a confident justification on top of an early slip.
The reply ends when the model produces a special token that marks completion, or when a length limit is reached. A response that stops mid-sentence has usually hit a limit, and asking it to continue often works.
How selection settings change the result
Developers can often adjust how the pick is made. Using the invented probabilities above, here is what some common choices would do.
| Selection approach | Effect in the umbrella example |
|---|---|
| Always take the top token | "umbrella" every time |
| Sample freely | "umbrella" most often, rarer options sometimes |
| Keep only the few most likely, then sample | Long-shot words are never chosen |
| Raise the temperature | "coat" and "boots" appear more often |
| Lower the temperature | "umbrella" dominates even more |
Trimming the unlikely tail helps prevent bizarre jumps while keeping some variety. Products usually choose these settings for you. The consequences for repeatability are covered in why answers to one question can differ.
Where the knowledge comes from
All of this depends on training. A model is shown enormous amounts of text and asked, over and over, to predict the next token. Each miss leads to small adjustments in its internal numbers. To get good at this, it has to capture grammar, common facts, styles and many patterns of reasoning.
A chat model usually goes through further training so that it answers questions helpfully, follows instructions and declines some requests, often with help from human ratings. When you chat, those internal numbers are normally fixed. The article on training and inference explains the two phases.
What the loop explains about behavior
Fabrications. If the most fitting continuation of a question is a plausible-looking date or book title, the model may produce one whether or not it is real.
Sensitivity to wording. A different phrasing changes the text being continued, and so changes the probabilities.
Benefit of working space. When a model writes out intermediate steps, those steps become input for later tokens. This can improve results on multi-step problems. Some models are trained to do extended working before giving an answer.
Odd weak spots. Tasks about individual letters can be awkward when the model receives chunks.
Is "just predicting the next token" a fair summary? It is an accurate description of the output procedure. What has to be going on inside for the predictions to be so good is a matter of active research and debate.
When the loop pauses for a tool
Many assistants can do more than emit prose. The model may generate a structured request, such as a search query or a calculation. Ordinary software notices the request, runs it and appends the result to the text. Then the same loop resumes, now with that result in view.
So when a chatbot appears to look up the weather or add a column of figures, two different things are working together: a model that writes requests and replies, and conventional programs that carry them out. The final wording still comes from the token loop, so a correct tool result can still be reported incorrectly. For figures that matter, compare the reply with the tool's raw output if the product shows it.
Using this in practice
- Give the context you want reflected, since the model continues what it is given.
- Ask for the working on calculations, then check it yourself.
- If a reply starts down a wrong path, start a fresh message that corrects the premise.
- Verify names, numbers, quotations and sources independently.
- Regenerate when you want alternatives, but do not treat agreement between tries as proof.
Common questions
Does the model plan ahead at all?
The text comes out one token at a time, yet research on model internals suggests that some computation can anticipate later words, such as an upcoming rhyme. How much planning happens, and in what sense, is still being studied.
Why does the reply appear gradually?
Tokens are generated in order, and many interfaces show each one as it arrives so you need not wait for the whole answer.
Is it copying sentences from somewhere?
Usually it composes new sequences. Models can, however, reproduce passages that appeared often in training, which is one reason copyright and privacy questions arise.