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.
// 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?+
Does Claude Code let you set the temperature?+
What is top-p and how does it relate to temperature?+
Master Claude Code in days, not months
37 hands-on lessons from beginner to CI/CD automation. Module 1 is free.
START FREE →