How does tool use (function calling) work?

Learning path, stage 4 of 5: Agents, tools and MCP · question 2 of 10

You describe tools to the model: a name, a description and a JSON Schema for the inputs. When the model wants one, it returns a structured tool-call block instead of plain text. Your code runs the function and sends the result back as a tool-result message. The model never runs anything itself. Your code decides what actually happens.

The round trip

  1. Request with tools: [...] and the user message.
  2. The response has stop_reason: "tool_use" and one or more tool_use blocks (id, name, input).
  3. You run each tool and reply with tool_result blocks that reference the tool_use_id.
  4. The model continues: it calls more tools or gives a final answer.

Writing good tools

  • Descriptions matter most. Say what it does, when to use it, what it returns and any limits.
  • Use few, well-scoped tools rather than many overlapping ones.
  • Return short, useful results: IDs plus summaries, not 10k-line dumps.
  • Return errors as results (is_error: true) so the model can recover.
  • Use strict schemas (strict: true, additionalProperties: false) for guaranteed-valid arguments.
  • When the model makes parallel calls, return all the results in one user message.

Sources

Code example

tsx
1// One tool-use round trip with the Anthropic SDK.
2import Anthropic from "@anthropic-ai/sdk";
3
4const client = new Anthropic();
5
6const tools: Anthropic.Tool[] = [{
7 name: "get_weather",
8 description: "Current weather for a city. Use when the user asks about weather right now.",
9 strict: true,
10 input_schema: {
11 type: "object",
12 properties: { city: { type: "string", description: "City name, e.g. 'Pune'" } },
13 required: ["city"],
14 additionalProperties: false,
15 },
16}];
17
18const messages: Anthropic.MessageParam[] = [{ role: "user", content: "Do I need an umbrella in Pune?" }];
19const first = await client.messages.create({ model: "claude-opus-5-5", max_tokens: 1024, tools, messages });
20
21if (first.stop_reason === "tool_use") {
22 messages.push({ role: "assistant", content: first.content });
23 const results: Anthropic.ToolResultBlockParam[] = first.content
24 .filter((b): b is Anthropic.ToolUseBlock => b.type === "tool_use")
25 .map((call) => ({ type: "tool_result", tool_use_id: call.id, content: '{"rain_chance": 0.8}' }));
26 messages.push({ role: "user", content: results });
27
28 const final = await client.messages.create({ model: "claude-opus-5-5", max_tokens: 1024, tools, messages });
29 console.log(final.content.flatMap((b) => (b.type === "text" ? [b.text] : [])).join(""));
30}

To run it: follow the one-time setup guide, add npm i @anthropic-ai/sdk zod @modelcontextprotocol/sdk, save the code as a .ts file and run npx tsx file.ts. Anything marked declare is a part you supply.

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the AI Agents & LLMs set.

Open in study app