Bring the mental model from Maximum Likelihood; this page will reuse it instead of restarting from zero.
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.
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.
Copy-only prompts — each action copies a page-grounded prompt to your clipboard. Nothing is sent by this site.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
Let zi be the logits for token i∈V at some step.
Temperature rescales logits
Temperature τ>0 produces:
- Smaller τ sharpens (more deterministic).
- Larger τ flattens (more exploratory).
Nucleus (top-p) truncation deletes the tail, then renormalizes
Let Sp be the smallest set of tokens whose probability mass is at least p (after sorting by probability):
Then you sample from p′, the truncated and renormalized distribution.
This is why top-p changes behavior: it literally changes the distribution you sample from.
Copy-only prompts — each action copies a page-grounded prompt to your clipboard. Nothing is sent by this site.
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))
Copy-only prompts — each action copies a page-grounded prompt to your clipboard. Nothing is sent by this site.
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
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.
Choose what to inspect in Decoding & Sampling: Temperature, Top-p & Inference-Time Control. This shared fallback is an observation guide, not evidence of learning.
Use the demo to see how temperature/top-p reshape the distribution step by step, and how small changes alter which toy tokens survive.
Copy-only prompts — each action copies a page-grounded prompt to your clipboard. Nothing is sent by this site.
Concept: 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?
Object contextLLM Systems
concept:llm-systems/decoding-samplingDecoding & 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?
Start with the prediction checkpoint, then compare the reveal to the mental model.
Take this moveStudy modes
Keep the object fixed; change the lens.Route back through the notebook
Carry the same object through intuition, math, code, and demo.
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.
After The First Pass
Turn the concept into an inspected object.
The lower panels are one second act: keep the object fixed, inspect it visually, check source boundaries, practice transfer, then attach the 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.
What is the smallest example that makes Decoding & Sampling: Temperature, Top-p & Inference-Time Control click without losing the math?
concept:llm-systems/decoding-samplingsources: holtzman-2019-nucleus
Open the closest source note before trusting the local explanation.
1 selected-object source shown first; 1 reference total.
Audit the claim boundary, then ask from the same selected object.
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.
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...
Checks temperature softmax and nucleus top-p truncation/renormalization only; not universal decoding-quality settings, safety behavior, task optimality, top-k guarantees, repetition penal...
Claim Review
How inference settings reshape the next-token distribution into actual model behavior: temperature, nucleus sampling, and why decoding is a control knob.
What is the smallest example that makes Decoding & Sampling: Temperature, Top-p & Inference-Time Control click without losing the math?
concept:llm-systems/decoding-samplingsources: holtzman-2019-nucleus
Treat every claim as provisional until source support and a local witness agree.
1 structured claim check on this concept.
Run the prediction or practice transfer before asking for a grounded review.
Publisher-side editorial review is not independent replication. Claims without it still need exact source-support review. 1 reference and 3 local witnesses are available for inspection.
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....
Checks 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 be...
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 notebook
Use the idea, then test it somewhere new
How inference settings reshape the next-token distribution into actual model behavior: temperature, nucleus sampling, and why decoding is a control knob.
What is the smallest example that makes Decoding & Sampling: Temperature, Top-p & Inference-Time Control click without losing the math?
concept:llm-systems/decoding-samplingsources: holtzman-2019-nucleus
Use one state from Decoding & Sampling: Temperature, Top-p & Inference-Time Control to explain what changes, why it changes, and which assumption the explanation needs.
No learner move yet; no learning state is inferred.
Write first, use only the help you need, then try a new case without it.
Use one state from Decoding & Sampling: Temperature, Top-p & Inference-Time Control to explain what changes, why it changes, and which assumption the explanation needs.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Write an attempt before asking the companion.
0 of 3 progressive hints opened.
This draft and any AI response do not establish mastery; a later unassisted case can.
- ObjectConceptDecoding & Sampling: Temperature, Top-p & Inference-Time Control
- PredictBefore revealDecoding & Sampling: Temperature, Top-p & Inference-Time Control pred...
- WitnessCompare codeDecoding & Sampling: Temperature, Top-p & Inference-Time Control code...
- RoomAsk groundedChecking local snapshot
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?
These are fixed, deterministic perspectives derived from the selected object. They do not represent people, community contributions, or independent review.
Source ids holtzman-2019-nucleus must support the exact object, not just the surrounding topic.
Treat this as a mechanism object: connect the definition to one equation, code witness, or demo before broadening the discussion.
Ask the learner to perturb one representation, then check whether the same invariant survives in math, code, and demo.
The learner can state the mechanism in their own words
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 Deterministic role lenses for this object: - Boundary: fixed perspectives, not people, community contributions, or independent review - Source-checking summary: Treat this as a mechanism object: connect the definition to one equation, code witness, or demo before broadening the discussion. - Proposed experiment: Ask the learner to perturb one representation, then check whether the same invariant survives in math, code, and demo. - Teach/transfer move: Turn the mechanism into one sentence that predicts a neighboring concept. - Assumptions: - Source ids holtzman-2019-nucleus must support the exact object, not just the surrounding topic. - The stable content-object key lets local drafts, prompts, and route memory attach without changing the source page. - The concept explanation is local atlas prose until checked against its math, code, and source support. - Prerequisite gaps should become a repair route, not a reason to leave the object vague. - Role-lens requests: - Learner: ask for "Ask what would make "Decoding & Sampling: Temperature, Top-p & Inference-Time Control" feel predictable rather than familiar." | assumption: Source ids holtzman-2019-nucleus must support the exact object, not just the surrounding topic. | next action: The learner can state the mechanism in their own words - Researcher: ask for "Source ids to inspect: holtzman-2019-nucleus" | assumption: The stable content-object key lets local drafts, prompts, and route memory attach without changing the source page. | next action: The learner can name the prerequisite that would repair confusion - Experimenter: ask for "Choose one variable or condition to perturb before asking for an explanation." | assumption: The concept explanation is local atlas prose until checked against its math, code, and source support. | next action: The learner can predict how the mechanism changes under one perturbation - Professor: ask for "Find the smallest transferable rule a learner could reuse without the AI." | assumption: Prerequisite gaps should become a repair route, not a reason to leave the object vague. | next action: Teach or transfer: Turn the mechanism into one sentence that predicts a neighboring concept. 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. Current deterministic role lens for this object: - Role lens: Learner - Evidence request: Ask what would make "Decoding & Sampling: Temperature, Top-p & Inference-Time Control" feel predictable rather than familiar. - Assumption to keep visible: Source ids holtzman-2019-nucleus must support the exact object, not just the surrounding topic. - Proposed experiment: Ask the learner to perturb one representation, then check whether the same invariant survives in math, code, and demo. - Next action: The learner can state the mechanism in their own words
concept/concept-notebook/llm-systems/decoding-sampling
concept:llm-systems/decoding-sampling