Skip to content
Streak:0Loading your progress…Sign in

Module 9 · Lesson 3 of 3

Tool calling

Let the model request actions; your code executes them and returns results. ~30 min

Lesson notes

Tool calling loop: (1) send tools (name, description, JSON schema) with the request; (2) the model replies with tool_use blocks (id, name, input); (3) your code runs each tool and replies with tool_result blocks referencing the same id; (4) repeat until the model stops asking for tools. The model never executes anything itself.

A tool definition
tools = [{
    "name": "get_order_status",
    "description": "Look up the shipping status of an order by its ID.",
    "input_schema": {
        "type": "object",
        "properties": {"order_id": {"type": "string"}},
        "required": ["order_id"],
        "additionalProperties": False,
    },
}]

Practice

  1. 1. Dispatch tool calls

    hard50 XP

    Implement class ToolRegistry with register(name, model, fn) (model = Pydantic input model) and run(tool_uses) -> list[dict]. For each {"type": "tool_use", "id", "name", "input"} return {"type": "tool_result", "tool_use_id": id, "content": str(result)}. Unknown tool, validation failure, or an exception in fn → same shape with "is_error": True and a short error message as content. Results keep input order. Also implement schemas() returning [{"name", "input_schema"}] from model.model_json_schema().

    Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor

  2. 2. Who runs the tool?

    Quizeasy5 XP
    With client-side tool calling, who executes get_order_status?