Measure how sampling temperature changes variation in model responses.

Download the runnable notebook

This complete lesson was converted from the matching notebook stored locally in the TechClub repository. Outputs are cleared; never add API keys or private data.

Goal, prerequisites, and setup

Compare low and high sampling settings without confusing creativity with correctness. Complete the requests lesson first; know Python functions and basic measurement. Install anthropic and python-dotenv, set ANTHROPIC_API_KEY and ANTHROPIC_MODEL, and record the exact temperature values supported by your provider.

Run the same prompt several times at each setting, save the outputs, and compare variety, relevance, and errors. A single response cannot demonstrate a temperature effect.

"""
TEMPERATURE
-> It controls the randomness of the model's output.
-> A low temperature (e.g., 0.2) makes the output more focused and deterministic.
-> A high temperature (e.g., 0.8) makes the output more diverse and creative.
"""

# Load environment variables
from dotenv import load_dotenv

load_dotenv()
# Create an API client
import os
from anthropic import Anthropic

client = Anthropic()

model = os.getenv("ANTHROPIC_MODEL")
# Helpers
import inspect

def add_message(messages, content, role):
    message = {"role": role, "content": content}
    messages.append(message)

def chat(messages, system=None):
    params = {
        "model": model,
        "max_tokens": 200,
        "messages": messages,
    }

    if system:
        params["system"] = system

    print(params)
    message = client.messages.create(**params)

    return message.content[0].text
messages = []

add_message(messages,
            "Generate a one sentence movie idea",
            role="user")
answer = chat(messages)
print("===== HIGH Temperature =====")
print(answer)
print(" ")

messages = []

add_message(messages,
            "Generate a one sentence movie idea",
            role="user")
answer = chat(messages)
print("===== LOW Temperature =====")
print(answer)
print(" ")

Evidence and references