Sampling Parameters

What Are Sampling Parameters?

When an LLM generates text, it computes a probability for every possible next token in its vocabulary. Sampling parameters are the settings that control how a token is actually chosen from that distribution.

The Three Main Sampling Parameters
  • Temperature — reshapes the probability distribution before sampling
  • Top-k — restricts sampling to the k most probable tokens
  • Top-p (nucleus sampling) — restricts sampling to the smallest set of tokens whose cumulative probability reaches p

Temperature

Temperature is a scalar value that reshapes the probability distribution before one token is selected — controlling how conservative or exploratory the model is. In short: the lower the number, the more rigid the response; the higher the number, the more creative the response becomes.

The Mechanism

The model first computes raw scores (logits) for each token. Temperature divides all logits before they are converted to probabilities: adjusted logit = original logit ÷ temperature. A small temperature makes differences between logits larger (sharpens the distribution). A large temperature shrinks those differences (flattens the distribution).

0

Temperature = 0 (Greedy) — always selects the single most probable token. Fully deterministic: same prompt always produces the same output. Best for structured output and code

Low

Temperature 0.1–0.4 — strongly prefers high-probability tokens. Very consistent across runs. Best for factual Q&A, data extraction, JSON generation

Mid

Temperature 0.5–0.8 — balanced range that preserves learned preferences while allowing moderate variation. Best for general assistants and chat

High

Temperature 0.9–1.5 — model explores less probable tokens. More creative but higher risk of errors. Best for creative writing and brainstorming

Common Misconception

Temperature only affects the sampling step — which token is selected from the probability distribution the model already computed. It does not change the model's underlying knowledge or probability estimates. Low temperature makes outputs deterministic, not accurate. Hallucinations can happen at any temperature.

Top-k Sampling

Top-k sampling keeps only the k highest-probability tokens at each step and samples from those.

01

Compute probabilities — model assigns a probability to every token in its vocabulary

02

Keep the top k — all tokens outside the top k are set to probability zero; they cannot be selected

03

Renormalize — the remaining k tokens are rescaled so their probabilities sum to 1

04

Sample — one token is drawn from the filtered, renormalized distribution

The Fixed-Cutoff Problem

Top-k uses the same value regardless of how confident the model is. If P("Paris") = 0.92 and k = 50, the model still considers 49 nearly-impossible alternatives alongside a near-certain answer. Conversely, if the model is genuinely uncertain across 200 equally-likely tokens, k = 50 arbitrarily cuts off half of the legitimate options. Top-k does not adapt to the model's confidence.

Top-p Sampling (Nucleus Sampling)

Top-p sampling — also called nucleus sampling — solves the fixed-cutoff problem by adapting the candidate pool size to the model's actual confidence at each step.

01

Sort tokens by probability — from highest to lowest

02

Build the nucleus — add tokens from the top until their cumulative probability reaches p

03

Sample from the nucleus — only tokens inside the cumulative-probability threshold are eligible

When the model is confident

P("Paris") = 0.92. With p = 0.9, the nucleus is just one token — very deterministic for this step.

When the model is uncertain

10 tokens each have ~0.09 probability. With p = 0.9, roughly 10 tokens are needed to reach the threshold. More variety allowed.

Why Top-p Is Usually Preferred

Top-p automatically narrows when the model is confident and expands when the model is uncertain. It uses only as much diversity as the model's probability distribution actually warrants. This is why top-p tends to outperform top-k in practice — it is self-adjusting.

How Temperature, Top-k, and Top-p Work Together

In most production deployments, all three parameters are set together. They apply in sequence:

01

Temperature is applied first — reshapes the logits before softmax, making the distribution sharper or flatter

02

Top-k is applied next — filters to only the k highest-probability tokens from the temperature-adjusted distribution

03

Top-p is applied after — further filters the remaining candidates based on cumulative probability

04

Sampling draws one token from what remains

Common Production Defaults

General-purpose chat: temperature = 0.7, top_p = 0.9, top_k disabled
Code and structured output: temperature = 0.0–0.2, top_p = 1.0
Creative writing: temperature = 0.9–1.2, top_p = 0.95
Setting top_p = 1.0 disables nucleus filtering. Setting top_k = 1 or temperature = 0 gives greedy decoding.

Summary

Key Takeaways
  • Sampling parameters control how a token is chosen from the model's probability distribution
  • Temperature reshapes the distribution before sampling — low sharpens it, high flattens it
  • Temperature = 0 is fully deterministic greedy decoding
  • Top-k keeps the k most probable tokens with a fixed cutoff that does not adapt to confidence
  • Top-p (nucleus sampling) adapts the candidate pool size automatically to the model's confidence
  • Temperature, top-k, and top-p are applied in sequence: temperature first, then top-k, then top-p
  • None of these parameters change the model's knowledge or accuracy — they only affect which token gets selected

Need Help?

Ask the AI if you need help understanding temperature, top-k, or top-p sampling, how they reshape or filter the probability distribution, how they interact with each other, or what settings to use for different types of tasks.