Skip to content
mentoor

Topic 1 of 5 · 4 min read

Generating one token at a time

Goal: Trace the generation loop for a short reply and explain why the model cannot go back and revise tokens it already wrote.

A language model writes by repeating one small step: it scores every possible next token, picks one, adds it to the text, and goes again.

Check yourself · 1 of 3

Which description matches how a language model generates a reply?

Choose one answer

Why it matters

Chat tools such as ChatGPT, Claude, Gemini and Copilot feel like they think a whole answer and then type it out. They do not. Knowing the real loop explains why long answers drift, why a model cannot take back a sentence, and why the first few words of a reply matter so much.

How the loop works

A token is a chunk of text, often a word or part of a word. (If this is new, read the tokens skill first.) Each turn of the loop has four steps:

  1. Read. The model takes everything so far: your prompt plus any tokens it has already written.
  2. Score. It gives a probability to every token in its vocabulary. "The capital of France is" might give "Paris" a very high score and "Lyon" a tiny one.
  3. Pick. One token is chosen. A setting called temperature controls how much chance is allowed. Low temperature almost always takes the top token. Higher temperature sometimes takes a lower one.
  4. Append and repeat. The chosen token joins the text, and the loop starts again with the longer text. It stops when the model picks a special end-of-reply token or hits a length limit.

Here is the loop for the prompt "The capital of France is":

Step Text so far Token picked
1 The capital of France is Paris
2 The capital of France is Paris .
3 The capital of France is Paris. (end)

No going back

Once a token is picked, it is part of the text. The model has no eraser. Its next step must continue from what is already there, even if that token was a poor choice. A human writer drafts, crosses out and rewrites. A model, on its own, only moves forward. Newer "reasoning" modes write extra thinking text first, but that is still one token at a time, and it works because the thinking text becomes part of the input.

Before and after

Before: "Is it safe to take ibuprofen with my blood pressure pill?" The model starts with "Yes, it is generally fine..." and now every following token has to support that opening.

After: "First list the facts that matter for this question, then give a yes or no at the end." The model writes the facts first. The answer comes last, so it can use them.

What goes wrong

  • Early commitment. A wrong first word, such as "Yes" or "No", pulls the rest of the reply toward it.
  • Drift in long answers. Each token depends on all the earlier ones, including the model's own mistakes.
  • Asking the model to "go back". In a chat, it cannot edit its earlier text. It writes a new reply that may or may not fix the problem.

Examples

Tracing a short reply

Prompt: "Name a primary colour."

  • Step 1: scores might be Red 40%, Blue 35%, Yellow 20%, other 5%. The model picks Blue.
  • Step 2: the text is now "Blue". The next scores favour a full stop or "is". It picks a full stop.
  • Step 3: the text is "Blue." and the model picks the end token.

Run it again and a higher temperature might pick Red at step 1. The rest of the reply then follows from Red.

Move the verdict to the end

Before: Should we migrate to the new supplier? Answer yes or no, then explain.

After: Compare the two suppliers on price, delivery time and reliability. Then give your recommendation in one sentence.

In the first version the model commits to yes or no before it has written any reasoning. In the second, the reasoning is already on the page when it reaches the recommendation.

Key points

  • A model produces text by looping: score every possible next token, pick one, append it, repeat.
  • The loop stops when the model picks an end-of-reply token or reaches a length limit.
  • Temperature controls how much a lower-scored token can be chosen instead of the top one.
  • A token that has been written stays in the text, so the model cannot revise it, only continue from it.
  • Asking for the facts first and the verdict last gives the model something to build on before it commits.

Common mistakes

  • Believing the model drafts a full answer in private and then types it out.
  • Asking for the verdict in the first word and the reasoning afterwards.
  • Expecting the model to quietly fix an earlier sentence while it keeps writing.
  • Treating the probabilities as a list of answers the model has already checked.

Check yourself · 2 of 3

A model has written "The meeting is on Tuesday" but the correct day is Thursday. What can it do on its own, in the same reply?

Choose one answer

Check yourself · 3 of 3

You ask: "Is this contract clause risky? Start with Yes or No, then explain." Why is that wording a weak choice?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic