Recap: Answer Budget
Part of the Calling Models From Code section of Coddy's AI Engineering journey. Lesson 10 of 22.
Challenge
MediumGet 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.
- Make at most 3 tries. Each try sends the question with
model="claude-opus-5-5",max_tokensset to the budget andoutput_config={"effort": effort}. - After each try, print
Try <n>: max_tokens=<budget>, stop=<stop reason>, counting tries from 1. - Stop trying when the stop reason is not
max_tokens; otherwise double the budget for the next try. - 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, orAnswer (cut):and the text if the last reply was still cut short. - 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: 5Each 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
2Controlling the Output
Max Tokens and Stop ReasonsVaried AnswersEffort and ThinkingStreaming ResponsesRecap: Answer BudgetPractice on your own: Online Python compiler