Chaining Tool Results
Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 14 of 22.
Many requests need two tools in a row, where the second needs what the first returned. Asked What is the price of the kettle with tax?, a model with a get_price tool and an add_tax tool cannot call add_tax yet: it has no price. It calls get_price, reads 30 dollars in the result, then calls add_tax with that number.
get_price {'item': 'kettle'}
add_tax {'price': 30}Your loop makes this possible without knowing about it: it sends each result back and keeps going while stop_reason is "tool_use". A loop that handles only one round stops after get_price, with the second call never made.
Two things help a chain work. Results should state their numbers plainly (30 dollars, not an internal code), because the model copies them into the next call. And an argument that takes a number must be "type": "number" or "integer", so the model passes a value your function can compute with.
Challenge
HardThe train assistant can find a fare in euros. Add a tool that converts it, so the model can chain the two.
- Add a tool to
TOOLSnamedconvert_euros, described asConvert an amount in euros to another currency., with two required properties:amount(a number, the amount in euros) andcurrency(a string, the currency code: USD, JPY or GBP). - Write
convert_euros(amount, currency). It returns<amount> euros is <converted> <currency>, where the converted value isamount * RATES[currency]with 2 decimals. - Add it to
FUNCTIONS.
For example, for the input What does the train to Lyon cost in dollars? the program prints:
Step 1: get_fare {'destination': 'Lyon'}
Step 2: convert_euros {'amount': 48, 'currency': 'USD'}
Answer: Here is what I found:
- Lyon: 48 euros
- 48 euros is 52.32 USDTry it yourself
import coddy_ai as anthropic # in production: import anthropic
client = anthropic.Anthropic()
FARES = {"Lyon": 48, "Nice": 95, "Lille": 35, "Bordeaux": 70}
RATES = {"USD": 1.09, "JPY": 162.0, "GBP": 0.86}
TOOLS = [
{
"name": "get_fare",
"description": "Get the price of a train ticket from Paris to a destination, in euros.",
"input_schema": {
"type": "object",
"properties": {"destination": {"type": "string", "description": "The destination city"}},
"required": ["destination"],
},
},
# Add the convert_euros tool here
]
def get_fare(destination):
return f"{destination}: {FARES[destination]} euros"
# Write convert_euros(amount, currency) here
FUNCTIONS = {"get_fare": get_fare}
request = input()
messages = [{"role": "user", "content": request}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)
step = 0
while response.stop_reason == "tool_use":
messages.append({"role": "assistant", "content": response.content})
results = []
for block in response.content:
if block.type == "tool_use":
step += 1
print(f"Step {step}: {block.name} {block.input}")
output = FUNCTIONS[block.name](**block.input)
results.append({"type": "tool_result", "tool_use_id": block.id, "content": output})
messages.append({"role": "user", "content": results})
response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)
for block in response.content:
if block.type == "text":
print("Answer: " + block.text)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Tools and Agents
3Designing Agents
Writing Tool DescriptionsValidating Tool InputHuman ApprovalChaining Tool ResultsRecap: Expense AgentPractice on your own: Online Python compiler