Advanced

Temperature: control vs chaos

Temperature is a knob that decides how much freedom a model has in every word it picks. Turn it low and the answer is focused and predictable. Turn it up and it starts taking chances, some of them good, some of them wrong. The value doesn't change the prompt; it changes the mood.

What you will see

summarize facts: temperature 0.2 brainstorm ideas: temperature 0.9

About 2 min of reading

Try It

Temperature is the knob that decides how much freedom the model has in every word it picks. I run the same prompt twice at a low value to see a stable answer, compare it to a high value for variety, and match each setting to what the task needs.

1

I call a fake model at low temperature twice and print that both replies are the same, showing that low heat keeps answers consistent and safe for anything that must be right.

def generate(prompt, temperature): high = ["Paris", "Lyon", "Marseille"] if temperature <= 0.2: return "Paris" idx = int(temperature * 10) % len(high) return high[idx] print("Low temp 0.2:", generate("The capital of France is", 0.2)) print("Low temp again:", generate("The capital of France is", 0.2)) print("High temp 0.9:", generate("The capital of France is", 0.9))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Low temp 0.2: Paris Low temp again: Paris High temp 0.9: Paris
Temperature does not change the prompt; it changes the mood. Keep it low when one stable answer matters.
2

I compare what a focused model says to what a creative one says for the same prompt, printing each so the difference in variety is clear at a glance.

def pick_most_likely(): return "Paris" def pick_creative(): return "might be Paris, or Lyon" print("Low temperature:", pick_most_likely()) print("High temperature:", pick_creative())

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Low temperature: Paris High temperature: might be Paris, or Lyon
High temperature is great for stories and useless for an answer key. Pick the setting to match the task, not the model.
3

I list two tasks with their best temperature so the mapping from job to knob is explicit before I ship anything.

rules = { "summarize facts": 0.2, "brainstorm ideas": 0.9, } for task, temp in rules.items(): print(f"{task}: temperature {temp}")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
summarize facts: temperature 0.2 brainstorm ideas: temperature 0.9
Getting temperature wrong is a quiet waste of good output. Set it once for the job, not by chance.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Pick a temperature, then the model samples words with more or less randomness depending on that setting.
1 Set temperature
↓
2 Model scores next words
↓
3 Sample with more or less randomness
↓
4 Produce the answer
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

def generate(prompt, temperature): sets nothing on screen
high = ["Paris", "Lyon", "Marseille"] sets nothing on screen
if temperature <= 0.2: sets nothing on screen
return "Paris" sets nothing on screen
idx = int(temperature * 10) % len(high) sets nothing on screen
return high[idx] sets nothing on screen
print("Low temp 0.2:", generate("The capital of France is", 0.2)) Low temp 0.2: Paris
print("Low temp again:", generate("The capital of France is", 0.2)) Low temp again: Paris
print("High temp 0.9:", generate("The capital of France is", 0.9)) High temp 0.9: Paris
Why this works
Low temperature

A low temperature makes the model reach for the most likely word every time. Answers are safe, consistent, and repeatable, which is exactly what you want for facts, math, and code.

High temperature

A high temperature lets the model reach for less likely words. Answers get more creative and varied, and correspondingly more likely to drift into nonsense. Great for stories, useless for an answer key.

Examples

Turning the dial

Low temperature (0.2): one clear, consistent answer.
High temperature (0.9): many different, surprising answers.

Low is for work that needs to be right, math, facts, code. High is for brainstorming, where variety is the point and a few bad ideas are the price of a good one.

Same words, different moods

Temperature 0.1: "The capital of France is Paris."
Temperature 0.9: "The capital of France might be Paris, or Lyon."

Higher temperature adds randomness to the guessing, so the same prompt can produce a different answer every time. That's useful for ideation and terrible when the answer has to be stable.

Matching the knob to the task

{ "task": "summarize facts", "temperature": 0.2 }
{ "task": "brainstorm ideas", "temperature": 0.9 }

Low for tasks that want one right answer, high when you want options. The best settings aren't about the model, they're about what the task actually needs. Getting this wrong is usually a quiet waste of good output.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← The context window
Previous Lesson
The context window
Next Lesson
Why models make mistakes
Why models make mistakes →