Varied Answers
Part of the Calling Models From Code section of Coddy's AI Engineering journey. Lesson 7 of 22.
A model writes its reply one token at a time, and at each step it picks among likely next tokens with some randomness. This is called sampling, and it is why the same question can get a different wording each time:
for attempt in range(2):
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=100,
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.content[0].text)This prints Hello! How can I help you today?, then Hi there! What can I do for you?. The stand-in changes its wording in a fixed order; a real model varies freely.
Older APIs let you lower the randomness with a temperature argument. Current Claude models take no temperature: API version 2026-10-21 refuses it, and the Python SDK raises a TypeError if you pass it. You steer the output with the prompt, with effort and with structured output instead. Code that uses a reply should check what matters (a keyword, a number, a label), never that the text matches an exact string.
Challenge
EasyAsk the same question twice and compare the answers the right way. The starter reads a question and a keyword, and ask(text) returns the model's reply.
- Call
ask(question)twice. PrintFirst:and the first answer, thenSecond:and the second. - Print
Same wording: yesif the two answers are exactly equal, elseSame wording: no. - Print
Both mention <keyword>: yesif the keyword appears in both answers, elseBoth mention <keyword>: no.
For example, for the input What is the capital of Canada? and Ottawa the program prints:
First: The capital of Canada is Ottawa.
Second: Sure. The capital of Canada is Ottawa.
Same wording: no
Both mention Ottawa: yesTry it yourself
import coddy_ai as anthropic # in production: import anthropic
client = anthropic.Anthropic()
question = input()
keyword = input()
def ask(text):
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=200,
messages=[{"role": "user", "content": text}],
)
return response.content[0].text
# Ask the question twice, print both answers, then compare them
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Calling Models From Code
2Controlling the Output
Max Tokens and Stop ReasonsVaried AnswersEffort and ThinkingStreaming ResponsesRecap: Answer BudgetPractice on your own: Online Python compiler