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.
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 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).
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
Temperature 0.1–0.4 — strongly prefers high-probability tokens. Very consistent across runs. Best for factual Q&A, data extraction, JSON generation
Temperature 0.5–0.8 — balanced range that preserves learned preferences while allowing moderate variation. Best for general assistants and chat
Temperature 0.9–1.5 — model explores less probable tokens. More creative but higher risk of errors. Best for creative writing and brainstorming
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 keeps only the k highest-probability tokens at each step and samples from those.
Compute probabilities — model assigns a probability to every token in its vocabulary
Keep the top k — all tokens outside the top k are set to probability zero; they cannot be selected
Renormalize — the remaining k tokens are rescaled so their probabilities sum to 1
Sample — one token is drawn from the filtered, renormalized distribution
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 — also called nucleus sampling — solves the fixed-cutoff problem by adapting the candidate pool size to the model's actual confidence at each step.
Sort tokens by probability — from highest to lowest
Build the nucleus — add tokens from the top until their cumulative probability reaches p
Sample from the nucleus — only tokens inside the cumulative-probability threshold are eligible
P("Paris") = 0.92. With p = 0.9, the nucleus is just one token — very deterministic for this step.
10 tokens each have ~0.09 probability. With p = 0.9, roughly 10 tokens are needed to reach the threshold. More variety allowed.
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.
In most production deployments, all three parameters are set together. They apply in sequence:
Temperature is applied first — reshapes the logits before softmax, making the distribution sharper or flatter
Top-k is applied next — filters to only the k highest-probability tokens from the temperature-adjusted distribution
Top-p is applied after — further filters the remaining candidates based on cumulative probability
Sampling draws one token from what remains
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.
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.