Skip to content
GenAI Learn/Prompting & Model Behavior
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Temperature & Sampling Controls

6 min read

You'll learn to

  • -Understand temperature as a control over how sharply the next-token distribution is peaked
  • -Compare temperature to top-p (nucleus) sampling and frequency/presence penalties
  • -Choose sampling settings appropriate to a given task, from deterministic to creative

At every step of generation, an LLM does not output a single guaranteed next token. It outputs a probability distribution over its entire vocabulary, tens of thousands of candidate tokens, each with some probability of coming next. Sampling settings control how that distribution actually gets turned into one chosen token, and getting them wrong produces exactly the symptom that opens this chapter's corresponding lab level: an AI content generator that is either robotically repetitive, or randomly hallucinating brand names that do not exist.

Temperature: Reshaping the Distribution

Before sampling, the model's raw output scores (logits) are converted into probabilities using softmax. Temperature divides every logit by a value T before that conversion. A low temperature (below 1) sharpens the distribution, the already-most-likely token becomes even more dominant, pushing the model toward its single best guess every time. A high temperature (above 1) flattens the distribution, giving lower-probability tokens a genuinely higher chance of being chosen, which reads as more varied, more surprising, and, past a certain point, less coherent.

Softmax with temperature
p_i = exp(logit_i / T) / Σ_j exp(logit_j / T)
T < 1 sharpens the distribution toward the top candidate. T > 1 flattens it toward uniform.
The exact math the interactive demo below uses, for the prompt "The weather today is ___".

At T=0.2, "sunny" claims 98% of the probability mass, essentially deterministic. At T=1.0 (the un-scaled distribution), the choice is meaningfully spread across the top few plausible candidates. At T=2.0, even "purple", a nonsense answer to "the weather today is", climbs to a non-trivial 1.3%, which is exactly the kind of occasional off-the-wall hallucination high-temperature settings are prone to producing.

Reshaping the Next-Token Distribution

Prompt: "The weather today is ___". Drag temperature and watch the probabilities redistribute.

sunny
53%
nice
24%
cloudy
12%
warm
7.2%
rainy
2.7%
terrible
0.6%
purple
0.0%
0.1 (deterministic)T = 1.002.0 (chaotic)

A balanced mix, weighted toward the more likely tokens.

Click to draw one token from the current distribution.

Top-P (Nucleus) Sampling: A Different Lever

Top-p sampling takes a different approach: instead of reshaping the whole distribution, it restricts sampling to the smallest set of top candidates whose cumulative probability adds up to p (say, the top 90%), then samples only from that shortlist. At low p, only the handful of most likely tokens are even eligible, regardless of how flat or sharp the underlying distribution is. Temperature and top-p are frequently used together in practice: temperature reshapes the distribution's overall sharpness, and top-p trims off the long, unreliable tail of implausible candidates before sampling from what remains.

Frequency & Presence Penalties: Fighting Repetition Directly

Neither temperature nor top-p directly stops a model from repeating itself, a distinct failure mode where the same phrase or sentence structure loops. Frequency and presence penalties subtract a small amount from a token's score every time it has already appeared in the output so far, frequency penalty scaling with how many times, presence penalty applying a flat penalty just for having appeared at all, making already-used tokens progressively less attractive and directly countering repetitive output, independent of temperature.

Matching Settings to the Task
T ≈ 0-0.3
Extraction, classification, code generation, anything with one correct answer
T ≈ 0.5-0.8
General assistant chat, balanced and mostly reliable
T ≈ 0.9-1.3
Creative writing, brainstorming, marketing copy variety
top_p ≈ 0.9-0.95
A common default that trims the extreme tail without over-constraining

Temperature 0 is not the same as a guarantee of an identical output every time. Even at temperature 0, floating-point non-determinism in how a provider batches and executes requests can occasionally produce a different token when multiple candidates are extremely close in probability. If a use case genuinely requires byte-for-byte reproducibility, low temperature reduces variance dramatically but should not be assumed to eliminate it entirely.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

ScaleDojo Logo
Initializing ScaleDojo