0
Generative AI Foundations
Phase 1Module 10 of 15

Temperature

LLMs output probability distributions over possible next tokens. Temperature scales these probabilities before sampling. Engineers need control over creativity vs consistency depending on the task — code generation needs precision, brainstorming needs variety.

Temperature is like a creativity dial on a thermostat. Setting 0 is a precise recipe follower (same dish every time). Setting 1 is an experimental chef who improvises with different ingredients each time.

Visual Workflows

Start here — study each diagram, then use + / to zoom if needed.

100%
Loading diagram...

Scroll inside the frame · use + / − to zoom

Key Takeaways

  • 1.Temperature is a sampling parameter that controls randomness in LLM output — low values produce deterministic, focused responses; high values produce creative, varied ones. At temperature 0, the model always picks the highest-probability token (greedy decoding) — deterministic and reproducible.
  • 2.At temperature 1, probabilities are used as-is. Above 1, distributions flatten — more random, creative, but potentially incoherent.
  • 3.Below 1, distributions sharpen — more focused. Typical settings: 0 for factual/code tasks, 0.3-0.7 for balanced conversation, 0.8-1.0 for creative writing.
  • 4.Temperature affects only generation, not understanding.

Real Example

Scenario

A code generation endpoint uses temperature 0 for consistent, correct syntax. A marketing copy generator uses temperature 0.8 for varied, creative headlines. Same model, different behavior via one parameter.

What you would do

In Generative AI Foundations, apply Temperature to this scenario: A code generation endpoint uses temperature 0 for consistent, correct syntax. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

Practice Task

Open the Code Walkthrough below and run it locally. Change one parameter related to Temperature (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

Code Walkthrough

Highlighted lines show where Temperature happens in the code.

Temperature
1from openai import OpenAI  # import dependencies2
3client = OpenAI()  # create API client4
5prompt = "Write a one-line tagline for a coffee shop"6
7for temp in [0, 0.5, 1.0]:8    response = client.chat.completions.create(  # call the API9        model="gpt-4o-mini",10        messages=[{"role": "user", "content": prompt}],11        temperature=temp,  # 0 = deterministic, higher = more creative/random12        n=3,  # generate 3 variants13    )14    print(f"Temperature {temp}:")  # show output for debugging15    for choice in response.choices:16        print(f"  - {choice.message.content}")  # show output for debugging

Cheat Sheet

Quick recap

quick ref
  • temperature=0 → deterministic
  • temperature=1 → default randomness
  • Code/facts: temp 0
  • Creative: temp 0.7-1.0
  • Scales logits before softmax
  • Does not prevent hallucination

Common Mistakes

  • Using high temperature for factual/code tasks
  • Expecting temperature 0 to eliminate hallucination
  • Not setting temperature explicitly (using unpredictable defaults)
  • Using temperature > 1 in production applications