Skip to content
AgenticAIWiki
  1. Learn
  2. Part 1
  3. 2. What an LLM is
  4. 2.4

Lesson 2.4 · 4 of 6 · 5 min read · Reviewed 24 Nov 2026

Why the same prompt can give different answers

By the end, you can

  • Explain what temperature changes and what it does not.
  • Choose a sensible temperature for extraction and for brainstorming.

Send the same prompt twice and you may get two different answers. That is not a bug. Step 3 of the generation loop samples from a probability distribution, so a less likely tokenToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. is sometimes picked, and every later token is conditioned on that choice.

Temperature

TemperatureTemperature: A sampling setting that controls randomness: lower values make output more predictable, higher values more varied. rescales the distribution before samplingSampling: Choosing the next token from the model’s probability distribution, rather than always taking the most likely one.. Try it in the diagram above:

  • Low (towards 0): the top token takes almost all the probability. Output becomes focused and repeatable.
  • Around 1: the model’s natural distribution.
  • High: unlikely tokens get a real chance. Output gets more varied and, past a point, less coherent.

Low temperature is a good default for extraction, classification and code. Higher values can help brainstorming. Even at low temperature, small differences in runs are possible, so never assume perfect repeatability.

Temperature is a general model concept, and many model APIs expose it. Current Claude models don’t accept sampling settings, so on Claude you get consistency from the design points below and the effort setting (13.6).

Temperature is not accuracy

A low temperature makes the model consistently pick its favourite answer. If that favourite is wrong, you get the same wrong answer every time. Accuracy comes from the prompt, the context you provide and the checks you build around the model.

Designing for variation

  • Give explicit criteriaExplicit criteria: Concrete, checkable rules in a prompt that define what a good answer is, instead of vague words like "good" or "concise". and examples so the likely answer is the right one.
  • Ask for structured outputStructured outputs: Making a model return data that matches a defined schema, such as JSON with required fields and types. and validate it (chapter 14).
  • Measure with an evalEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria. set rather than trusting one run (chapter 7).

Key points

  • Sampling from a distribution makes output vary between runs.
  • Lower temperature makes output more focused and repeatable, not more correct.
  • Design for variation with clear instructions, examples, validation and evals.
Hands-on labRuns in the Playground

Sample one prompt at three temperatures

See how temperature changes variety, and confirm that low temperature means consistent, not correct.

You will have: A script that samples 20 runs at three temperatures and reports distinct answers and the top token's chance.

Steps

  1. Write probs(temperature) with softmax over logit / temperature, and argmax at 0.
  2. Sample 20 runs at each temperature with the seeded random.Random(7).
  3. Count the distinct tokens in each set of runs.
  4. Note what happens to the top token's chance as temperature rises.

Done when

  • T=0 gives 1 distinct answer in 20 runs.
  • Higher temperatures give more distinct answers.
  • The top token's chance falls as temperature rises.

Open in the Playground No API key needed.

Starter code (Python)
import math
import random

TOKENS = [" mat", " floor", " sofa", " bed", " moon"]
LOGITS = [4.1, 3.2, 2.7, 2.3, 0.4]

def probs(temperature):
    # TODO: at temperature 0 give all probability to the largest logit
    # TODO: otherwise return softmax(logit / temperature)
    return [1 / len(LOGITS)] * len(LOGITS)

rng = random.Random(7)
for t in [0, 0.7, 1.5]:
    p = probs(t)
    # TODO: sample 20 runs with rng.choices(TOKENS, weights=p) and count distinct tokens
    print(f"T={t}: top token chance {p[0]:.0%}")
Show a solution
import math
import random

TOKENS = [" mat", " floor", " sofa", " bed", " moon"]
LOGITS = [4.1, 3.2, 2.7, 2.3, 0.4]

def probs(temperature):
    if temperature == 0:
        best = LOGITS.index(max(LOGITS))
        return [1.0 if i == best else 0.0 for i in range(len(LOGITS))]
    m = max(LOGITS)
    e = [math.exp((l - m) / temperature) for l in LOGITS]
    return [x / sum(e) for x in e]

rng = random.Random(7)
for t in [0, 0.7, 1.5]:
    p = probs(t)
    runs = [rng.choices(TOKENS, weights=p)[0] for _ in range(20)]
    print(f"T={t}: {len(set(runs))} distinct in 20 runs, top token chance {p[0]:.0%}")
print("Low temperature is consistent, not correct: the top token wins every time at T=0.")
Quick checkYour extraction job gives a different wrong answer on each run. What is the most useful first change?
How sure are you?
Pick an answer and how sure you are