Temperature is a sampling parameter that controls how random or deterministic an LLM's output is. It's one of the most important parameters you can set — and one of the most misunderstood. Temperature directly affects output quality, consistency, and token cost.
When an LLM generates text, it produces a probability distribution over all possible next tokens at each step. Temperature scales this distribution before sampling:
Temperature T divides the logits (raw scores) before the softmax function converts them to probabilities. When T approaches 0, the highest-scoring token gets probability approaching 1. When T is very high, all tokens approach equal probability — effectively random selection.
temperature = 0.0 # Always picks the most likely token temperature = 1.0 # Samples from the raw distribution temperature = 2.0 # Much more random — low-probability tokens become likely
| Use case | Recommended temperature | Why |
|---|---|---|
| Code generation | 0.0–0.2 | Correctness matters; creativity doesn't |
| Data extraction / classification | 0.0 | Deterministic, consistent labels |
| Factual Q&A / RAG | 0.0–0.3 | Accuracy over variety |
| Customer support | 0.3–0.5 | Consistent but not robotic |
| General chat assistant | 0.7–1.0 | Natural, varied responses |
| Creative writing | 0.8–1.2 | Variety and surprise valued |
| Brainstorming / ideation | 1.0–1.5 | Maximum variety of ideas |
Temperature has a subtle but real effect on token cost. Higher temperatures tend to produce longer outputs — because the model is more likely to continue generating when it would otherwise stop. A verbose, high-temperature response uses more output tokens than a focused, low-temperature one for the same prompt.
For high-volume production workloads where output tokens dominate cost, setting temperature to 0.0–0.3 and adding explicit length constraints is one of the cheapest optimizations available. Combined with prompt compression and semantic caching, it compounds into meaningful savings.
Top-p (nucleus sampling) is an alternative to temperature that also controls output randomness. Instead of scaling probabilities, it samples from the smallest set of tokens whose cumulative probability exceeds p.
| Parameter | How it works | When to use |
|---|---|---|
| temperature | Scales the entire distribution | Most use cases — simpler to reason about |
| top_p | Truncates the distribution at a cumulative probability threshold | When you want variety but want to exclude very unlikely tokens |
Most practitioners use temperature and leave top_p at its default (1.0). Adjusting both simultaneously is generally not recommended — they interact in ways that are hard to predict.
import { wrap } from 'tokoscope'
import OpenAI from 'openai'
const client = wrap(new OpenAI(), { apiKey: 'ts_live_...' })
// Deterministic code generation
const code = await client.chat.completions.create({
model: 'gpt-4o',
temperature: 0.0,
messages: [{ role: 'user', content: 'Write a binary search function in Python' }]
})
// Creative writing
const story = await client.chat.completions.create({
model: 'gpt-4o',
temperature: 1.1,
messages: [{ role: 'user', content: 'Write the opening paragraph of a sci-fi story' }]
})
Temperature 0 for tasks with a right answer. Temperature 1+ for tasks where variety is the point. Everything else in between based on how much consistency vs creativity you need.
Tokoscope tracks output tokens per call — so you can measure the real cost difference between temperature settings. Free to start.
Get started free →