Menu
CoddyTech

Reading a Tool Call

Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 2 of 22.

A tool_use block carries three things: name (which tool), input (a dict of the arguments the model chose, with the keys of your schema) and id (a unique id such as toolu_..., which the next lesson sends back with the result). Your code reads them and calls the matching Python function.

def get_hours(day):
    return HOURS.get(day, "closed")

for block in response.content:
    if block.type == "tool_use" and block.name == "get_hours":
        print(block.id.startswith("toolu_"))
        print(get_hours(block.input["day"]))

For What are your opening hours on Saturday? this prints True, then 10 am to 2 pm.

The arguments come from the model, so treat them like user input: the item it names may not be in your data. A good function returns a clear message for that case instead of crashing, because that message is what the model reads next.

challenge icon

Challenge

Easy

Run the tool the model asks for. The starter defines get_price, the tool and the call.

  1. Find the block whose type is "tool_use".
  2. Print Tool: and its name.
  3. Print Item: and the item argument from its input.
  4. Call get_price with that item and print Result: and what it returns.

For example, for the input What is the price of the lamp? the program prints:

Tool: get_price
Item: lamp
Result: The lamp costs 40 dollars.

Try it yourself

import coddy_ai as anthropic  # in production: import anthropic

client = anthropic.Anthropic()

PRICES = {"lamp": 40, "desk": 120, "chair": 65}


def get_price(item):
    if item in PRICES:
        return f"The {item} costs {PRICES[item]} dollars."
    return f"No price for {item}."


price_tool = {
    "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"],
    },
}

question = input()
response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=300,
    tools=[price_tool],
    messages=[{"role": "user", "content": question}],
)

# For the tool_use block: print "Tool: <name>", "Item: <item>",
# then run get_price with that item and print "Result: <what it returns>"
quiz iconTest yourself

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