Temperature
APIDefinition
API parameter (0.0-1.0) that controls the randomness of Claude's output. Lower values (0.0) produce deterministic, consistent responses ideal for classification and extraction. Higher values increase creativity and variety for generative tasks.
Example Usage
Use temperature 0 for classification and extraction tasks. Use higher temperature for creative writing and brainstorming.
Why It Matters for the CCA-F Exam
The exam specifically tests whether candidates know that temperature is not a universally available knob. Expect questions where a solution uses `temperature=0` on `claude-fable-5` — the correct answer is that this returns a 400 error and the right mitigation is prompt-based steering, not adjusting the parameter value.
In Depth
What Temperature Does
Temperature is a sampling parameter that scales the probability distribution over the model's next-token predictions before sampling. A temperature of 0.0 makes the model deterministic — it always picks the highest-probability token. Higher values (up to 1.0) flatten the distribution, giving lower-probability tokens a better chance of being selected, which produces more varied, creative, and sometimes surprising output.
The practical axis is consistency vs. diversity:
- Low temperature (0.0 – 0.3): Use for classification, extraction, structured output, and any task where you want the same input to reliably produce the same output.
- High temperature (0.7 – 1.0): Use for brainstorming, creative writing, generating multiple candidate ideas, and tasks where variety is a feature.
Critical Caveat: Temperature Is Removed on Current Models
This is the most exam-critical fact about temperature: temperature (and top_p, top_k) are removed — returning HTTP 400 — on Claude Opus 4.7, Opus 4.8, and Fable 5. On the 4.x family with older models (Opus 4.6, Sonnet 4.6, Haiku 4.5), at most one of temperature / top_p may be set per request.
For current flagship models, you cannot steer randomness via sampling parameters. The recommended approach is to control output style and diversity through prompting — for example, explicitly asking for multiple distinct options, or instructing the model to be concise and factual. The model's own adaptive reasoning depth (controlled by output_config: {effort: ...}) affects how much it "thinks" but is not a direct randomness dial.
Comparison Table
| Parameter | Effect | Status on Fable 5 / Opus 4.8 |
|---|---|---|
temperature | Scales token probabilities | Removed (400) |
top_p | Nucleus sampling cutoff | Removed (400) |
top_k | Limits vocab to top-k tokens | Removed (400) |
| Prompting | Instructs style/diversity | Fully supported |
output_config.effort | Controls reasoning depth/cost | Supported |
Prompt-Based Alternatives
When temperature is unavailable, use prompt engineering to achieve similar effects:
- For more creative output: *"Generate five distinct approaches, each as different as possible from the others."*
- For deterministic output: *"Return only the JSON object. No commentary."*
- For structured variation: few-shot prompting with diverse examples shapes the output register effectively.
See also structured output patterns for cases where you need guaranteed format consistency regardless of model tier.
How It Compares
| Task type | Old approach (temperature) | Current approach (prompting) |
|---|---|---|
| Deterministic classification | temperature: 0 | Strict output schema + explicit instruction |
| Creative brainstorm | temperature: 0.9 | "Generate 5 distinct options, maximise variety" |
| Balanced summarisation | temperature: 0.5 | "Summarise neutrally in 3 bullet points" |
| Reproducible extraction | temperature: 0 | Structured output via output_config.format |
Example
import anthropic
client = anthropic.Anthropic()
# On current models (Fable 5, Opus 4.8), passing temperature raises HTTP 400.
# Use structured output + explicit prompting instead.
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=512,
# temperature=0, # DO NOT: removed on Opus 4.7+ and Fable 5
messages=[{
"role": "user",
"content": "Classify the following support ticket as: billing, technical, or general.\n"
"Return only the category label, nothing else.\n\n"
"Ticket: I can't log into my account after resetting my password."
}]
)
print(response.content[0].text) # Expected: 'technical'
print(response.stop_reason) # Expected: 'end_turn'Frequently Asked Questions
Can I still use temperature on Sonnet 4.6 or Haiku 4.5?
Yes, but only one of `temperature` or `top_p` may be set per request — not both. Setting both simultaneously returns a 400 error even on models where the parameters are nominally supported. The rule is: pick one or neither.
If I need truly deterministic output on a current flagship model, what do I do?
Use `output_config: {format: {type: "json_schema", schema: ..., strict: true}}` to constrain the output shape, and write a precise instruction that leaves the model no ambiguity. Determinism is about reducing the model's degrees of freedom — structured output constraints are the closest equivalent to `temperature: 0` on models where sampling params are removed.