g↗genai/ learner
← Learning path
How GenAI works / Lesson 02

One token at a time.

A language model doesn’t write an answer all at once. It repeatedly predicts what could come next.

Concept + interactive labBeginner friendly

A prediction, then another.

Imagine the model sees “The sky is”. It assigns probabilities to possible next tokens. “Blue” might receive a high probability, but “clear” or “dark” could also fit. The model produces scores over its vocabulary; these become a probability distribution.

A decoding rule chooses a token. That token is added to the context, and the model makes a new prediction using the updated sequence. This repeated process is called autoregressive generation.

The key distinction: a probable continuation is text that fits the context. It is not necessarily a true statement about the world.
The context ‘The sky is’ flows through candidate next tokens, selects ‘blue’, appends it, and loops back to predict again.
The selected token becomes part of the next context.

Watch the context grow.

Step through a tiny illustrative sequence. Notice that each choice changes the context for the next prediction.

01 / The generation loop
01 / Context

Read the sequence

The sky is

The prompt plus everything generated so far.

02 / Model

Score what comes next

Learned weights produce scores. Softmax turns scores into probabilities.

03 / Choose & append

One possible continuation

blue
62%
clear
25%
dark
13%
↳ Append the chosen token to the context. Repeat with the updated sequence.
0 of 3 steps

Hand-authored probabilities and preset choices, not a real model or tokenizer. Real models score a much larger vocabulary; tokens can be word fragments.

What the loop is doing.

In a real system, the model outputs scores called logits. A softmax operation converts those scores to probabilities. A decoding strategy can choose the highest-probability token or sample from the distribution.

# Illustrative pseudocode — not executable here
context = tokenize(prompt)
for _ in range(max_new_tokens):
    logits = model(context)
    probabilities = softmax(logits[-1])
    token = sample(probabilities)
    if token == end_token:
        break
    context.append(token)

The next call includes the new token. Generating a longer answer repeats this loop, subject to stopping rules and context limits. The example above omits implementation details such as caching.

Engineering connection: streaming exposes this incremental generation to the user. Seeing text appear early improves perceived responsiveness even while the rest of the answer is still being generated.
✎ Try it yourselfKnowledge check · Practice only

Likely doesn’t mean verified.

A model assigns a high probability to the next token in a factual claim. What can you conclude?

Take this with you.

Generation is a loop: predict → choose → append → repeat. The distribution describes possible continuations, and the decoding strategy selects among them. Neither step alone verifies a claim.