Skip to content
NLEN
Illustration: Temperature and Top-p Practically Explained

Temperature and Top-p Practically Explained

By Ivo Donker — compiled with AI assistance (Claude & Gemini)

What you need to know beforehand

This article is part of Module 2 (Using & steering) of the learning path on large language models. To fully understand the mechanism behind steering parameters, it helps to be familiar with how a model processes words. Read in advance the explanation of text tokenization to see how words are converted into numbers. If you want a high-level overview of all settings first, you can consult the guide on sampling parameters.

When a large language model (LLM) generates text, it doesn't do so by coming up with an entire sentence all at once. The model predicts the most likely next element step by step. We call this basic element a token. But how does the model actually choose which token to select from the list of tens of thousands of candidates? This selection process is driven by so-called sampling parameters. The two most important and widely used parameters in this process are temperature and top-p (also known as nucleus sampling).

Many AI model users tweak these knobs by intuition or leave them at their default values. Yet the right configuration of temperature and top-p makes the difference between a crisp, factual report and a creative, engaging storyline. In this in-depth article, we dissect the exact mathematical and logical mechanics of both parameters, show how they interact with each other, and provide practical guidelines for developers and prompt engineers.

The mathematical foundation: from logits to probabilities

To understand what temperature and top-p do, we need to take a step back to the moment the neural network completes its computation. At the end of a forward step (the so-called forward pass), the final layer of the network outputs a list of unnormalized numbers for every token in the vocabulary. We call these raw values logits.

A typical vocabulary for a modern language model contains between 32,000 and 128,000 unique tokens. For each of these tokens, the model generates a specific logit value. A higher logit indicates that, based on its training and the given context, the model considers this token a better fit for the preceding text. However, logits are not yet probabilities; they can be negative numbers, they do not sum to 100%, and their relative scale is arbitrary.

To convert logits into a usable probability distribution, the system applies the softmax function increases. The softmax function takes all logits, raises them to the power of e (making all values positive), and divides each result by the sum of all exponentials. The result is a neat probability distribution: all values lie between 0 and 1, and the sum of all token probabilities is exactly equal to 1 (or 100%).

If we were to simply select the token with the absolute highest probability, we refer to this as greedy decoding. Although greedy decoding works for simple tasks, for longer texts it often leads to dull, repetitive, and unnatural sentences. To make the generated text more human, varied, and customizable, we perform stochastic sampling. And it is precisely during the transformation from logits to probabilities that temperature and top-p come into play.

What exactly does Temperature do to the probability distribution?

The parameter temperature (denoted as T) is a positive number applied directly to the logits before the softmax function is computed. Formally, each logit z_i is divided by the temperature value T:

aangepaste_logit = z_i / T

By dividing the logits by T, the relative distance between the logits changes before they undergo the exponential transformation in the softmax. This has dramatic consequences for the final probability distribution:

Suppose that after the context "The capital of France is", a model considers the following top tokens along with their raw logits:

Token Raw Logit Probability at T = 1.0 Probability at T = 0.2 Probability at T = 1.5
Paris 10.0 88.0% 99.9% 55.0%
a 7.5 7.2% 0.08% 21.0%
not 6.0 1.6% 0.001% 10.0%
London 5.0 0.6% 0.000% 5.1%

The table clearly shows how a low temperature makes the dominant option "Paris" virtually guaranteed, while a high temperature opens the door to incorrect or unusual continuations such as "London".

How does Top-p (Nucleus Sampling) work and what sets it apart?

While temperature is very effective for making text more creative or focused, it has an important drawback. At high temperatures, all tokens in the vocabulary get a higher probability — including tokens that make no sense contextually or are grammatically incorrect. This is where top-p (where the 'p' stands for probability) comes into play.

Top-p sampling, introduced in 2019 by Holtzman et al. under the name nucleus sampling, does not work by altering the probabilities themselves, but by dynamically truncating the list of selectable tokens. The algorithm operates according to the following steps:

  1. The model calculates the probability distribution across all tokens in the vocabulary (after any temperature scaling).
  2. The tokens are sorted in descending order of probability.
  3. The algorithm sums the probabilities of the tokens from the top down (the cumulative probability).
  4. As soon as the cumulative sum reaches or exceeds the configured threshold value p p
  5. , the algorithm truncates the list. All tokens falling below the cutoff are discarded.

The remaining top tokens (the 'nucleus' or core) are re-normalized so that their probabilities sum to 100% again. A token is then randomly selected from this smaller set based on its relative probability. The crucial advantage of top-p is that the number of candidate tokens changes dynamically

depending on the context and the model's uncertainty:

If the model is highly confident (for example, when completing a well-known proverb), the top token might already have a 92% probability. With a top-p setting of 0.9 (90%), the selection pool contains only 1 single token. The model cannot take an incorrect turn.

If, on the other hand, the context is open-ended and creative (for example, "Once upon a time, an old man in a..."), the probability distribution is very flat. The top token might only have a 12% probability. To reach a cumulative probability of 90%, the algorithm may need to include 40 or 50 different tokens in the selection. This gives the model ample room for variety without ever selecting truly bizarre or illogical tokens that fall outside the top 90%.

Temperature versus Top-p: The fundamental operational difference

Think of temperature as an amplifier that adjusts the noise level across the entire system, while top-p acts as an intelligent filter that cuts out background clutter regardless of the volume.

Combining Temperature and Top-p: Order of Execution and Interaction

In virtually all well-known APIs and inference engines (such as OpenAI, Anthropic, vLLM, and Ollama), you can configure both temperature and top-p simultaneously. But how do they work together, and in what order are they applied?

The standard pipeline in almost all software architectures runs in this exact sequence:

  1. Logit Generation: The model outputs the raw logits.
  2. Temperature Scaling: The logits are divided by T. This reshapes the distribution.
  3. Softmax: The scaled logits are converted into absolute probabilities.
  4. Top-p Truncation: The tokens are sorted, and the cumulative probability sum determines which tokens are retained.
  5. Renormalization & Sampling: The remaining candidate tokens are rescaled, and a final selection is sampled.

Because temperature is applied before top-p, your temperature setting directly affects how many tokens fall within the top-p threshold. If you set the temperature very low (e.g., 0.2), the top probabilities become so dominant that the cumulative threshold of top-p = 0.9 is reached after just 1 or 2 tokens. At that point, the top-p setting has virtually no effect.

Conversely, setting the temperature very high (e.g., 1.5) flattens the distribution so much that dozens of additional tokens are needed to reach the top-p threshold. In this scenario, top-p serves as a crucial safety net to prevent the model from selecting complete nonsense.

Real-World Scenarios and Recommended Settings per Use Case

The optimal parameter combination depends on the specific task you want the LLM to perform. Below is an overview of proven configurations for a variety of use cases.

1. Factual Tasks, JSON Output, and Code Generation

When writing source code, parsing data, or answering factual questions, randomness is a disadvantage. A single incorrect character can break a JSON structure or introduce a syntax error in Python.

2. Summarization, Customer Support, and Business Correspondence

When summarizing articles or drafting professional emails, you want the text to sound natural and fluent, yet the content must strictly stay within the bounds of the source material to prevent hallucinations or factual errors.

3. Brainstorming, creative writing, and marketing copy

When you are looking for original angles, slogans, storylines, or surprising metaphors, you want the model to think outside the box.

Use case Recommended Temperature Recommended Top-p Output characteristics
Code & Math 0.0 – 0.1 0.1 – 0.3 Extremely strict, predictable, factual
Data extraction / JSON 0.0 1.0 No variation, follows schema exactly
RAG & Knowledge questions 0.2 – 0.4 0.7 – 0.8 Reliable, sticks to the source
Translations 0.3 – 0.5 0.8 – 0.9 Correct grammar, fluent style
Creative writing 0.8 – 1.1 0.9 – 0.95 Rich vocabulary, surprising, varied

The impact of sampling on reliability, consistency, and determinism

A common misconception among software developers is the idea that setting temperature = 0.0 guarantees 100% identical responses across every API call. Although a temperature of zero makes the sampling process deterministic (since it always selects the highest logit), several other factors come into play within modern LLM infrastructure.

Due to the parallel processing of compute operations on GPUs (floating-point rounding differences across CUDA threads) and techniques such as Mixture-of-Experts (MoE) or speculative decoding, the raw logits can deviate slightly from run to run. If two top tokens have almost identical logits (e.g., 8.000001 versus 8.000002), this minuscule floating-point variation can still cause run A to select one token and run B to pick the other.

For anyone running automated tests or conducting scientific research, understanding how this works is valuable. Want to know exactly how to measure and control the impact of this variation in testing environments? Check out the guide on reproducibility of AI evaluations on the benchmark platform.

In addition, there is a direct interplay with complex prompt techniques. When you ask a model to reason step-by-step, the chosen sampling parameters carry significant weight. If the temperature is set too high, a single misstep in reasoning can derail the entire process. Want to learn more about how models think step-by-step? Read the in-depth article on applying chain-of-thought reasoning.

Common Mistakes and Myths Surrounding Temperature and Top-p

In practice, developers and prompt engineers regularly make incorrect assumptions about how these controls work. Here, the three most persistent myths are debunked:

Myth 1: "You should always adjust both parameters at the same time"

Much official documentation (including OpenAI's) advises adjusting **either** temperature **or** top-p, but not drastically altering both simultaneously. The reason for this is clarity. Because both parameters influence the stochastic variation of the output, simultaneously modifying, for example, temperature = 1.4 and top-p = 0.3 makes it very difficult to determine which parameter is responsible for a change in the output. The best approach is to leave top-p at 0.9 or 1.0 and steer using temperature, or leave temperature at 1.0 and adjust top-p exclusively.

Myth 2: "A high temperature makes the model smarter"

A higher temperature does not grant the model access to additional knowledge or higher intelligence. It simply forces the model to explore less probable paths within the learned network. For logic puzzles or mathematical problems, a high temperature almost always leads to degraded performance, as the correct reasoning steps usually have the highest probability.

Myth 3: "Top-k is the same as Top-p"

In addition to top-p, there is also a parameter called top-k. Whereas top-p looks at the *cumulative probability* (a variable number of tokens), top-k strictly selects a *fixed number* of the top tokens (for instance, always the top 40 tokens). Top-k does not take the model's certainty into account: even with absolute certainty, 40 tokens remain in contention, increasing the risk of noise. Because of this, top-p is superior to top-k in nearly all modern applications.

Conclusion: Choosing the Right Balance

Temperature and top-p are powerful instruments for fine-tuning a language model's behavior to your specific use case. Remember the core rule: for factuality, structure, and automation, choose a low temperature (0.0 – 0.3) or a tight top-p. For creativity, human-like variation, and exploration, dial the temperature up toward 0.8 to 1.0.

By understanding how logits are transformed via the softmax function and how the candidate pool is truncated, you can make well-informed choices instead of gambling on arbitrary settings.

Next steps

Now that you know exactly how to guide a model's selection process, you can dive deeper into how models handle long-form text and memory. Read the guide on context engineering and prompt limitations, or discover how small models are trained to produce structured outputs in the article on model distillation in LLMs.