← Back to articles Blog

Temperature in LLMs: What It Is and How to Set It

Emmanuel Ekunsumi · 5 min read · 2026-07-20

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.

What temperature actually does

When an LLM generates text, it produces a probability distribution over all possible next tokens at each step. Temperature scales this distribution before sampling:

The mathematical intuition

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

Temperature by use case

Use caseRecommended temperatureWhy
Code generation0.0–0.2Correctness matters; creativity doesn't
Data extraction / classification0.0Deterministic, consistent labels
Factual Q&A / RAG0.0–0.3Accuracy over variety
Customer support0.3–0.5Consistent but not robotic
General chat assistant0.7–1.0Natural, varied responses
Creative writing0.8–1.2Variety and surprise valued
Brainstorming / ideation1.0–1.5Maximum variety of ideas

Temperature and token cost

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.

Temperature vs top_p: what's the difference

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.

ParameterHow it worksWhen to use
temperatureScales the entire distributionMost use cases — simpler to reason about
top_pTruncates the distribution at a cumulative probability thresholdWhen 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.

Setting temperature in the API

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.

See how temperature affects your token costs

Tokoscope tracks output tokens per call — so you can measure the real cost difference between temperature settings. Free to start.

Get started free →