Lakshya

AI from First Principles · Chapter 10 of 10

Temperature, top-p, and how the next word is chosen

The one place randomness enters a language model, what each knob actually does, and why the current answer has changed.

8 min read2 diagramsAll 10 chapters

Chapter 1 covered what a model is. This is the step at the very end that turns its output into text — the only place randomness enters the system, and the reason the same prompt can give you two different answers.

THE THING EVERY KNOB ACTS ON — ONE DISTRIBUTION, PER TOKEN A model does not choose a word. For every position it outputs a probability for every token in its vocabulary — often 100,000+ of them. “The capital of France is ___” might come out as: Paris0.92Lyon0.04located0.02a0.01home0.006cheese0.002…the rest…~100k more Every sampling parameter is a rule for turning that distribution into ONE token. That is all any of them do. TEMPERATURERESHAPES the whole curve beforepicking. Low = peakier, high = flatter.0 ≈ always take the top one.TOP-KTRUNCATES to the k most likely,then samples among those.Fixed count — ignores the shape.TOP-P (nucleus)TRUNCATES to the smallest set whoseprobabilities add up to p.ADAPTIVE — and that is the point. Why top-p beats top-k: above, top-k=5 would keep four junk options alongside “Paris”. Top-p=0.95 keeps two, because the model was alreadysure. On a genuinely open question the same top-p would keep forty. It widens when the model is uncertain and narrows when it is not.
Fix this picture first. Every parameter below is a rule for collapsing that distribution into one token — nothing more exotic than that.

Greedy decoding, and why nobody uses it alone

The simplest rule: always take the highest-probability token. It is deterministic in principle, and it produces noticeably poor text — it loops, repeats phrases, and collapses into the same safe constructions. The reason is worth understanding: at every step it takes the locally most likely token, and a sequence of locally optimal choices is not a good sentence. Human writing is not the most probable continuation at every position.

Temperature

Temperature reshapes the distribution before anything is picked from it. The model's raw scores are divided by the temperature before being turned into probabilities, which has a simple effect:

  • Below 1 — the peaks get peakier. Likely tokens become more likely, unlikely ones nearly vanish. More focused, more repetitive.
  • Exactly 1 — the model's own distribution, untouched.
  • Above 1 — the curve flattens. Unlikely tokens get a real chance. More varied, and past about 1.5 it starts producing genuine nonsense.
  • At 0 — mathematically undefined (you would divide by zero), so every implementation special-cases it to mean greedy. Which is why ‘temperature 0’ and ‘greedy’ are used interchangeably.

The intuition that helps: temperature does not make the model smarter or more creative. It changes how willing the sampler is to take the model's second and third guesses. ‘More creative’ and ‘more likely to be wrong’ are the same dial.

Top-k and top-p

Both truncate the distribution before sampling, and the difference between them is the whole reason one won.

Top-k keeps the k most likely tokens and discards the rest. Simple, and blind to how confident the model is. When the model is certain, k=50 drags in 49 bad options. When it is genuinely uncertain across hundreds of reasonable words, k=50 cuts off good ones.

Top-p, also called nucleus sampling, keeps the smallest set of tokens whose probabilities add up to p. That makes it adaptive. At p=0.95, a confident prediction keeps two or three candidates and an open-ended one keeps forty. It widens exactly when the model is unsure and narrows when it is not, which is what you wanted top-k to do and it could not.

Stacking them: top-k and top-p filter first, temperature reshapes what survives. Setting all three at once is common and usually unnecessary — temperature plus top-p is the pair that carries almost all the benefit.

The repetition controls

  • Frequency penalty — reduces a token's probability in proportion to how often it has already appeared.
  • Presence penalty — a flat reduction once it has appeared at all, pushing toward new subject matter.
  • Repetition penalty — a multiplicative version of the same idea.
  • Use them sparingly. They are blunt: they will happily penalise a technical term, a product name or a variable that legitimately needs repeating, and the result reads like someone avoiding a word.

What to actually set

TaskTemperatureNotes
Classification, extraction, structured output0You want the same input to give the same label. Variation here is pure downside.
Code generation0 to 0.2There is usually one right answer and creativity is not a virtue.
Factual question answering, retrieval0 to 0.3Higher settings do not make it more accurate — they make it more willing to guess.
Summarisation0.3 to 0.5A little variation reads better without inventing anything.
Conversation, drafting0.7 to 1.0The default range in most chat interfaces.
Brainstorming, deliberate variety1.0 and upAnd generate several, rather than turning it up further.

Raising temperature does not make a model more accurate, and it is a surprisingly common belief that it does.

If the model is wrong at temperature 0, it is wrong because the highest-probability answer is wrong. Turning up the temperature makes it sample further down a distribution whose top was already incorrect.

Wrong answers are a knowledge or context problem — see retrieval. Sampling parameters cannot fix them.

Temperature 0 is not reproducible

THE MISCONCEPTION WORTH FIXING “Set temperature to 0 and the output is deterministic.” — NOT TRUE IN PRODUCTION. It removes the sampling randomness. It does not make the numbers underneath reproducible. BATCHINGYour request is batched with other people’s, and the batch size varies.Different batch shape → different kernel → a slightly different number.FLOATING POINTAdding the same numbers in a different ORDER gives a different result.GPUs parallelise sums, and the order is not fixed run to run.MIXTURE-OF-EXPERTSWhich experts a token is routed to can depend on the rest of the batch.So the same prompt can be processed by a different sub-network entirely. Any of those can flip a near-tie between two tokens. One different token early on changes everything after it. So: temperature 0 buys you CONSISTENCY, not REPRODUCIBILITY. Build tests that tolerate variation, or you will chase ghosts.
This surprises people, and it wastes real debugging time. Three separate mechanisms, none of which you control.

The practical consequence: do not write a test that asserts an exact output string from a model, even at temperature 0. It will pass for weeks and then fail for reasons that have nothing to do with your code. Assert on structure, on a schema, on a property that must hold, or use a model-graded check with a tolerance. Anything else is a flaky test you will eventually delete in frustration.

And the part that has changed — check before you tune

On current reasoning models, these parameters are often simply gone

As models moved to built-in reasoning, several providers removed sampling controls altogether rather than deprecating them.

On Anthropic's current models — Opus 5, Sonnet 5, Opus 4.8 and 4.7, Fable 5 — sending temperature, top_p or top_k returns a 400 error. Not ignored. Rejected.

What replaces them is a different kind of control: an effort setting (how hard to think, from low to max) and adaptive thinking. You are no longer tuning how the next token is picked; you are tuning how much reasoning happens before the answer.

So this chapter is doing two jobs. The mechanics are permanently worth understanding — they explain why models vary, why greedy text is poor, and why turning up temperature does not buy accuracy, and they still apply to every open-weight model you self-host. But if you are writing against a current frontier API, check whether the parameter exists before you spend an afternoon tuning it.

On a model you run yourself, all of these controls are fully available and you set them per request — which is one more concrete difference between using an API and hosting your own.

Check yourself

What does a model actually output at each step?A probability for every token in its vocabulary — often more than 100,000. Every sampling parameter is a rule for collapsing that into one token.
Why is greedy decoding poor?It takes the locally most likely token at every step, and a sequence of locally optimal choices is not good writing. It loops and repeats.
What does temperature do?Reshapes the distribution before sampling — below 1 sharpens it, above 1 flattens it. At 0 it is special-cased to mean greedy.
Why did top-p win over top-k?Top-p is adaptive. It keeps the smallest set summing to p, so it narrows when the model is confident and widens when it is not. Top-k is a fixed count blind to confidence.
Does raising temperature improve accuracy?No. If the top answer is wrong, higher temperature samples further down a distribution whose top was already wrong. That is a knowledge problem, not a sampling one.
Why is temperature 0 not reproducible?Variable batch shapes, floating-point addition order on GPUs, and expert routing in mixture-of-experts models. Any of those can flip a near-tie, and one different token changes everything after it.
What does that mean for tests?Never assert an exact output string. Assert on structure, a schema, a property that must hold, or a graded check with tolerance.
What has changed on current models?Several providers removed these parameters. On Anthropic's current models, sending temperature, top_p or top_k returns a 400 — replaced by an effort setting and adaptive thinking. They remain fully available on open-weight models you host yourself.
← Open, open-weight, and closed — what the words actually meanPractice bank →