Predicting the next token
8 min read
Why the same prompt answers differently twice
Send the exact same prompt to an LLM API twice and you will often get two different opening words. Engineers new to LLMs sometimes assume the model is moody, or that its weights shifted on the server.
Neither is true. The neural network is deterministic math: given the same tokens, the forward pass produces the exact same numbers every time. The randomness happens after the model finishes, when the serving layer picks one token from a probability distribution.
To understand temperature, top-p, structured outputs, and why models occasionally emit garbage tokens, you need to trace the three stages inside a single step of Next-token predictionThe one task an LLM is trained on: given the text so far, give a probability to every token that could come next. A reply is written by repeating this prediction one token at a time..
- Raw scores (logits)The final layer outputs one unbounded number per vocabulary token.
- Probabilities (softmax)Softmax exponentiates the logits and normalises them so they sum to 1.
- Token pick (sampling)Greedy decoding takes the max; sampling draws randomly by weight.
Logits: raw scores with no ceiling
The final layer of the model does not emit probabilities. It outputs LogitsThe raw scores a model gives to every token in its vocabulary before softmax. A higher logit means a more likely token. Logits can be negative and do not add up to 1.: plain floating-point numbers that can be negative, positive, tiny, or huge. They do not add up to 1.
Say your prompt is "The HTTP status for Not Found is". Across a 100,000-token vocabulary, the logits might look like 6.2 for "404", 3.4 for "400", 1.8 for "not", and -4.5 for "banana".
Higher means more likely, lower means less likely — that is all a logit guarantees on its own.
Softmax: gaps matter, absolute values do not
To sample a token, we need proper probabilities between 0 and 1 that add up to 1. That is the job of SoftmaxA formula that turns a list of scores into probabilities. It raises e to the power of each score and divides by the total, so every result is positive and they add up to 1.: exponentiate every logit (e ** score) and divide each by the sum.
Because e ** x grows fast, softmax amplifies gaps. A lead of 2.5 in logit space turns into more than a 12x lead in probability.
Here is the subtle mathematical property that trips people up in interviews and debugging: adding the same constant to every logit does not change the probabilities at all. If you add +5 to every score, e ** 5 factors out of both numerator and denominator and cancels. Softmax only cares about the gaps between scores.
That also explains the numerical stability trick you will see in every real implementation: subtract max(logits) before calling math.exp. Without that subtraction, large logits overflow float64 to inf and poison your run with NaN.
import math
def softmax(scores):
peak = max(scores.values())
weights = {tok: math.exp(val - peak) for tok, val in scores.items()}
total = sum(weights.values())
return {tok: weight / total for tok, weight in weights.items()}
logits = {"Paris": 6.0, "Lyon": 3.5, "banana": -1.0}
shifted = {tok: val + 5.0 for tok, val in logits.items()}
print("original:", {k: round(v, 4) for k, v in softmax(logits).items()})
print("shifted :", {k: round(v, 4) for k, v in softmax(shifted).items()})Running this prints:
original: {'Paris': 0.9234, 'Lyon': 0.0758, 'banana': 0.0008}
shifted : {'Paris': 0.9234, 'Lyon': 0.0758, 'banana': 0.0008}"Paris" only led "Lyon" by 2.5 raw points, yet softmax hands it 92.3% of the probability mass while "banana" drops under a tenth of a percent. And adding 5.0 to every logit left every single probability untouched.
Greedy vs. sampling: where nondeterminism enters
Once softmax hands back a probability table, the serving code has a choice:
- Greedy decoding (
temperature = 0). Always pick the single highest-probability token. Great for JSON extraction, SQL generation, and reproducible evals; terrible for creative writing because it repeats bland phrases. - Weighted sampling (
temperature > 0). Roll a biased die using the probabilities. Most runs pick"Paris"(92.3%), roughly one in thirteen picks"Lyon"(7.6%), and once in a blue moon it picks a long-tail token.
Remember that every chosen token becomes part of the input for the next step. One different token on step 1 shifts every logit on step 2, which is why two runs of the exact same prompt quickly branch into completely different paragraphs.
What this means when you ship
Two practical rules save hours of debugging:
- Keep temperature low for code, tool calls, and classifiers. Use
0or0.1— you are not paying for poetry when you need valid JSON. - Never treat temperature 0 as a bit-identical guarantee. Floating-point batching differences on GPUs can still flip a near-tie between two tokens. Validate outputs in code anyway.
For the full picture of how temperature, top-k, and top-p reshape this distribution before the roll, the next lessons walk through tokenisation and sampling controls — and Jay Alammar's illustrated GPT-2 walkthrough is still one of the clearest visual references on the web.
What are these numbers?
A model outputs 6.0 for "Paris", 3.5 for "Lyon" and -1.0 for "banana".
Logits can be negative and do not add up to 1. Softmax turns them into probabilities.
What happens to the probabilities?
You add 5 to every logit before you apply softmax.
Softmax only depends on the gaps between scores. Adding the same number to every score keeps every gap, so the probabilities do not change.
Why can the same prompt give two different replies?
You send the same prompt twice and get two different first words.
Softmax gives the same probabilities each time. The program then picks a token, and a random pick can differ between runs.
Your reading position is saved
0 of 20 complete
Lesson complete
Predicting the next token
Up next