How LLMs Predict the Next Word?
A model does not output words — it outputs a score for every token in its vocabulary. Reshape those scores with temperature, top-k and top-p, and watch the same model turn from deterministic to wildly creative.
A model does not output words — it outputs a score for every token in its vocabulary. Reshape those scores with temperature, top-k and top-p, and watch the same model turn from deterministic to wildly creative.
Ask an LLM to continue a sentence and it does not pick a word. It emits one raw score — a logit — for every token in its vocabulary, often 50,000 or more. Everything that feels like personality, creativity or repetitiveness happens in the small amount of maths applied after that.
Logits are unbounded and can be negative, so they are pushed through softmax, which exponentiates each one and normalises so they sum to 1:
pᵢ = exp(zᵢ / T) / Σ⫺ exp(z⫺ / T)
Because of the exponential, small gaps in logits become large gaps in probability. A token scoring 2 points higher is not slightly more likely — it is roughly seven times more likely.
That T in the formula divides every logit before the exponential, which stretches or compresses the gaps between them:
Temperature never changes the ranking, only the confidence spread. Watch the entropy readout as you drag it — that number is the distribution's uncertainty in bits.
Even after temperature, the long tail of thousands of near-zero tokens still holds meaningful total probability. Sample from it often enough and you eventually draw something absurd. Two filters prevent this:
After truncating, the survivors are renormalised so they sum to 1 again — which is why the surviving bars grow when you tighten the filter.
The model supplies a distribution; the decoding strategy supplies the behaviour. Identical weights can produce rigid factual answers or freewheeling prose depending purely on temperature and truncation. And because each pick is appended and fed back in, one unlucky sample early on steers everything that follows — which is why these knobs matter far more than their obscurity suggests.
A model does not output words — it outputs a score for every token in its vocabulary. Reshape those scores with temperature, top-k and top-p, and watch the same model turn from deterministic to wildly creative.