The Agent Loop
Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 6 of 22.
Some requests need more than one tool call. To price a lamp in euros, the model first needs the price, and only then can it ask for a conversion. It cannot plan both calls at once, because the second depends on the first result.
An agent loop handles this: call the model, and while it answers with stop_reason == "tool_use", run the tools it asked for, send the results back and call it again. When the stop reason is anything else, the text block holds the final answer.
response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=TOOLS, messages=messages)
while response.stop_reason == "tool_use":
messages.append({"role": "assistant", "content": response.content})
results = [run_tool(block) for block in response.content if block.type == "tool_use"]
messages.append({"role": "user", "content": results})
response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=TOOLS, messages=messages)Each pass appends two turns: the model's tool calls, then your results. The list grows, and the model reads all of it on every call. Production code often hands this loop to the SDK's tool runner (client.beta.messages.tool_runner) or to Managed Agents; writing it once shows what they do for you.
Challenge
MediumWrite the agent loop for a shop's price assistant. The starter defines two tools, the functions behind them in FUNCTIONS, and the first message.
- Call the model with
model="claude-opus-5-5",max_tokens=500,tools=TOOLSand the messages. - While the stop reason is
"tool_use": append the assistant turn, run everytool_useblock withFUNCTIONS[block.name](**block.input), printingTool:, the name, a space and the input dict for each one, append all results as one user message, and call the model again. - Print
Answer:and the text of the final response. - Print
Steps:and how many times you called the model.
For example, for the input What is the price of the mug in yen? the program prints:
Tool: get_price {'item': 'mug'}
Tool: convert_currency {'amount': 8, 'currency': 'JPY'}
Answer: Here is what I found:
- The mug costs 8 dollars
- 8 dollars is 1200.00 JPY
Steps: 3Try it yourself
import coddy_ai as anthropic # in production: import anthropic
client = anthropic.Anthropic()
PRICES = {"lamp": 40, "desk": 120, "chair": 65, "mug": 8}
RATES = {"EUR": 0.92, "GBP": 0.79, "JPY": 150.0}
def get_price(item):
if item in PRICES:
return f"The {item} costs {PRICES[item]} dollars"
return f"No price for {item}"
def convert_currency(amount, currency):
return f"{amount} dollars is {amount * RATES[currency]:.2f} {currency}"
TOOLS = [
{
"name": "get_price",
"description": "Look up the price of an item in dollars",
"input_schema": {
"type": "object",
"properties": {"item": {"type": "string", "description": "The item to price"}},
"required": ["item"],
},
},
{
"name": "convert_currency",
"description": "Convert a dollar amount to another currency",
"input_schema": {
"type": "object",
"properties": {
"amount": {"type": "number", "description": "The amount in dollars"},
"currency": {"type": "string", "description": "The currency code, such as EUR"},
},
"required": ["amount", "currency"],
},
},
]
FUNCTIONS = {"get_price": get_price, "convert_currency": convert_currency}
request = input()
messages = [{"role": "user", "content": request}]
# Run the agent loop: print "Tool: <name> <input>" for every call,
# then "Answer: " and the final text, then "Steps: " and the number of API calls
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Tools and Agents
Practice on your own: Online Python compiler