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.
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.
| Setting | Value | Effect | Use Case |
|---|---|---|---|
| Top-p = 0.1 | Very narrow | Only top ~10% of words | Precise, factual output |
| Top-p = 0.5 | Moderate | Top ~50% of words | Balanced variety |
| Top-p = 0.9 | Wide | Almost all words | Creative, diverse output |
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
| Task | Temperature | Top-p | Why |
|---|---|---|---|
| Data extraction | 0 | 1 (default) | Need exact, consistent output |
| Code generation | 0 - 0.2 | 1 | Correctness over creativity |
| Customer support bot | 0.3 | 1 | Consistent but natural |
| Marketing copy | 0.7 | 1 | Creative but coherent |
| Brainstorming | 1.0 | 1 | Maximum 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
- This lesson covered temperature, top-p, and controlling randomness
- Apply these concepts in your own projects before moving on
- Refer back to this lesson when you encounter related challenges