Bring the mental model from Maximum Likelihood; this page will reuse it instead of restarting from zero.
LLM Systems
Decoding & Sampling: Temperature, Top-p & Inference-Time Control
How inference settings reshape the next-token distribution into actual model behavior: temperature, nucleus sampling, and why decoding is a control knob.

Concept Structure
Decoding & Sampling: Temperature, Top-p & Inference-Time Control
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learning map
Decoding & Sampling: Temperature, Top-p & Inference-Time ControlConceptual Bridge
What should feel connected as you move through this page.
How inference settings reshape the next-token distribution into actual model behavior: temperature, nucleus sampling, and why decoding is a control knob.
The next edge should feel earned: use the demo prediction here before following Speculative Decoding: Lossless Multi-Token Generation.
01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
Training gives you a next-token distribution. Decoding is what turns that distribution into actual behavior.
Two products can ship the "same model" and feel completely different because decoding choices differ:
- greedy vs sampling,
- temperature (how sharp the distribution is),
- top-p/top-k truncation (how much tail you delete),
- penalties and constraints (repetition, min length, format forcing).
So decoding is not a cosmetic detail. It's an inference-time control surface that trades off determinism, diversity, and failure modes like repetition loops.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
Let be the logits for token at some step.
Temperature rescales logits
Temperature produces:
- Smaller sharpens (more deterministic).
- Larger flattens (more exploratory).
Nucleus (top-p) truncation deletes the tail, then renormalizes
Let be the smallest set of tokens whose probability mass is at least (after sorting by probability):
Then you sample from , the truncated and renormalized distribution.
This is why top-p changes behavior: it literally changes the distribution you sample from.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
import numpy as np
def softmax(z):
z = z - z.max()
ez = np.exp(z)
return ez / ez.sum()
def top_p_filter(p, probs):
idx = np.argsort(-probs)
keep = np.zeros_like(probs, dtype=bool)
cum = 0.0
for i in idx:
keep[i] = True
cum += probs[i]
if cum >= p:
break
q = probs * keep
return q / q.sum()
rng = np.random.RandomState(0)
logits = np.array([3.0, 2.0, 1.0, 0.2, -0.5]) # 5 tokens
for tau, p in [(1.0, 1.0), (0.7, 0.9), (1.4, 0.9)]:
probs = softmax(logits / tau)
probs = top_p_filter(p, probs)
samples = rng.choice(len(probs), size=20000, p=probs)
counts = np.bincount(samples, minlength=len(probs)) / len(samples)
print("tau=", tau, "top_p=", p, "freqs=", np.round(counts, 3))
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Use the demo to see how temperature/top-p reshape the distribution step by step, and how small changes alter which toy tokens survive.
Live Concept Demo
Explore Decoding & Sampling: Temperature, Top-p & Inference-Time Control
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what Decoding & Sampling: Temperature, Top-p & Inference-Time Control should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
How inference settings reshape the next-token distribution into actual model behavior: temperature, nucleus sampling, and why decoding is a control knob.

Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change Decoding & Sampling: Temperature, Top-p & Inference-Time Control should make visible.
Visual Inquiry
Make the image answer a mathematical question
How inference settings reshape the next-token distribution into actual model behavior: temperature, nucleus sampling, and why decoding is a control knob.
Which visible object should carry the first intuition?
Pick the cue that should make Decoding & Sampling: Temperature, Top-p & Inference-Time Control easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
Primary nucleus sampling source. Sec. 3.1 defines the top-p vocabulary, rescales the truncated distribution, and samples from a dynamically sized nucleus; Sec. 3.3 defines temperature softmax.
Open sourceClaim Review
How inference settings reshape the next-token distribution into actual model behavior: temperature, nucleus sampling, and why decoding is a control knob.
Claims without a substantive review badge still need exact source-support review.
holtzman-2019-nucleus
Use equation, code, and demo objects to check whether the source support is operational.
Holtzman et al. define nucleus sampling as a top-p vocabulary whose cumulative probability exceeds p, then rescale that truncated distribution for sampling; the candidate set changes with distribution shape. Sec. 3.3 defines temperature softmax over logits. Local math/code/demo are toy distribution-shaping witnesses.
Sources: The Curious Case of Neural Text DegenerationChecks temperature softmax and nucleus top-p truncation/renormalization only; not universal decoding-quality settings, safety behavior, task optimality, top-k guarantees, repetition penalties, beam-search behavior, or production sampler implementations. Demo is toy/local only.A bounded review summary is present; still check caveats and exact source scope.Checked Holtzman Sec. 3.1 and 3.3: temperature is softmax re-estimation over logits divided by temperature, while nucleus/top-p selects a smallest cumulative-probability set, rescales the truncated distribution, samples from a dynamically sized nucleus, and targets the unreliable tail. The first two equations, first code witness, and updated demo are toy/local distribution-shaping witnesses only.
Reviewer: codex+oracle; reviewed 2026-05-07Practice Loop
Try the idea before it explains itself
How inference settings reshape the next-token distribution into actual model behavior: temperature, nucleus sampling, and why decoding is a control knob.
Before touching the demo, predict one visible change that should happen in Decoding & Sampling: Temperature, Top-p & Inference-Time Control.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to an exact object
Pick the concept, equation, source, code witness, claim, misconception, or demo state before asking for help. The handoff stays grounded to that object.Open the draft below to save one note and next action in this browser.
Decoding & Sampling: Temperature, Top-p & Inference-Time Control
What is the smallest example that makes Decoding & Sampling: Temperature, Top-p & Inference-Time Control click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays locally in this browser for concept:llm-systems/decoding-sampling.
- Source ids to inspect: holtzman-2019-nucleus
- Definition, prerequisite, and contrast concept links
- The equation or code witness that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - Decoding & Sampling: Temperature, Top-p & Inference-Time Control Object key: concept:llm-systems/decoding-sampling Context: LLM Systems Anchor id: concept/concept-notebook/llm-systems/decoding-sampling Open question: What is the smallest example that makes Decoding & Sampling: Temperature, Top-p & Inference-Time Control click without losing the math? Evidence to inspect: - Source ids to inspect: holtzman-2019-nucleus - Definition, prerequisite, and contrast concept links - The equation or code witness that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/llm-systems/decoding-sampling
concept:llm-systems/decoding-sampling