AI API Temperature & Top-p Guide

Control creativity, randomness, and determinism. Know exactly which settings to use for coding, writing, analysis, and more.

What Is Temperature?

Temperature controls how "random" or "creative" the model's output is. It ranges from 0 to 2 (most APIs default to 0.7 or 1.0).

0.0
2.0
Deterministic Balanced Creative Chaotic Random
Key rule: Lower temperature = more predictable, repeatable output. Higher temperature = more diverse, surprising output. For production systems, always test at temperature=0 first.

Recommended Temperature by Use Case

Code Generation

0.0 - 0.2

Deterministic output. Same prompt always produces same code. Essential for unit tests and CI/CD.

Data Extraction

0.0 - 0.3

Structured, consistent results. You want the same entities extracted from similar inputs every time.

Technical Writing

0.2 - 0.5

Factual and precise with minor variation in phrasing. Good for documentation and reports.

Chat & Conversations

0.7 - 1.0

Natural, varied responses. The default range for most chat applications. Balanced and engaging.

Creative Writing

1.0 - 1.4

Stories, poems, marketing copy. More surprising word choices and narrative directions.

Brainstorming

1.2 - 1.8

Wild, unconventional ideas. High diversity for ideation sessions. Filter manually afterward.

What Is Top-p (Nucleus Sampling)?

Top-p is an alternative to temperature. Instead of scaling all token probabilities, it only considers tokens whose cumulative probability reaches a threshold p.

Recommendation: Use temperature OR top-p, not both. The OpenAI docs suggest setting the one you're not using to 1.0. In practice, most developers just use temperature and leave top_p at 1.0.

Temperature vs Top-p: When to Use Which

ScenarioUseWhy
Need exact same output every timeTemperature = 0Greedy decoding, no randomness
Need some variety but boundedTop-p (0.7-0.95)Filters out truly bizarre tokens
Need full creative rangeTemperature (1.0+)Scales all probabilities uniformly
Testing prompt consistencyTemperature = 0Same prompt = identical output

Code Example: Temperature in Practice

from openai import OpenAI

client = OpenAI(api_key="YOUR_KEY", base_url="https://tokenease.io/v1")

# Deterministic code generation
code = client.chat.completions.create(
    model="deepseek-tc",
    messages=[{"role": "user", "content": "Write a Python function to reverse a string"}],
    temperature=0,
    max_tokens=200
)

# Creative marketing copy
copy = client.chat.completions.create(
    model="k3",
    messages=[{"role": "user", "content": "Write a catchy headline for a running shoe"}],
    temperature=1.2,
    max_tokens=50
)

Common Mistakes

Mistake #1: Using temperature=0.7 for everything. Code generation at 0.7 produces inconsistent, buggy output. Always match temperature to the task.
Mistake #2: Setting both temperature and top-p to non-default values. They interact in complex ways. Pick one, set the other to 1.0.
Mistake #3: Expecting temperature=0 to be 100% deterministic across model versions. A model update can change output even at T=0.
Mistake #4: Not testing edge cases. A prompt that works at T=0.5 might hallucinate at T=1.2. Test your full temperature range.

Other Sampling Parameters

max_tokens

Hard limit on output length. Always set this to prevent runaway generation. For chat, 1000-2000 is usually sufficient. For long-form writing, you may need 4000+.

presence_penalty & frequency_penalty

Discourage repetition. Presence penalty penalizes tokens that have appeared at all. Frequency penalty penalizes tokens proportionally to how often they've appeared. Range: -2.0 to 2.0. Default: 0. Use 0.1-0.3 to reduce repetition without breaking coherence.

seed

For reproducibility. Setting a seed with temperature=0 (or low) gives more consistent results across calls. Not all providers support this.

Experiment with All Models

One API key. Six models. Instant switching between temperature settings.

Get Free API Key