Enter the same prompt twice and an AI assistant may produce two different answers. That variation is usually not a memory failure or a change of mind. It comes from how the system chooses among several plausible next tokens: instead of always taking the single most likely option, it often picks one at random in proportion to how likely each option is.
A language model calculates a probability distribution over possible continuations [1, 2]. The generation settings decide whether the system always takes the highest-scoring option or samples—makes a weighted random pick—from a controlled set of possibilities. Temperature, top-k and top-p are the settings that shape that pick. They affect variation—not whether the answer is true.
This guide builds on two earlier ones. Why Does AI Predict the Next Token—and How Does That Become an Answer? showed that a response is built one token at a time. How Does AI Follow Instructions? Instruction Tuning, System Prompts, and Rules showed how training, system instructions and your prompt steer what the model is trying to do. Even when all of those stay the same, the final choice of each token can still vary.
TL;DR
- At each step, every candidate next token gets a probability [1].
- Sampling makes a weighted random choice among possibilities instead of always choosing the top token.
- Lower temperature concentrates probability on leading choices; higher temperature spreads it more broadly [1].
- Top-k keeps a fixed number of candidates. Top-p keeps the smallest group reaching a cumulative probability threshold [3, 1].
- Small early differences can send the rest of a response down a different path.
- Repeatability and factual accuracy are separate. A consistent answer can still be wrong [4].
From Scores to Probabilities
Suppose the prompt ends with: The meeting starts at…
For illustration, the model might assign:
| Possible next token | Illustrative rank |
|---|---|
| 9 | Highest |
| noon | Second |
| the | Third |
| tomorrow | Fourth |
| blue | Fifth |
This ranking is hypothetical. A real model has a much larger vocabulary, assigns a probability to each option and recalculates the distribution after every selected token.
Think of sampling as drawing from a bag of tickets in which likelier tokens hold more tickets. 9 would usually be drawn, but noon would sometimes win, and a very unlikely token such as blue would rarely appear.
The highest-probability token is not automatically correct. The prompt may omit the actual meeting time. The distribution reflects learned patterns and context, not access to ground truth.
Four Ways to Choose the Next Token
| Method | What it does | Typical tradeoff |
|---|---|---|
| Greedy decoding | Always chooses the highest-probability token | Repeatable, but can be rigid or repetitive |
| Temperature sampling | Rescales how peaked or flat the distribution is | Controls variety across candidates |
| Top-k sampling | Samples only from the k highest-scoring tokens | Removes the long tail with a fixed cutoff |
| Top-p sampling | Samples from the smallest set reaching probability mass p | Adapts the candidate count to the distribution |
Research on neural text generation found that maximization-focused decoding could produce bland or repetitive text, while unconstrained sampling could drift into incoherence [3]. Nucleus sampling was proposed to sample from a dynamic high-probability set while cutting off the unreliable tail [3].
No method is universally best. A poetry prompt and a structured extraction task have different goals.
What Temperature Changes
Temperature rescales the token scores before they become a probability distribution.
- Lower temperature: makes the leading choices more dominant.
- Higher temperature: makes alternatives relatively more likely.
- Very low or deterministic selection: can improve repeatability when the surrounding system is otherwise stable.
Google’s technical glossary describes higher temperature as producing more randomness and lower temperature as producing less [1]. The word randomness does not mean every token is equally likely. Sampling remains weighted by the model’s distribution.
Temperature also does not make the model more knowledgeable. Lowering it can make the system choose the same unsupported answer more consistently.
Top-k and Top-p
Top-k draws from a fixed number of candidates. If k is 20, only the 20 highest-scoring tokens remain eligible, regardless of how probability is distributed.
Top-p, also called nucleus sampling, uses a cumulative threshold. If p is 0.9, the system keeps the smallest set whose probabilities add to at least 90 percent [3, 1]. A confident distribution may need only a few tokens; an uncertain one may keep many.
Systems may combine temperature with top-k or top-p. Products may also impose hidden defaults and other decoding rules, so a visible temperature control is rarely the whole story.
Why One Difference Becomes a Different Answer
Generation is sequential. If one run selects noon and another selects 9, every later token is conditioned on a different sequence.
The branches compound:
- One token differs.
- The new sequence produces a different next-token distribution.
- Another choice differs.
- Sentence structure, examples or conclusions diverge.
This is why two responses can begin similarly and end with different recommendations. The token-by-token loop is the one described in the next-token prediction guide; sampling only changes which branch each step takes.
Repeatability Is Not Correctness
Deterministic output is useful for testing and structured workflows. It lets developers compare changes without as much sampling variation.
But a model can assign its highest probability to a false continuation. NIST uses confabulation for confidently stated erroneous content [4]. Choosing the same continuation every time does not verify it.
Reproducibility can also be affected by model updates, hidden system instructions, retrieved information, tool results and implementation details. Temperature is only one source of variation.
Which Settings Fit Which Task?
Use these as tendencies, not guarantees:
- Extraction or classification: favor lower variation and strict output validation.
- Factual explanation: use moderate or low variation, retrieval and source checking.
- Brainstorming: allow more alternatives, then evaluate them separately.
- Creative writing: higher variety may be useful if coherence remains acceptable.
- Code: repeatability can help, but execution and tests matter more than temperature.
The setting should follow the task. A single “creative” or “accurate” slider cannot replace evaluation. Practical advice on adjusting settings and prompts for real work belongs to BuildAIQ’s Prompting + Working With AI chapter.
From Sampling to Reasoning Models
Sampling explains variation within one pass through the model: each token is chosen once, and the response follows whichever branch those choices create. Some systems go further and spend extra computation while answering, for example by working through intermediate steps before giving a final response. The next guide, How Do Reasoning Models Work? What Changes When AI Gets More Time to Think, explains what that extra work changes and what it does not.
Practical Takeaways
- Expect variation when sampling is enabled.
- Lower temperature for repeatability, not as a truth guarantee.
- Use top-k or top-p to restrict unlikely candidates.
- Test complete settings, prompts and model versions together.
- Validate structured outputs rather than trusting formatting probability.
- Verify consequential facts regardless of decoding settings.
Sources
- Machine Learning Glossary. Google for Developers, accessed 2026-11-08.
- Language Models are Few-Shot Learners. arXiv, 2020-05-28, accessed 2026-11-08.
- The Curious Case of Neural Text Degeneration. arXiv, 2019-04-22, accessed 2026-11-08.
- Artificial Intelligence Risk Management Framework: Generative Artificial Intelligence Profile. NIST, 2024-07-26, accessed 2026-11-08.
