Temperature & Top-p Explainer
See how temperature, top-p and top-k reshape an LLM next-token distribution. Drag the sliders, watch the bar chart update. Free, runs in your browser.
Temperature & Top-p Explainer
Low = sharp, 0 = always the top token. High = flat.
Keep the smallest set of tokens that sum to ≥ p. 1 = off.
Keep only the k most probable tokens. 0 = off.
Show the numbers behind the bars
| Token | Logit | p the cut sees | Running total | Final p |
|---|
Use your own candidates
Up to 20 candidates. A logprob from an API (OpenAI returns up to 20 per position) works as a logit, because softmax cancels the constant between them; the result is renormalised over only the tokens you give.
The example logits are a fixed teaching set, not output from any model. Greyed bars are removed by top-k or top-p; kept bars are renormalised to 100%. Several current APIs no longer accept these settings: see the table below. Everything runs in your browser — no model is called.
How to Use the Temperature & Top-p Explainer
Start at the defaults
Temperature 1, top-p 1 and top-k off show the plain softmax of the example logits — nothing is filtered yet. This is your baseline to compare against.
Slide the temperature
Drag temperature toward 0 and watch the bars collapse onto the single most likely token (greedy decoding). Push it toward 2 and the bars flatten out — rare tokens like “xylophone” get a visible share.
Apply top-p and top-k
Lower top-p to cut the long tail — only the tokens that make up the top “nucleus” of probability survive. Raise top-k from off to cap the candidate count outright. Filtered tokens grey out at 0%.
Switch the order
Hugging Face applies temperature before the cuts; llama.cpp applies it after. With temperature away from 1 and top-p below 1, switching can change how many tokens survive. Open the numbers to see the running total each cut used.
How Sampling Knobs Reshape an LLM's Next Word
From logits to a probability distribution
Every time a large language model predicts the next token, it doesn't pick a word directly — it produces a logit (a raw, unnormalised score) for every token in its vocabulary. Those logits are turned into a probability distribution by the softmax function, which exponentiates each score and divides by the total so everything sums to one. The example in this tool uses a small, fixed set of eight candidates so you can see the whole distribution; a real model does the same across a vocabulary of tens or hundreds of thousands of tokens. Once you have probabilities, the question becomes how to choose, and temperature, top-p and top-k are the three settings most runtimes have exposed for it.
Temperature divides every logit by the temperature value before the softmax: p = exp(u / t) ÷ Σ exp(u / t), the form Holtzman and colleagues give in the paper that introduced nucleus sampling. When temperature is high (toward 2), the gaps between logits shrink, the softmax output flattens, and unlikely tokens get a real chance of being chosen. When temperature is low, the gaps stretch and probability piles onto the highest-scoring token; the same paper notes that lowering temperature improves quality at the cost of diversity, and in its own experiments temperatures below 0.9 sharply increased repetition. As temperature approaches 0 you get greedy decoding, where the model always takes its single best guess, which is why this tool treats 0 as a pure argmax. Temperature never adds or removes candidates and never changes their ranking — it only changes how peaked or flat the curve is.
“Temperature decides how flat the curve is. Top-p and top-k decide how much of the tail you are allowed to touch.”
Truncation: top-p (nucleus) and top-k sampling
Temperature alone has a weakness: even at a sensible setting, the long tail of very unlikely tokens still carries a sliver of probability, and occasionally the model picks genuine nonsense. Top-k and top-p truncate the distribution before sampling. Top-k sorts the tokens by probability and keeps only the k highest. Top-p, or nucleus sampling, sorts the same way, walks down the list adding up probability, and stops at the smallest set whose total reaches p. Holtzman and colleagues proposed it because a fixed k misjudges both kinds of step: on a flat distribution a small k cuts off reasonable words, and on a peaked one a large k lets in junk. On a confident step top-p may keep one or two tokens; on an uncertain one it keeps many. After the cut, the kept tokens are renormalised so their probabilities sum back to 100%.
The order is not standard
Runtimes chain these steps differently, and the order changes which tokens survive. Hugging Face transformers applies temperature first, then top-k, then top-p, and its top-p step measures probability over whatever top-k left. llama.cpp's default chain runs top-k and top-p first, on the untempered distribution, and applies temperature last. Try temperature 2 and top-p 0.5 on the example and switch the order: the cut is measured on a different curve. OpenAI and Mistral, for their part, recommend changing temperature or top-p but not both, while llama.cpp ships all three active by default (top-k 40, top-p 0.95, temperature 0.8).
Which APIs still accept these settings
Read from each vendor's own API reference on 3 November 2026. The dials are being withdrawn on several hosted models, so check the current reference before relying on one.
| API | Temperature | Top-p | Top-k |
|---|---|---|---|
| OpenAI (Chat Completions, Responses) | 0 to 2 | 0 to 1 | Not offered |
| Google Gemini API (generateContent) | 0.0 to 2.0; ignored on Gemini 3.6 Flash and newer | Ignored on Gemini 3.6 Flash and newer | Only on models that list it; ignored on Gemini 3.6 Flash and newer |
| Anthropic Messages API | Removed in API version 2026-10-21: sending any of the three returns an error. On earlier versions, Claude 4.7 and later models reject non-default values. | ||
| Mistral (chat completions) | Recommends 0.0 to 0.7; default varies by model | Offered | Offered |
| Hugging Face transformers | Must be above 0; default 1.0 | Default 1.0 (off) | Default 50; 0 = off |
| llama.cpp | Default 0.80; 0 or below = greedy | Default 0.95; 1.0 = off | Default 40; 0 = off |
The creativity-versus-determinism trade-off still holds where the dials exist: higher temperature and a looser top-p for brainstorming or varied copy, lower temperature for code and extraction. Low temperature makes output more consistent, not guaranteed: Anthropic's glossary says that even at temperature 0, identical inputs can produce different outputs across API calls.
Disclosure: RECATOOLS is written with the assistance of Claude, made by Anthropic, whose API is described on this page. Readers should weigh that interest.
What each runtime actually does with the dials
Nucleus sampling comes from Holtzman, Buys, Du, Forbes and Choi, ICLR 2020, which defines the top-p vocabulary as the smallest set of tokens whose probability adds up to at least p.
llama.cpp's defaults are top-k 40, top-p 0.95, temperature 0.80, and its default chain applies temperature after the cuts.
Hugging Face transformers falls back to top-k 50 when a model's generation config sets none, and keeps every token tied with the k-th.
Anthropic's API version 2026-10-21 removed temperature, top_p and top_k; its guidance is to steer Claude with the prompt instead.
Google's reference says Gemini 3.6 Flash and newer silently ignore temperature, topP and topK.
OpenAI's API has no top-k setting at all; it offers temperature (0 to 2) and top_p (0 to 1).
Frequently Asked Questions
- Temperature is a number that divides the model's logits before the softmax. Below 1 it sharpens the distribution toward the most likely token, making output more focused; above 1 it flattens it, giving rare tokens more probability. OpenAI and Google accept 0 to 2; Mistral recommends 0 to 0.7; some current models no longer accept it at all.
- Top-p keeps the smallest set of the most probable tokens whose combined probability reaches at least p, discards the rest and renormalises. Because the set size follows the shape of the distribution, it keeps few tokens when the model is confident and many when it isn't.
- Top-k caps the number of candidate tokens at a fixed count; top-p caps them by combined probability instead, so its window grows and shrinks with the model's confidence. In both Hugging Face and llama.cpp, a top-k of 0 means the cut is off.
- Yes, for which tokens survive. Temperature never changes the ranking, but it changes the probabilities top-p adds up, so a cut measured after temperature (Hugging Face's order) can keep a different number of tokens than one measured before it (llama.cpp's default). Once the survivors are fixed, the final probabilities are the same either way.
- In the maths, temperature 0 is greedy decoding: always the single highest-scoring token. Hosted APIs do not promise identical results, though. Anthropic's glossary says that even at temperature 0 identical inputs might produce different outputs across API calls. Treat low temperature as more consistent, not as a guarantee.
- OpenAI's and Mistral's API references both say they generally recommend altering one or the other, not both. Local runtimes such as llama.cpp run all three by default. If the API you use has dropped the settings, as Anthropic's has, the prompt is the remaining lever.
- The vendors withdrew them. Anthropic's migration guide for API version 2026-10-21 says a request carrying temperature, top_p or top_k returns an error, even at the default value, and Google's reference says Gemini 3.6 Flash and newer ignore all three. The mechanism on this page still describes open models and the APIs that keep the settings.
- The example words and logits are a fixed teaching set, not output from any model. The maths applied to them is the published method. To try real numbers, paste token logprobs from an API that returns them into “Use your own candidates”; the tool renormalises over just those tokens, so it ignores the tail you did not paste.
Method & sources
How it computes
p_i = exp(u_i / t) / Σ exp(u_j / t) (Holtzman et al. 2020, eq. 4); t <= 0 = greedy. Top-k keeps the k most probable (ties with the k-th kept, 0 = off); top-p keeps the smallest set summing to >= p (eq. 2, 1 = off), then renormalises (eq. 3). Order: Hugging Face (temperature, top-k, top-p) or llama.cpp (top-k, top-p, then temperature). Final = softmax(u / t) over the survivors (SamplingKernel).
What this tool implements
- Temperature, top-p and the renormalisation follow Holtzman, Buys, Du, Forbes and Choi (ICLR 2020), equations 2, 3 and 4.
- Step order is shown both ways because implementations differ: Hugging Face transformers appends TemperatureLogitsWarper, then TopKLogitsWarper (when top_k != 0), then TopPLogitsWarper (when top_p < 1), and TopP measures softmax of the scores top-k left; llama.cpp's default sampler chain runs top-k, typical-p, top-p, min-p and xtc before temperature.
- Top-k ties: Hugging Face removes scores below the k-th, so tokens tied with it are kept; the kernel does the same. Top-k 0 and top-p 1 are "off", as in both runtimes.
- API ranges were read from each vendor's reference on 2026-11-03 and are stated as read: OpenAI temperature 0-2, top_p 0-1, no top_k; Google 0.0-2.0, all three ignored on Gemini 3.6 Flash and newer; Anthropic removed all three in API version 2026-10-21; Mistral recommends temperature 0.0-0.7.
- The example logits are a fixed teaching set and the page says so. Reader-supplied logprobs are renormalised over only the tokens given, and the page says so.
- Corrected 2026-11-03: top-k 0 kept nothing (now off); one undisclosed step order (now both, named); "temperature 0 ... deterministic and repeatable" (Anthropic: not fully deterministic across calls); "nucleus sampling became the default" and "the best decoding setups use them together" (unsourced, and contrary to OpenAI and Mistral advice) cut; the example ranked "the" first after "The big brown" (replaced).
Sources
- Holtzman A, Buys J, Du L, Forbes M, Choi Y. The Curious Case of Neural Text Degeneration. ICLR 2020. arXiv:1904.09751: https://arxiv.org/abs/1904.09751
- Hugging Face. transformers generation/logits_process.py (TemperatureLogitsWarper, TopKLogitsWarper, TopPLogitsWarper), read 2026-11-03: https://raw.githubusercontent.com/huggingface/transformers/main/s…
- Hugging Face. transformers generation/utils.py, _get_logits_processor warper order, read 2026-11-03: https://raw.githubusercontent.com/huggingface/transformers/main/s…
- llama.cpp. common/common.h, default sampler order and defaults (top_k 40, top_p 0.95, temp 0.80), read 2026-11-03: https://raw.githubusercontent.com/ggml-org/llama.cpp/master/commo…
- OpenAI. API reference, Create chat completion — temperature and top_p, read 2026-11-03: https://developers.openai.com/api/reference/resources/chat/subres…
- Google AI for Developers. Gemini API generateContent — GenerationConfig temperature, topP, topK, read 2026-11-03: https://ai.google.dev/api/generate-content.md.txt
- Anthropic. API migration guide for version 2026-10-21 — "Sampling parameters are removed", read 2026-11-03: https://platform.claude.com/docs/en/api/migration-2026-10-21.md
- Anthropic. Glossary — Temperature (non-determinism at temperature 0), read 2026-11-03: https://platform.claude.com/docs/en/about-claude/glossary.md
What can make this go out of date
- None at runtime: the teaching logits ship with the page and nothing the reader types is sent anywhere.
Abridged — the full review record for this tool runs longer than the list above.
Pick up where you left off
Stored only in this browser — never sent to our servers.