A trained LLM does not produce words — it produces a probability distribution over 100,000 tokens at every step. The decoding strategy is the rule that turns those probabilities into text, and it is a major knob between "helpful chatbot" and "incoherent ramble." Temperature, top-p, beam search, speculative decoding: this is the layer where Claude, GPT-4o, and Llama 3.3 all hand control back to you.
After this lesson, you will be able to:
Pick the right decoding strategy (greedy, beam, top-k, top-p, temperature) for translation, creative writing, code, or structured output, and know why each one fails on the wrong task
Tune temperature, top-k, and top-p together without canceling each other out, and avoid the classic 'why is my model producing gibberish' bugs
Explain how speculative decoding gets a 2-3x inference speedup with zero quality loss, and why every major LLM provider uses Medusa, EAGLE, or vLLM-style draft models in production
Use constrained decoding (Outlines, Guidance, JSON mode) to force a model to produce valid JSON, regex-conformant strings, or schema-compliant output without retraining
Don't worry if all the knobs feel overwhelming — once you see them on a real distribution, the temperature and top-p choices become intuitive in about an hour.
A decoder LLM produces a vector of logits z ∈ ℝ^V at every step (one number per vocab token, often V ≈ 30k–100k). Softmax turns those logits into a probability distribution over the vocabulary. Decoding is the rule that turns this distribution into the actual next token.
Maintain b candidate sequences (the beam). At each step, expand each candidate by all V vocab tokens, score the b·V resulting sequences by joint log-probability, keep the top b.
score(x1:t)=i=1∑tlogP(xi∣x1:i−1)
Try it! Open a notebook and run model.generate(..., num_beams=5) on a translation task and the same with num_beams=1 (greedy). For news-headline translation you'll see beam beating greedy by 2-4 BLEU points.
Beam wins when
Translation, summarization, image captioning — tasks with a notion of "correct"
Constrained generation where you'd rather get a high-confidence safe output than a creative one
Beam fails when
Open-ended generation — output is generic, "boring," and statistical-mode-collapsed
Beam sizes >10 — outputs degrade as longer sequences get penalized into vacuous ones
Here is the same idea as a small tree you can run. It uses a made-up table of next-word probabilities, so you can follow every pruning decision by hand.
Try it: run a beam search treeInteractive
Loading visualization...
Do: Pick k = 1 and press Step four times, then press Reset, pick k = 3 and press Run all. Compare the Σlog P labels on the surviving nodes.
Notice: k = 1 keeps one path. k = 3 expands the top three tokens of each live beam, keeps the three best by cumulative log-probability and draws the rest dashed and grey. In this table the line mat then a full stop scores about -1.41 at every width, so a wider beam did not find a better line here.
Why: A wider beam keeps more rivals alive, which only pays when a weak first token leads to a strong continuation. Drawn stand-in, not a trained model: the probabilities are a hand-written table for "the cat sat on the", so the numbers show how pruning works, not how a real model behaves.
Pure sampling: draw the next token from the distribution P(token | x at positions 1 to t). This explores the full vocabulary including the long tail of low-probability tokens, which is usually where the gibberish lives.
To make sampling usable, we shape the distribution before drawing.
T = 0.2-0.4 → code, structured output, factual Q&A
T = 0.7 → default for chat, creative writing
T = 1.0 → model's "natural" distribution
T > 1.2 → high-entropy creative tasks, story brainstorming
T > 2.0 → mostly noise
Here is temperature on a fixed set of eight next-word candidates, so you can watch one number reshape the whole distribution.
Try it: move the temperature sliderInteractive
Loading visualization...
Do: Drag the temperature slider from 1.0 down to 0.5, then up to 5.0, and press Resample at each stop.
Notice: The top token mat holds about 44% at T = 1, about 72% at T = 0.5 and only about 17% at T = 5, where even zebra gets about 9%. The resampled previews repeat mat at low T and wander at high T.
Why: Dividing the logits by T before the softmax is the whole mechanism, and this lab computes exactly that. The eight logits are fixed, hand-picked numbers for "The cat sat on the", not output from a trained model, and the quality labels such as Focused and Chaotic are guides, not measurements.
Why do the two ends of the temperature dial behave the way the cheat sheet says? Open the derivation to see both limits.
Entropy also moves smoothly between those ends, and the next derivation shows it only ever rises as T rises.
Only sample from the k most-probable tokens (set the rest to zero, renormalize).
Vk=top-ktokens by P;P′(v)=∑v′∈VkP(v′)P(v)⋅1[v∈Vk]
Top-k weakness: at positions where the model is very confident (e.g., predicting "the" after "I went to"), top-k=50 still includes 49 unlikely tokens. At positions where the model is very uncertain, top-k=50 may exclude many plausible tokens. This is what nucleus sampling fixes.
Pick the smallest set V_p whose cumulative probability mass is ≥ p. Sample from that set.
Vp=smallest set s.t.v∈Vp∑P(v)≥p;P′(v)=∑v′∈VpP(v′)P(v)⋅1[v∈Vp]
Cutting the tail changes the probabilities of the tokens that remain. Open the derivation to see which tokens the nucleus keeps and why the new probabilities are the old ones divided by the kept mass.
In production you almost always combine them — apply temperature first, then top-p (or top-k).
What Do You Think?
You set temperature=0 and top-p=0.9 simultaneously in the OpenAI API. What controls the generation?
Temperature=0 effectively rounds logits to spike at the argmax (one massive logit, the rest at -∞ after division). Once that happens, top-p sees a distribution with one token at probability 1.0, which trivially covers ≥ 0.9 of mass, so the nucleus contains exactly that one token. Temperature 0 wins; you get greedy decoding regardless of top-p.
Repetition penalty (CTRL, Keskar 2019) divides the logits of recently-emitted tokens by a constant above 1, making them less likely to be picked again. Dividing a negative logit would raise it (-2.0 divided by 1.2 is -1.67, which is larger), so common implementations multiply negative logits by the constant instead (-2.0 times 1.2 is -2.4). Helpful but blunt, since it can suppress legitimate repetition (lists, code).
No-repeat-ngram size simply forbids any n-gram from repeating. Effective for catching loops but breaks legitimate repetition (variable names in code, refrains in poetry).
Min-p sampling (Minh 2024): keep tokens whose probability is ≥ min_p × max_prob. Adaptive like top-p but threshold-based. It is recently gaining traction in open-source serving as a top-p replacement.
Top-k, top-p, and temperature were the workhorses of 2019-2023, but the open-source serving stack has moved on. The samplers below are now first-class options in llama.cpp, vLLM, SGLang, exllamav2, and most local-LLM front-ends. Each addresses a specific failure mode that the older trio handled poorly.
Min-p sampling (Minh 2024). Keep tokens with probability ≥ min_p × max_p — i.e., set the floor as a relative fraction of the most likely token's probability. The threshold is dynamic: when the model is confident, the nucleus shrinks; when it is uncertain, the nucleus widens. Min-p is now the default in llama.cpp and Mistral inference, and it has largely replaced top-p in open-source roleplay/creative-writing setups because it is more robust to the "long tail of garbage" problem that flat top-p suffers from at high temperature.
Vmin-p={x:P(x)≥min_p⋅ymaxP(y)}
Open the derivation to see why min-p is adaptive: the number of tokens it can keep depends on how sure the model is.
Mirostat (Basu 2020, widely adopted 2023-2024). Adaptive sampler that targets a fixed output entropy (often called perplexity surprise τ) by tuning an internal k value on the fly. Useful for long generation where you want consistent variety — neither slowly collapsing into greedy nor drifting into chaos. Mirostat-v2 is the version in most inference servers; it is especially popular for story-writing and dialog generation where you want the model's surprise level to feel uniform across thousands of tokens.
DRY — Don't Repeat Yourself (pcalc 2024). Penalty applied to tokens that would extend a detected n-gram repetition. Unlike no-repeat-ngram (which forbids any repetition outright), DRY uses an exponential penalty that grows with the length of the matched repeated suffix, so legitimate short repetitions (variable names, refrains, "no, no, no") pass through while genuine loops are choked off. Now standard in oobabooga/SillyTavern stacks.
XTC — eXclude Top Choices (p-e-w 2024). Counterintuitive: with some probability, drop the highest-probability tokens before sampling, forcing the model to pick from less obvious continuations. The goal is to escape the strong attractor of greedy-ish outputs without flattening the distribution wholesale. It produces noticeably more creative text on roleplay/fiction tasks, though it is risky on factual workloads.
Eta sampling (Hewitt 2022). Drops tokens whose probability falls below an entropy-dependent threshold η = min(ε, √ε · exp(−H)), where H is the current entropy of the next-token distribution. Because of the minus sign it interpolates between aggressive truncation when the model is confident (small H) and gentler truncation when it is uncertain (large H) — a more principled cousin of min-p.
Top-q sampling. Similar in spirit: keep tokens whose log-probability is within q of the maximum log-probability (i.e., a multiplicative quality floor in log space rather than absolute probability space). Less common than min-p in production but useful for quantized models whose softmax is slightly miscalibrated.
Locally typical sampling (Meister 2023). Selects tokens whose information content (−log P) is close to the expected information content of the distribution, rather than just the most likely tokens. The justification is information-theoretic: human-generated text tends to hover near typical surprise levels, not at the extreme low-surprise tail that greedy decoding produces.
Quick check
Which sampler keeps tokens with probability ≥ min_p × max_p?
#Speculative Decoding: 2-3x Speedup, Zero Quality Loss
The big idea: a fast, small draft model proposes the next k tokens. The big model runs ONE forward pass on all k proposed tokens in parallel, then accepts a prefix of the draft based on a probability ratio test. Most of the time, all k tokens are accepted, so the big model effectively decoded k tokens in one forward pass.
The claim "zero quality loss" is a theorem. Open the derivation to see why accepting with probability min(1, p/q) and resampling from the leftover gives exactly the big model's distribution.
Move the draft and target bars, the lookahead k and the draft cost, and watch that a draft token is accepted with probability min(1, p/q), that a rejection is redrawn from the normalised residual, that the emitted token is distributed exactly as p, and that the acceptance rate equals the shaded overlap.
Loading visualization...
The draft proposes k tokens in a row. Open the derivation to count how many tokens one big-model pass delivers on average.
Tokens per step is not the same as speedup, because the draft also costs time. Open the derivation to include that cost.
Modern speculative variants:
Medusa (Cai 2024) — train extra "Medusa heads" on the big model to predict the next 4 tokens; no separate draft model needed
EAGLE / EAGLE-2 (Li 2024) — train a single-layer draft network that conditions on the big model's hidden states; SOTA acceptance rates
Lookahead decoding (Fu 2024) — Jacobi-style fixed-point iteration with no draft model
Tree-based speculation. Propose a tree of token sequences and verify the path with highest acceptance
Real applications often need exactly-formatted output: JSON conforming to a schema, code matching a regex, SQL with valid column names. Three techniques:
JSON mode / structured outputs (OpenAI, Anthropic, Mistral) — the API accepts a JSON schema and the server-side decoding masks invalid tokens at every step.
Outlines / Guidance — Python libraries that wrap a local model with a Pydantic / regex / context-free-grammar constraint, masking the logits to set invalid tokens to -∞.
Logit bias / fine-tuning — explicit per-token bias (OpenAI's logit_bias) or fine-tuning on schema-conforming examples.
Constrained decoding is "free" — no retraining needed, just decode-time masking. The cost: walk an automaton for the constraint, which can be slow for complex schemas.
Tests: Verify that gibberish tokens are excluded by top-p but appear in pure sampling. Verify that lowering temperature concentrates output on the top tokens. Verify your min-p implementation produces stable output.
Sampling PlaygroundInteractive
Adjust temperature, top-k, and top-p and watch how the same model produces wildly different completions of the same prompt.
Greedy is deterministic, beam is mode-seeking, sampling is creative — pick by task. Greedy and beam for translation/summarization/code/structured output. Top-p sampling for chat, creative writing, and any open-ended task.
Top-p is the modern default for sampling — top-k can't adapt to peaked vs flat distributions. Top-p (nucleus) sampling adjusts the candidate set per-position to match the distribution's shape; that's why it became standard after Holtzman 2019.
Temperature is the personality dial; T=0 → greedy, T~0.3 → factual/code, T=0.7 → chat, T>1 → creative, T>1.5 → noise. Combine with top-p/top-k; temperature is applied first, then the truncation.
Speculative decoding gives 2-3x speedup with zero quality loss. Production LLMs use Medusa, EAGLE, or vLLM speculation — the technique is provably equivalent to sampling from the big model.
Constrained decoding (JSON mode, Outlines, Guidance) makes models reliably emit schema-conforming output without retraining. Mask invalid tokens at decode time — fast, free, and the modern way to plug LLMs into structured pipelines.
You're building a customer-support chatbot that generates open-ended replies. Which decoding strategy is the best default?
You now have the full decoding toolkit. Next up: prompt engineering — the art of designing the input that drives all of this decoding machinery toward what you actually want.