Skip to content
PHP2Shell: The PHP Bug
Akshay Thakare

How LLMs Generate Text, One Token at a Time

Tokens, probabilities, sampling and context windows — the mental model every GenAI engineer needs.

Generative AIBeginner2 min readUpdated 11 Oct 2026

The whole trick: predict the next token

A large language model is trained to answer one question over and over: given everything so far, what token comes next? Generating a paragraph is just repeating that step.

next-token prediction

Data pipelines must be idempotent so that a rerun never doubles revenue

Done — the model emitted an end token.

The model never writes a whole sentence at once. It predicts one token, appends it, and predicts again.

Tokens, not words

Text is split into tokens — common words are one token, rare words become several pieces. Prices, context windows and output limits are all measured in tokens.

Plain text.txt
"Snowflake COPY INTO is idempotent"
→ ["Snow", "flake", " COPY", " INTO", " is", " idem", "potent"]

From scores to a choice

  1. 1

    Score every candidate

    The model outputs a score (logit) for every token in its vocabulary.
  2. 2

    Turn scores into probabilities

    Softmax converts scores into a probability distribution.
  3. 3

    Sample one token

    Temperature and top-p decide how adventurous the choice is.
  4. 4

    Append and repeat

    The chosen token joins the input, and the model predicts again — until a stop token or a length limit.
SettingLow valueHigh value
TemperatureFocused, repeatable — good for SQL, extractionVaried — good for brainstorming
Top-pOnly the most likely tokensWider vocabulary
Max tokensShort, cheap answersLong answers, higher cost

Finished this note?

Share