Temperature

Suppose we are building a brainstorming machine. We ask Claude for a one-sentence movie idea, and it delivers: “A time-travelling archaeologist must prevent ancient artefacts from being stolen.”

Not bad. Let us brainstorm harder. We run it again. Time-travelling archaeologist. Again. The same archaeologist, the same artefacts, run after run. Claude has exactly one movie and is going to pitch it until one of us dies.

What understand what is going on, we must first learn how Claude chooses its words at all.

How Claude Generates Text #

When we send Claude a prompt like “What do you think?”, three things happen.

For our prompt, Claude might assign “about” a 30% probability, “would” 20%, “of” 10%, and so on down a long tail of candidates. It selects one token, appends it, and repeats the entire process. Predict, sample, append, until a full response exists.

That sampling step is where the dial sits.

What Temperature Does #

Temperature is a decimal between 0 and 1 that reshapes those selection probabilities before sampling. Think of it as the creativity dial on Claude’s responses.

At low temperatures, near 0, Claude becomes very deterministic. It almost always picks the highest-probability token. At temperature 0.0, “about” gets 100% of the probability.

At high temperatures, near 1, the probability spreads more evenly across the candidates. Lower-ranked tokens get a real chance of being chosen, and the output becomes more varied and creative.

We were brainstorming at temperature 0.0, so every run had the same maximum-probability path. Time-travelling archaeologist.

Choosing the Right Temperature #

Low temperatures, 0.0 to 0.3, suit tasks where you want the same answer twice: factual responses, coding assistance, data extraction, content moderation.

Medium temperatures, 0.4 to 0.7, suit most general work: summarisation, educational content, problem-solving, and creative writing that must stay within constraints.

High temperatures, 0.8 to 1.0, are for when variety is the point: brainstorming, creative writing, marketing content, joke generation.

Adding Temperature to Our Chat Function #

Our chat function needs one new parameter, defaulting to 1.0, passed straight through to the create call.

async function chat(
  messages: Anthropic.MessageParam[],
  system?: string,
  temperature = 1.0,
): Promise<string> {
  const message = await client.messages.create({
    model,
    max_tokens: 1000,
    messages,
    temperature,
    ...(system ? { system } : {}),
  });

  const block = message.content[0];
  return block?.type === "text" ? block.text : "";
}

That is the entire change: accept temperature, include it in the request. The type checker asks nothing further.

Testing the Dial #

Back to the movie pitches, once at each extreme.

// Low temperature — more predictable
const answer = await chat(messages, undefined, 0.0);

// High temperature — more creative
const creative = await chat(messages, undefined, 1.0);

At 0.0, the archaeologist returns. At 1.0, the pitches scatter. New themes, new characters, new plots each time. Brainstorming begins.

Keep in mind #

Temperature does not guarantee different outputs. It only changes the probability of getting them; even at high temperatures, Claude may occasionally produce similar responses. The dial just loads the dice.

The skill here is matching the dial to the task. Consistent, factual answers want low temperature. If brainstorming, go high. And for everything else, the middle works well.

 
0
Kudos
 
0
Kudos

Now read this

typescript

Variables # let apples = 5; let speed: string = 'fast'; let hasName: boolean = true; let nothingMuch: null = null; let nothing: undefined = undefined; Built in objects # let now: Date = new Date(); Arrays # let colors: string[] = ['red',... Continue →