The Agent Loop Runs
Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 17 of 22.
Challenge
MediumLet the agent search hotels too, and keep going until the model stops asking for tools. Keep everything you wrote so far.
- Add a tool named
find_hotelwith the descriptionFind a hotel room in a city with its nightly rateand one required string property,city, described asThe city to stay in. - Add
TOOLS = {"search_flights": search_flights, "find_hotel": find_hotel}and send both tools on every call. - Replace the single follow-up call with a loop: while
response.stop_reasonis"tool_use", print theTool:line for eachtool_useblock, run the functionTOOLSnames with the block's input, send all the results back in one user message, and call the model again. - After the loop, print
Agent:and the text of the text block.
For example, for the input Find a flight to Rome and a hotel in Lisbon the program prints:
Tool: search_flights {'destination': 'Rome'}
Tool: find_hotel {'city': 'Lisbon'}
Agent: Here is what I found:
- Flight to Rome: 95 dollars
- Hotel in Lisbon: 80 dollars a nightThe model asked for both tools in one response, so both results go back in one user message.
Try it yourself
import coddy_ai as anthropic # in production: import anthropic
client = anthropic.Anthropic()
FLIGHTS = {"Lisbon": 120, "Rome": 95, "Tokyo": 640, "Oslo": None}
HOTELS = {"Lisbon": 80, "Rome": 110, "Tokyo": 150, "Oslo": 130}
def search_flights(destination):
if destination not in FLIGHTS:
raise ValueError(f"No flights to {destination}")
if FLIGHTS[destination] is None:
raise ConnectionError("The flight service is temporarily unavailable, try again")
return f"Flight to {destination}: {FLIGHTS[destination]} dollars"
def find_hotel(city):
if city not in HOTELS:
raise ValueError(f"No hotels in {city}")
return f"Hotel in {city}: {HOTELS[city]} dollars a night"
def book_flight(destination):
return f"Booked a seat to {destination} for {FLIGHTS[destination]} dollars"
request = input()
# Write your code below
flight_tool = {
"name": "search_flights",
"description": "Search flights to a destination and give the ticket price",
"input_schema": {
"type": "object",
"properties": {"destination": {"type": "string", "description": "The destination city"}},
"required": ["destination"],
},
}
messages = [{"role": "user", "content": request}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=[flight_tool], messages=messages)
results = []
for block in response.content:
if block.type == "tool_use":
print(f"Tool: {block.name} {block.input}")
result = search_flights(block.input["destination"])
results.append({"type": "tool_result", "tool_use_id": block.id, "content": result})
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": results})
response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=[flight_tool], messages=messages)
for block in response.content:
if block.type == "text":
print("Agent: " + block.text)
All lessons in Tools and Agents
Practice on your own: Online Python compiler