Lesson 2 / 25

Tool Calling Step by Step

Follow one tool call from schema to result, including the tool_use and tool_result messages.

The message exchange

You send the user message plus a list of tool definitions (name, description, JSON schema for inputs). If the model wants a tool, it replies with a tool_use block holding the tool name, an id and the arguments, and stops. You run the function, then send a tool_result block with the same id. The model reads it and either asks for another tool or gives the final answer.

The shapes involved

This trace is simplified. The id links the result to the request, which matters when several tools are called at once.

// 1. model asks
{ "type": "tool_use", "id": "toolu_01", "name": "get_weather", "input": { "city": "Pune" } }

// 2. your code replies
{ "type": "tool_result", "tool_use_id": "toolu_01", "content": "31°C, clear" }

Validate the arguments

The model can send wrong or unexpected arguments. Validate them like any untrusted input before running the function, and return a helpful error message if they are bad.

Quick check: What does the tool_use id let you do?

  • Match each result to the request that caused it
  • Encrypt the message
  • Choose the model size
  • Skip validation
Answer

Match each result to the request that caused it — Results are paired with requests by id, which is essential with parallel tool calls.