← Course | Module 01 - Foundations Lesson 4
MODULE 01

Temperature, Top-p, and Controlling Randomness

🕑 8 min read 🎯 Beginner

What is Temperature?

Temperature controls how creative vs predictable Claude's responses are. It's a number between 0 and 1.

Temperature = 0

Most predictable. Always picks the highest-probability word. Great for factual extraction, classification, code generation.

Temperature = 0.5

Balanced. Some variety while staying coherent. Good default for most production use cases.

Temperature = 1.0

Most creative. More surprising word choices. Use for brainstorming, creative writing, generating diverse options.

Think of it this way

Temperature 0 = Claude always takes the highway (fastest, most direct). Temperature 1 = Claude explores side streets (interesting, sometimes gets lost).

What is Top-p (Nucleus Sampling)?

Top-p controls the pool of words Claude considers. Instead of looking at ALL possible next words, it only considers the top words whose combined probability reaches p.

SettingValueEffectUse Case
Top-p = 0.1Very narrowOnly top ~10% of wordsPrecise, factual output
Top-p = 0.5ModerateTop ~50% of wordsBalanced variety
Top-p = 0.9WideAlmost all wordsCreative, diverse output
Pro tip

Don't change both temperature AND top-p at the same time. Anthropic recommends adjusting temperature and leaving top-p at default. They interact in complex ways.

Practical Settings Guide

TaskTemperatureTop-pWhy
Data extraction01 (default)Need exact, consistent output
Code generation0 - 0.21Correctness over creativity
Customer support bot0.31Consistent but natural
Marketing copy0.71Creative but coherent
Brainstorming1.01Maximum variety
# Python example - setting temperature import anthropic client = anthropic.Anthropic() # Factual extraction - temperature 0 response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=1024, temperature=0, messages=[{"role": "user", "content": "Extract the date from: 'Meeting scheduled for March 15th'"}] ) # Creative writing - temperature 0.8 response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=1024, temperature=0.8, messages=[{"role": "user", "content": "Write a tagline for a coffee shop"}] )

Key Takeaways

🖨 Download PDF 🐦 Share on X
← Back to Course