Choosing Between Tools
Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 4 of 22.
Pass several tools and the model picks the one that fits the request, reading each tool's name and description. Your code then has to run whichever it picked. A long if/elif chain on block.name works, but a dict from tool name to function grows better: one line per tool.
functions = {"add": add, "multiply": multiply}
name = "multiply"
arguments = {"a": 6, "b": 7}
print(functions[name](**arguments))This prints 42. **arguments passes the dict's keys as keyword arguments, so block.input fits straight into a function whose parameters have the schema's property names.
Keep every tool's name in the dict: a model only calls tools you listed, so a missing entry is a bug in your program, not in the model's choice. If no tool fits, the model answers in text and there is nothing to run.
Challenge
MediumThe starter gives the model three tools: weather, local time and prices. Run whichever one it picks.
- Make a dict that maps each tool name to its Python function.
- For each block whose type is
"tool_use", printTool:and its name, then run the function from the dict with**block.inputand printResult:and what it returns. - For a block whose type is
"text", printAnswer:and its text.
For example, for the input What time is it in Rome? the program prints:
Tool: get_time
Result: Rome: 14:05Try it yourself
import coddy_ai as anthropic # in production: import anthropic
client = anthropic.Anthropic()
WEATHER = {"Rome": "21 C and sunny", "Tokyo": "16 C and cloudy", "Oslo": "4 C and windy"}
TIMES = {"Rome": "14:05", "Tokyo": "22:05", "Oslo": "14:05"}
PRICES = {"lamp": 40, "desk": 120}
def get_weather(city):
return f"{city}: {WEATHER.get(city, 'no data')}"
def get_time(city):
return f"{city}: {TIMES.get(city, 'no data')}"
def get_price(item):
return f"{item}: {PRICES.get(item, 'no price')} dollars"
def city_schema():
return {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}
tools = [
{"name": "get_weather", "description": "Get the current weather for a city", "input_schema": city_schema()},
{"name": "get_time", "description": "Get the current local time in a city", "input_schema": city_schema()},
{"name": "get_price", "description": "Look up the price of an item in dollars",
"input_schema": {"type": "object", "properties": {"item": {"type": "string"}}, "required": ["item"]}},
]
question = input()
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=300,
tools=tools,
messages=[{"role": "user", "content": question}],
)
# 1. Make a dict that maps each tool name to its Python function.
# 2. For each tool_use block, print "Tool: <name>", run the function with **block.input
# and print "Result: <what it returns>".
# 3. If the model answered in text instead, print "Answer: <text>".
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Tools and Agents
1Tool Use Basics
Defining a ToolReading a Tool CallReturning Tool ResultsChoosing Between ToolsRecap: Weather DeskPractice on your own: Online Python compiler