AI Learning Hub
beginner

Sampling: temperature, top-p, top-k

The dials that control how creative — or how predictable — the model is.

For "The capital of France is ___" the model has rated every possible next word.Temperature reshapes those probabilities before the dice roll.Cold die — temperature 0Paris99%Lyon0.5%the0.3%Berlin0.1%banana0.1%↑ always lands on "Paris"Hot die — temperature 1.2Paris32%Lyon22%the18%Berlin16%banana12%↑ "Paris" most likely, but "banana" can win tooLow temp = code, math, JSON. High temp = poetry, brainstorming, names.
Temperature is a knob on the same probability distribution — not a different model.

What's actually happening

KnobWhat it doesTypical values
temperatureSharpness of the distribution0 (deterministic) → 1 (natural) → >1 (chaotic)
top_pKeep the smallest set of words whose probabilities sum to ≥ p, sample from those0.9–0.95 is the default sweet spot
top_kKeep only the k highest-probability wordsLess common; mostly replaced by top-p

Two production rules of thumb:

  • Most teams ship at temperature=0 or 0.2 and leave top-p alone. Easier to debug, fewer surprises.
  • Crank temperature only when variety is the product (creative writing, prompt brainstorming) — not because you "want better answers."

Prompt: "My favourite colour is". These are the (illustrative) probabilities the model assigns to the next token at different sampling settings.

blue
45.4%
red
25.7%
green
16.7%
purple
5.3%
yellow
3.5%
orange
1.7%
pink
0.8%
black
0.5%
white
0.3%
fuchsia
0.1%

Green = inside the top-p nucleus (eligible to be sampled). Grey = excluded by top-p. Lower temperature sharpens the distribution; higher temperature flattens it.

Defaults that just work

TaskTemperature
Code generation, JSON extraction0
Summarisation, classification0–0.3
Default chatbot reply0.5–0.8
Creative writing, brainstorming0.8–1.2

Where it shows up in real life

Under the hood (optional)

The sampling knobs are just request parameters. ~8 lines of Python. Skip if you don't code.

Show the example code (Python, ~8 lines)click to expand
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=512,
    temperature=0.7,           # 0 for code, 1.0+ for creative
    top_p=0.95,                # nucleus sampling
    stop_sequences=["\n\n##"], # stop at a heading
    messages=[{"role": "user", "content": "Write a haiku about debugging."}],
)

Try it yourself (no coding, ~5 minutes)

Most chat products hide temperature; playgrounds expose it. Free option:

  1. console.anthropic.com → Workbench (free signup). Temperature slider on the right.
  2. Test 1: temp = 0. "Write a one-sentence opening line for a fantasy novel." Run 3×.
  3. Test 2: temp = 1. Same prompt. Run 3×.
  4. Test 3: temp = 0, "What's 247 × 89?". Run twice.

Test 1: nearly identical. Test 2: wildly different. Test 3: same answer twice (right or wrong, but consistent). You just felt why deterministic tasks want low temperature.

Check your understanding

  1. 1. What does temperature=0 do?
  2. 2. Building a JSON extractor — what temperature first?
  3. 3. What does top-p = 0.9 mean?

Found this useful? Share it with someone learning AI.

Further reading

Related lessons in this track