Menu
CoddyTech

Recap: Answer Budget

Part of the Calling Models From Code section of Coddy's AI Engineering journey. Lesson 10 of 22.

challenge icon

Challenge

Medium

Get a whole answer without paying for a huge limit up front: start small and double the limit while the answer is cut short. The starter reads a question, a starting token budget and an effort level.

  1. Make at most 3 tries. Each try sends the question with model="claude-opus-5-5", max_tokens set to the budget and output_config={"effort": effort}.
  2. After each try, print Try <n>: max_tokens=<budget>, stop=<stop reason>, counting tries from 1.
  3. Stop trying when the stop reason is not max_tokens; otherwise double the budget for the next try.
  4. Take the text of the last reply's text block, or an empty string when it has none (thinking can use up a small budget). Print Answer: and the text, or Answer (cut): and the text if the last reply was still cut short.
  5. Print Output tokens: and the last reply's output token count.

For example, for the input What is 9 * 9?, 50 and medium the program prints:

Try 1: max_tokens=50, stop=end_turn
Answer: 9 * 9 = 81
Output tokens: 5

Each new try is a new sample, so its wording can differ from the try before.

Try it yourself

import coddy_ai as anthropic  # in production: import anthropic

client = anthropic.Anthropic()

question = input()
budget = int(input())
effort = input()

# Write your code below

All lessons in Calling Models From Code

Practice on your own: Online Python compiler