Tokenization and Token IDs
Generation picks the single highest-scoring token at each step, so the same context always leads to the same choice. Nothing in this rule introduces randomness.
Predicting the Next Piece of Text
A text generation call takes a short starting phrase and returns that phrase extended with new text. Before learning the rule that controls this behavior, it is worth making a prediction, because the answer tells you what the rest of the article needs to explain. Imagine running the same generation call three times in a row, with the same model, the same starting phrase, and the same settings.
What do you think happens?
Will the printed text be the same on each of the three runs, or will it change?
Reveal answer
The same text every time, as long as the model, the starting phrase, and the settings stay the same.
The source explains that the selection rule always picks the same token for the same context. Because every step is determined by the same inputs, the same sequence of tokens comes out on each run.
The rest of this article explains why that answer holds. It also shows which inputs you should check when a result looks different from what you expected. The model is never run in isolation here: the starting phrase goes in, a generation function extends it, and the result is converted back to text for printing.
Tokens and Token IDs
The starting phrase is not passed to the generation function as raw text. It is first converted into a sequence of token IDs. A token is a word-piece from the model's vocabulary, and each token has an integer ID that identifies it. The conversion from text to token IDs is handled by a helper named text_to_token_ids, and the reverse conversion from token IDs back to text is handled by token_ids_to_text.
The diagram is illustrative: it shows the shape of the mapping, with three tokens standing in for however many a real phrase produces. The key idea is that the model works with integer IDs. Each ID points into the vocabulary, which is the full set of tokens the model can produce. The generation function extends the sequence of IDs, and only at the end are those IDs turned back into readable text.
One Token at a Time
Generation is a sequence of steps, one for each new token. The generation call takes a parameter that sets how many new tokens to produce. In the call described in the source, that parameter is max_new_tokens=25, so the loop runs until 25 new tokens have been added. Each step follows the same cycle, and the cycle is what turns a single prediction into a continuation of the starting phrase.
Follow one pass through the loop. The model reads the current context, which is the starting IDs plus any tokens already generated. It assigns a probability score to every token in its vocabulary. One token is selected and appended to the context. That longer context then feeds the next prediction. Because each new token is added to the context, later predictions are based on everything that came before, which is why the output extends the starting phrase rather than being a fixed reply.
Why the Top Score Wins
The source states the selection rule precisely. At each generation step, the model assigns a probability score to every token in its vocabulary. The generated token is the one with the largest probability score among all of them. The choice is not a draw from the distribution, where a lower-scoring token might sometimes win. It is always the top-scoring candidate, and this single rule decides which word-piece comes next.
Choosing One Step
At one generation step, the model has scored three candidate tokens from its vocabulary. Token A has the highest score, Token B has a middle score, and Token C has the lowest score. Which token is generated, and why?
- 1List every candidate
The rule applies across all tokens in the vocabulary, so the comparison includes every candidate, not just the three shown here. These three stand in for the full set.
- 2Compare the scores
Token A has the largest probability score of the candidates in view. Token B and Token C both score lower.
- 3Apply the selection rule
The generated token is the one with the largest probability score, so Token A is selected. Token B and Token C are not chosen even though they have non-zero scores.
- 4Append and continue
Token A is added to the context, and the next step begins with that longer context.
Token A is generated because it has the largest probability score among all candidates at this step.
Generation picks the single highest-scoring token at each step, so the same context always leads to the same choice. Nothing in this rule introduces randomness.
Why Repeated Runs Match
Determinism follows directly from the selection rule. If each step always picks the top-scoring token, and each step starts from the same context, then each step gives the same choice. Step after step, the same tokens get appended, so the full output is the same. This is why repeated runs of the same generation call on the same starting phrase produce the same outputs.
Two runs are identical only when their inputs are identical. The source names the inputs that determine the output: the starting context, the generation settings, the model, and the tokenizer. If any one of these changes, the path through the loop can change, and the printed text can change with it. The same-output guarantee applies to a fixed set of inputs, not to every call that looks similar.
Tracing an Unexpected Output
When the generated text is not what you expected, the way to find the cause is to follow the data through the pipeline, one stage at a time. The pipeline starts with the phrase you typed and ends with the printed text. Each stage produces a value, and an unexpected token must have come from some stage that produced a different value than you assumed.
- Starting context: confirm the exact phrase and that it was converted to the same token IDs as before.
- Generation settings: confirm the parameter values, such as
max_new_tokens, match the earlier run. - Model: confirm the same model is being used.
- Tokenizer: confirm the same tokenizer is being used to convert text to IDs and IDs back to text.
Mistakes Learners Make with Generation
Common mistake Assuming the model samples randomly at each step
Why it failsThe source states that the generated token is always the one with the largest probability score. The choice is not a draw from the distribution.
Do this insteadExpect the same context to produce the same next token, and look for changed inputs when results differ.
Common mistake Treating the starting phrase as the only input that matters
Why it failsThe output also depends on the generation settings, the model, and the tokenizer.
Do this insteadWhen a result changes, check all four inputs, not just the text you typed.
Common mistake Thinking the model works directly with characters
Why it failsThe starting phrase is converted to token IDs before generation, and the generated IDs are converted back to text afterward.
Do this insteadThink of the generation loop as operating on token IDs, then remember the final conversion back to text.
Common mistake Expecting a single step to produce a whole sentence, when the source describes one new token per step.
Practice Questions
Two students run the same generation call with the same model, starting phrase, and settings. One gets a different continuation. Name the inputs you would check first, and explain why the selection rule alone does not explain the difference.
Hints
- Recall the four inputs the source names as determining the output.
- If the selection rule were the only variable, the two outputs would match.
Explain, in your own words, why each new token depends on the tokens added before it. Use the terms context and next prediction in your answer.
Hints
- Think about what the context contains after the first step.
Key Takeaways
- An LLM generates text by predicting one token at a time, appending each chosen token to the context, and repeating until the requested number of new tokens is produced.
- The starting phrase is converted to token IDs before generation, and the IDs are converted back to text afterward.
- At each step, the generated token is the one with the largest probability score among all tokens in the vocabulary.
- Because the selection rule always picks the same token for the same context, repeated runs with the same inputs produce the same output.
- When a result changes, check the starting context, generation settings, model, and tokenizer, since these are the inputs that determine the output.