Handling the Tool Use Response
In the last lesson, we sent a request asking about the weather in Paris, and we attached a get_weather tool.
When Claude receives that request, it realizes it needs to use the tool. It stops generating a normal text response. Instead, the response object your API call returns will contain a special block type called tool_use.
Inspecting the Response
Usually, response.content[0].type is "text". But when a tool is triggered, you'll see this:
const response = await anthropic.messages.create({ /* payload */ });
// Claude might say "I need to check the weather for that!" before triggering the tool.
console.log(response.content[0].type); // "text"
// The second block will be the tool request
const toolBlock = response.content[1];
console.log(toolBlock.type); // "tool_use"
The tool_use Object
The tool_use block tells your code exactly what to do. It looks like this:
{
"type": "tool_use",
"id": "toolu_01A09q90qw90ll",
"name": "get_weather",
"input": {
"city": "Paris"
}
}
id: A unique ID generated by Anthropic for this specific tool call. You must save this ID, you will need it later.name: Matches the tool name you provided (get_weather). This tells your router which function to actually run.input: A JSON object containing the arguments Claude generated based on your schema. Because you asked for the weather in Paris, Claude automatically filled in"Paris"for thecityargument!
Executing the Code
Now that Claude has handed you the instructions, your Node.js app takes over.
let toolResultData;
if (toolBlock.name === "get_weather") {
const cityArg = toolBlock.input.city; // "Paris"
// Actually run YOUR code!
toolResultData = await myWeatherFunction(cityArg);
}
Claude is paused. Your code is running myWeatherFunction("Paris"). This might be a call to a 3rd party API, a database lookup, or an internal calculation.
Once your code finishes and toolResultData holds the live weather data, you have to hand it back to Claude so it can finally answer the user. We will cover that hand-off in the next lesson.