Anik Das

Jun 04, 2025 • 2 min read

Temperature in Prompt Engineering


In prompt engineering and generative AI (especially in models like GPT, Gemini, Claude, etc.), temperature is a parameter that controls the randomness of the model’s output.

It determines how "creative" or "risky" the AI should be while generating responses.


  • Low temperature (e.g., 0–0.3): More deterministic, focused, and accurate.

  • High temperature (e.g., 0.7–1.0): More diverse, creative, and sometimes chaotic.

  • It affects repeatability, consistency, and novelty.


temperature like a dice-rolling game:

  • Temperature = 0 → Always chooses the most probable answer (like a loaded die that always rolls 6).

  • Temperature = 1 → Rolls a fair die (all options have fair chance).

  • Temperature > 1 → Prefers unexpected or lower-probability choices (unpredictable).


Temperature   Output Behavior
────────────  ──────────────────────────────
0.0           Very repetitive, deterministic
0.3           Accurate with slight variation
0.7           Balanced randomness
1.0           Creative, varied, less accurate
1.5+          Very random, often incoherent

Example: Prompt with Different Temperatures

Prompt:

Write a short story about a dog who learns to fly.


🔹 Temperature = 0.2

Once upon a time, there was a dog named Max who found a pair of wings in the forest. He practiced flying every day and soon became the first flying dog in the world.

📝 Simple, logical, and safe.


🔸 Temperature = 0.7

Max was no ordinary dog. One night, after chasing fireflies, he saw a shooting star and wished for wings. The next morning, wings sprouted from his back, and off he soared above rooftops and trees.

📝 A bit more creative, slightly magical.


🔺 Temperature = 1.2

The dog barked at the moon until gravity gave up. He flapped his ears, summoned clouds, and danced with birds who spoke in riddles.

📝 Very imaginative and poetic, but may not make logical sense.


import openai

response = openai.ChatCompletion.create(
  model="gpt-4",
  messages=[{"role": "user", "content": "Tell me a joke about cats"}],
  temperature=0.9
)

print(response['choices'][0]['message']['content'])

Try changing temperature from 0.2 to 1.2 and observe the tone change.


Goal Recommended Temperature Factual Q&A 0.0 - 0.3 Creative writing (story, ad) 0.7 - 1.0 Brainstorming ideas 0.8 - 1.2 Code generation 0.1 - 0.4 Paraphrasing 0.5 - 0.8


⚠️ Mistakes

  1. Using high temperature for factual tasks → Can lead to hallucinations.

  2. Using very low temperature for creative tasks → Output becomes boring.

  3. Changing temperature without understanding the effect → Inconsistent results.


Best Practices

  • Test prompt at multiple temperatures.

  • Use temperature = 0 for reproducible outputs (especially in production).

  • Pair temperature with top_p for more control over randomness.


Join Anik on Peerlist!

Join amazing folks like Anik and thousands of other people in tech.

Create Profile

Join with Anik’s personal invite link.

0

5

0