Project Overview
Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 16 of 22.
Challenge
MediumOver four lessons you build a travel agent: a program that lets the model search flights and hotels, then book a seat. The starter holds the trip data and three Python functions; today you give the model one of them.
- Define a tool named
search_flightswith the descriptionSearch flights to a destination and give the ticket priceand one required string property,destination, described asThe destination city. - Send the request as the only user message with
tools=[...],model="claude-opus-5-5"andmax_tokens=500. - For each
tool_useblock, printTool:, the tool's name, a space and its input, then runsearch_flightswith the destination. - Append the assistant's content and a user message with the
tool_resultblocks, call the model again with the same tools, and printAgent:and the text of the text block.
For example, for the input Find a flight to Lisbon the program prints:
Tool: search_flights {'destination': 'Lisbon'}
Agent: Here is what I found: Flight to Lisbon: 120 dollarsTry 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
All lessons in Tools and Agents
Practice on your own: Online Python compiler