Menu
CoddyTech

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 icon

Challenge

Easy

Ask 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.

  1. Call ask(question) twice. Print First: and the first answer, then Second: and the second.
  2. Print Same wording: yes if the two answers are exactly equal, else Same wording: no.
  3. Print Both mention <keyword>: yes if the keyword appears in both answers, else Both 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: yes

Try 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Calling Models From Code

Practice on your own: Online Python compiler