mej7.comHow AI Works

How does a language model choose the next word?

Author: Mehdi Sadriu, independent educator. Last reviewed: 25 November 2026. How we check claims: methodology.

The model does not plan a whole paragraph in one snap. It produces one token, appends it, and asks again. Under the hood it outputs a logit for every vocabulary item, softmax turns those logits into a probability distribution, and a decoding rule picks a token. That loop is older than transformers — it is the neural language-model idea (Bengio et al. (2003)) — and Shannon's 1948 theory already treated communication as choosing messages from a distribution (Shannon (1948)).

In one paragraph. Next-token prediction: p(token_t | token_1…t−1). Generation repeats argmax or sampling from that p.

Toy softmax. Three logits.
TokenLogit
meow4.0
woof1.0
hello−1.0

What is temperature?

Dividing logits by T > 1 flattens the distribution (more random); T → 0 approaches always picking the top logit. It is a sampling knob, not a measure of 'creativity' in a human sense.

Why does this feel like thinking?

Because human writing is also sequential, and the model was trained on that writing. Fluency is what you get when you copy sequential statistics well. It is not a proof of an inner life (see the next pages).

Where is the transformer?

The scores come from a deep network, usually a transformer (Vaswani et al. (2017)), that mixed the previous tokens with attention. This page only needs the last step: logits → softmax → pick.

What is the math?

p_i = exp(z_i / T) / Σ_j exp(z_j / T)

z_i is the logit for vocabulary item i. T is temperature. Greedy decoding takes argmax_i p_i (equivalently argmax z_i when T → 0).

Worked example

'The cat says' → toy logits: meow 4.0, woof 1.0, hello −1.0. Softmax (T=1) puts most mass on 'meow'. The model emits 'meow', then scores 'The cat says meow ___'. It never wrote the whole joke in one matrix multiply.

Check your understanding

Five questions. Answers are in the disclosure under each item, so this page still works if JavaScript is off.

1. Generation is usually…
Show answer

B. Autoregressive loop.

2. Softmax outputs…
Show answer

B. A probability distribution.

3. Temperature T → 0 means…
Show answer

B. Greedy limit.

4. This next-token idea in neural nets dates at least to…
Show answer

A. A Neural Probabilistic Language Model.

5. Picking the most likely token always gives the best essay.
Show answer

B. Decoding is a choice.

Further reading

  1. A Neural Probabilistic Language Model — Bengio et al. (2003). Journal of Machine Learning Research.
  2. A Mathematical Theory of Communication — Shannon (1948). Bell System Technical Journal.
  3. Attention Is All You Need — Vaswani et al. (2017). NeurIPS.