Skip to content

Home / Glossary / Temperature

Definition

Temperature

Temperature is a parameter in large language models that controls the randomness of the output. A temperature of 0 makes the model deterministic, always choosing the most probable next token. Higher temperatures (up to 1.0 or 2.0) increase randomness, making less probable tokens more likely to be selected. For coding tasks, lower temperatures generally produce more reliable, consistent code.

How temperature affects output

When an LLM generates text, it calculates a probability distribution over all possible next tokens. Temperature scales these probabilities before sampling. At temperature 0, the model always picks the highest-probability token (greedy decoding). At temperature 1.0, it samples according to the original probabilities. Above 1.0, the distribution flattens further, making rare tokens more likely. Think of it as a creativity dial: low temperature is precise and predictable; high temperature is creative and varied.

Temperature for code generation

For code, lower temperatures (0-0.3) are usually better. Code has strict syntax rules and typically one correct implementation for a given specification. Higher temperatures can introduce syntax errors, incorrect API calls, or unusual patterns. However, slightly elevated temperatures (0.3-0.5) can be useful for brainstorming alternative implementations or creative problem-solving. Most AI coding tools set temperature automatically—Claude Code, for instance, uses optimized settings for different types of coding tasks.

typescript
// Temperature comparison (conceptual)
// Prompt: "Write a function to reverse a string"

// Temperature 0 — always produces the canonical answer:
function reverse(s: string): string {
  return s.split("").reverse().join("");
}

// Temperature 0.8 — might produce creative alternatives:
function reverse(s: string): string {
  let result = "";
  for (let i = s.length - 1; i >= 0; i--) {
    result += s[i];
  }
  return result;
}

You rarely need to adjust temperature when using AI coding tools like Claude Code—they handle it internally. If you are building your own AI-powered tool, start with temperature 0 for code generation and only increase it if you want more variation in outputs.

What temperature should I use for code generation?+
For reliable code generation, use 0 to 0.2. This produces deterministic, predictable output. For brainstorming or generating multiple alternative implementations, 0.3 to 0.5 works well. Avoid temperatures above 0.7 for code—the randomness introduces bugs.
Does Claude Code let you set the temperature?+
Claude Code manages temperature internally and does not expose it as a user setting. The tool uses optimized temperature values for different types of tasks. If you need to control temperature directly, use the Anthropic API.
What is top-p and how does it relate to temperature?+
Top-p (nucleus sampling) is an alternative to temperature for controlling randomness. Instead of scaling probabilities, top-p limits sampling to the smallest set of tokens whose cumulative probability exceeds p. Both control output diversity but work differently. They can be used together, though most practitioners adjust one and keep the other at default.

Related terms

Large Language Model (LLM)TokenFine-TuningCode Generation

Master Claude Code in days, not months

37 hands-on lessons from beginner to CI/CD automation. Module 1 is free.

START FREE →
← ALL TERMS