Skip to main content

Temperature, Top-k & Top-p Explained

Editorial

Introduction

Greedy Decoding vs Sampling ended with two failures: greedy text that loops, and samples that pick characters the model gave almost no chance. The settings in between are temperature, which reshapes the shares, and top-k, top-p and min-p, which cut part of them away before the dart is thrown. Here all four are measured on the tiny GPT, at the places From Logits to Probabilities (Softmax) looked at.

You'll learn:

  • what temperature does to the shares, and what it never changes
  • how top-k, top-p and min-p each decide what to keep
  • why the order of the temperature and the cut is a runtime's choice, and what it changes
  • what cutting the tail buys, measured, and which knobs each provider lets you set

Two Jobs: Reshape and Cut

The knobs do two different jobs on the same shares. Temperature reshapes them: every logit is divided by the temperature before softmax, so the shares grow sharper or flatter, and every character keeps some share. A cut keeps only part of them: top-k, top-p and min-p each drop the characters their rule leaves out, and what is left is divided by its own sum, so it adds up to 100% again. Then the dart is thrown at what is left, as in Greedy Decoding vs Sampling.

Temperature: Sharper or Flatter

At the start of a docstring the tiny GPT gives 'R' 64.7% at temperature 0.5, 26.2% at 1, 13.8% at 1.5 and 8.8% at 2. As the temperature rises the shares flatten: 9 characters have 1% or more at 0.5, 17 at 1, 24 at 1.5 and 26 at 2. Dividing every logit by the same number never changes which of two is larger, so the order of the characters is the same at every temperature, and 'R' stays on top.

The tiny GPT's 96 shares for its next character, and what the sampling knobs do to them. Temperature reshapes the shares and never their order: at the start of a docstring the top character goes from R 64.7% at temperature 0.5, to R 26.2% at temperature 1, to R 8.8% at temperature 2, and 9, 17, 26 characters have 1% or more. At temperature 1 the cuts keep 5 with top-k 5, 19 with top-p 0.9, 10 with min-p 0.1; after "Retur", where the model is sure, top-k 5 still keeps 5, 4 of them under 1%, and top-p 0.9 keeps 1. At temperature 2 the order matters: top-p 0.9 keeps 51 when the temperature comes first, as in Transformers, and 19 when it comes last, as in llama.cpp's default chain; min-p 0.1 keeps 30 and 10. In 600 picks a setting, the picks the model gives under 1% at temperature 1 were at temperature 1, 19 with no cut, 3 with top-k 5, 1 with top-p 0.9, 0 with min-p 0.1; at temperature 1.5, 137 with no cut, 83 with top-p 0.9, 3 with min-p 0.1; at temperature 2, 252 with no cut, 193 with top-p 0.9, 65 with min-p 0.1. Read on 16 November 2026: OpenAI advises changing temperature or top_p, not both; Anthropic's API version 2026-10-21 refuses all three with a 400 error; llama.cpp turns min-p 0.05 on by default.

Top-k: A Fixed Number

Top-k keeps the k largest shares and drops the rest. Fan and colleagues used it in 2018 to write stories: "We randomly sample from the k = 10 most likely candidates from this distribution." Their reason was the tail: "Completely random sampling can introduce very unlikely words". The count is fixed, whatever the shares look like. After "Retur", where the tiny GPT gives 'n' 99.3%, top-k 5 still keeps five characters, 4 of them under 1%. Holtzman and colleagues note that "Values of k greater than 100 are rarely used in practice".

Top-p: The Fewest That Reach p

Top-p keeps the fewest of the largest shares whose sum reaches p, and drops the rest. Holtzman and colleagues proposed it in 2019 as Nucleus Sampling: "sampling from the dynamic nucleus of tokens containing the vast majority of the probability mass". Because the count follows the shares, it adapts. With p at 0.9 the tiny GPT keeps 1 character after "Retur", 19 at the start of a docstring and 23 after "Return a string the ". Along greedy's 200 characters it kept between 1 and 27, 5 at the median. In the same paper, "values of p are usually in [0.9, 1)".

Min-p: A Share of the Top

Min-p keeps every character whose share is at least a set fraction of the largest share. Nguyen and colleagues proposed it in 2024 so that the cut follows the model's confidence. With a fraction of 0.1 the tiny GPT keeps 1 after "Retur", 10 at the start of a docstring and 20 after "Return a string the ". Their paper suggests a fraction between 0.05 and 0.1, which it says "effectively balances creativity and coherence across tasks".

The three cuts in Python, on the tiny GPT's own 96 logits at the start of a docstring (rounded to two places):

import math

# the tiny GPT's 96 logits at the start of a docstring (rounded to two places)
logits = [
    2.84, 2.74, -3.93, -2.11, -1.13, -5.03, -5.46, -5.62, 0.48, 0.35,
    -3.28, -1.30, -1.41, -3.80, -0.64, -0.38, -3.33, -0.84, -0.65, -1.23,
    -1.56, -2.52, -2.87, -2.76, -3.13, -3.05, -2.93, -2.00, -5.27, -2.28,
    -2.84, 2.42, -3.42, -2.69, 3.72, 0.98, 3.35, 2.42, 2.95, 1.80,
    2.23, 1.59, 2.68, -3.22, -0.95, 1.31, 1.34, 2.25, 1.86, 2.49,
    -1.98, 4.73, 2.72, 3.87, 1.01, -0.11, 1.44, -2.85, -0.61, -3.39,
    0.16, -2.83, -6.10, -5.38, -0.52, -0.82, 1.19, -1.04, -0.19, 0.03,
    -0.83, 0.38, -2.29, -0.91, -0.61, -2.79, -3.58, -1.26, -0.09, -0.65,
    0.13, 0.20, -1.34, 0.97, -0.15, -0.87, -0.72, -1.79, -0.33, -1.33,
    -2.19, -3.82, -2.42, -3.22, -6.46, -6.06,
]


def shares(z, t=1.0):
    m = max(x / t for x in z)
    e = [math.exp(x / t - m) for x in z]
    s = sum(e)
    return [x / s for x in e]


def top_k(p, k):
    return sorted(range(len(p)), key=lambda i: -p[i])[:k]


def top_p(p, reach):
    # the fewest of the largest shares whose sum reaches `reach`
    kept, total = [], 0.0
    for i in sorted(range(len(p)), key=lambda i: -p[i]):
        kept.append(i)
        total += p[i]
        if total >= reach:
            break
    return kept


def min_p(p, fraction):
    return [i for i in range(len(p)) if p[i] >= fraction * max(p)]


for t in (1.0, 2.0):
    p = shares(logits, t)
    print(f"temperature {t}: top-k 5 keeps {len(top_k(p, 5))}, "
          f"top-p 0.9 keeps {len(top_p(p, 0.9))}, min-p 0.1 keeps {len(min_p(p, 0.1))}")

Its run:

temperature 1.0: top-k 5 keeps 5, top-p 0.9 keeps 19, min-p 0.1 keeps 10
temperature 2.0: top-k 5 keeps 5, top-p 0.9 keeps 51, min-p 0.1 keeps 30

Top-k keeps its five at both temperatures. At temperature 2 the flatter shares let top-p and min-p keep many more characters, because here the temperature is applied first.

See It: Reshape and Trim

Here are the tiny GPT's 96 shares at one of three places, largest first, as the cut sees them. Pick a cut and a temperature: top-k draws a line after the fifth bar, top-p its running share climbing to 90%, min-p a level at a tenth of the largest. The ticks under the bars show what is kept, and the bar below shows what is left once it adds up to 100%; draw a character to throw the dart at it. Then switch the order and watch what the cut keeps.

The tiny GPT's 96 shares at three places of its own writing: set a temperature and a cut, switch their order, and draw a character from what is left. Every share is the model's own.

After "Retur" at temperature 2, with the temperature first, top-p 0.9 keeps 10 characters and min-p 0.1 keeps 1: when the model is sure, min-p stays sure.

The Order Is the Runtime's

When a temperature and a cut are both set, the runtime decides which comes first. Hugging Face Transformers divides by the temperature first and cuts after: its generation code adds the temperature's step before top-k, top-p and min-p. llama.cpp's default chain applies the temperature last, after top-k, top-p and min-p. For top-k the order changes nothing, since a temperature keeps the order of the characters. For top-p and min-p it changes what is kept: at the start of a docstring at temperature 2, top-p 0.9 keeps 51 characters when the temperature comes first and 19 when it comes last, and min-p 0.1 keeps 30 and 10.

What a Cut Buys, Measured

With the dart thrown 200 times on each of three seeds, as in Greedy Decoding vs Sampling, the tiny GPT took 19 characters it gives under 1% at temperature 1 with no cut. It took 1 with top-p 0.9, 3 with top-k 5 and 0 with min-p 0.1. A higher temperature brings the tail back: at 1.5 the counts were 137 with no cut, 83 with top-p 0.9 and 3 with min-p 0.1; at 2 they were 252, 193 and 65. Top-p made after a high temperature keeps many more characters, the case Nguyen and colleagues built min-p for: methods like top-p, they write, "often struggle to balance quality and diversity, especially at higher temperatures".

A cut can also go too far. At temperature 1, 2 of the nine cut runs ended stuck in a loop, one of them 200 spaces in a row, and no run without a cut did. Holtzman and colleagues saw the same: "all stochastic methods face repetition issues when their tuning parameters are set too low, which tends to overtruncate, mimicking greedy search".

The Knobs in Real APIs

What you can set depends on where the model runs, and these were read on 16 November 2026:

  • OpenAI offers temperature and top_p, and says of top_p: "We generally recommend altering this or temperature but not both."
  • Anthropic removed all three in its API version of 2026-10-21: "The temperature, top_p, and top_k request parameters are removed." A request that sends one gets a 400 error. On the older version, Claude 4.7 and later models accept only the defaults, and for a lowered temperature Anthropic says "No other setting replaces it."
  • Open runtimes choose their own defaults. llama.cpp samples at temperature 0.8 with top-k 40, top-p 0.95 and min-p 0.05; Ollama at 0.8 with top-k 40 and top-p 0.9, and min-p off; vLLM at temperature 1 with every cut off; Transformers takes them from each model's own settings file, and without one samples at temperature 1 with top-k 50.
  • One dial at a time. Temperature and top-p both decide how far a reply strays from the top; change one, and log what you set, because it decides what you can reproduce. What a provider promises about getting the same reply twice is the subject of The Probabilistic Nature of AI (Determinism, Seeds).

Check Yourself

Eight questions on temperature and the three cuts.

Check yourself: temperature and the cuts

1 / 8

Multiple choice

What does a higher temperature change?

Key Takeaways

  • Temperature reshapes the shares and never their order: 'R' at the start of a docstring goes from 64.7% at 0.5 to 8.8% at 2.
  • A cut keeps part of the shares and makes it add up to 100% again: top-k a fixed number, top-p the fewest that reach p, min-p every share at least a fraction of the largest.
  • Top-p and min-p follow the model's confidence (top-p kept 1 where the model was sure, 23 where it was not); top-k does not.
  • Whether the temperature comes before or after the cut is the runtime's choice, and it changes what top-p and min-p keep.
  • Top-p 0.9 and min-p 0.1 took the long shots from 19 to 1 or fewer in 600 picks; cutting too hard can loop, as greedy does.

Next: Controlling Output: Stop Sequences, Max Tokens, Penalties: the settings that decide when a reply stops and how long it may run.