Course contentsShow
← AI Engineering
Lesson 137 of 1,8864. Prompt Engineering FoundationsFree lesson

Temperature and Randomness Control

Understanding how temperature affects model creativity and determinism in text generation outputs.

Temperature and Randomness Control

What You'll Learn

You'll learn how the temperature parameter controls whether your AI outputs are creative and varied or consistent and predictable.

Understanding Temperature

When you ask an AI model to generate text, it doesn't just pick the "best" word each time—it samples from a probability distribution. Temperature controls how that sampling works.

Think of temperature like a creativity dial:

  • Low temperature (0.0–0.3): The model becomes focused and deterministic, almost always choosing the most likely next word. This produces consistent, predictable outputs. Great for factual tasks, data extraction, or when you need repeatable results.

  • High temperature (0.8–1.5+): The model becomes more exploratory, giving less likely words a real chance. This produces creative, varied, and sometimes surprising outputs. Ideal for brainstorming, creative writing, or generating diverse alternatives.

How It Works

At temperature 0, the model essentially becomes deterministic—you'll get nearly identical outputs for the same prompt. At temperature 1.0 (the default for many models), you get balanced randomness. Above 1.0, outputs become increasingly unpredictable and potentially incoherent.

Practical Application

Based on what you learned in "Temperature, Top-p, and Generation Parameters," you control this when making API calls:

# Focused, consistent output
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Classify sentiment: The movie was great!"}],
    temperature=0.2
)

# Creative, varied output
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Write a story opening"}],
    temperature=0.9
)

Key Takeaway: Temperature controls the creativity-consistency spectrum: use low values (0–0.3) for deterministic tasks and higher values (0.7–1.2) for creative, varied generation.