Execute external actions
Run model-selected APIs, database queries, or application functions inside a graph.
AI Agents · Const
Tool calling lets a LangGraph.js workflow execute tools selected by a model. ToolNode handles dispatch, parallel execution, and returns one ToolMessage per tool call.
Tool calling in LangGraph.js connects model-generated tool requests to executable tools. ToolNode reads the tool calls in the latest AI message, runs the requested tools, and returns one ToolMessage per call; multiple calls can run in parallel.
new ToolNode(tools)| Name | Type | Description |
|---|---|---|
tools | StructuredToolInterface[] | Record<string, StructuredToolInterface> | The tools that ToolNode may execute; supplied as a tool array or a mapping of tool names to tools. |
A Runnable node result containing ToolMessages, one for each executed tool call.
import { ToolNode } from "@langchain/langgraph/prebuilt";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
const getWeather = tool(
async ({ city }) => `Sunny in ${city}`,
{
name: "get_weather",
description: "Get the weather for a city",
schema: z.object({ city: z.string() }),
},
);
const toolNode = new ToolNode([getWeather]);
const result = await toolNode.invoke({
messages: [{
role: "assistant",
content: "",
tool_calls: [{
name: "get_weather",
args: { city: "Paris" },
id: "call-1",
type: "tool_call",
}],
}],
});
console.log(result.messages[0].content);ToolNode receives a message containing model-generated tool calls, executes the matching tools, and returns ToolMessages.
Run model-selected APIs, database queries, or application functions inside a graph.
Let several independent model requests finish concurrently.
Use a ready-made execution node instead of implementing tool dispatch and result messages.
ToolNode is a prebuilt LangGraph node that executes the tools requested in the latest AI message. It returns ToolMessages corresponding to the executed calls.
Follow-up: How are concurrent calls handled? Multiple requested tool calls are executed in parallel, producing one ToolMessage for each call.
Construct ToolNode with the tools it may execute, then invoke it with graph state containing the relevant messages. The node matches each model tool call to the configured tool.
Follow-up: What input shape is required? The state must expose a messages collection containing an AI message with tool_calls.
A tool receives the arguments generated by the model as its first argument. In JavaScript, graph-side state and run-scoped context can be accessed through the tool's second ToolRuntime argument.
Follow-up: What is the access boundary? A tool can access only state values passed to ToolNode, so required state must be available there.
ToolNodeToolMessageStateGraphMessageGraphToolRuntimecreateReactAgent