Run independent operations concurrently
A model requests weather, calendar, and database operations in one response.
AI Agents · Const
ToolNode executes the tools requested by the latest AIMessage. Multiple requested tools run in parallel, and the node returns one ToolMessage for each call.
Parallel tool calls are handled by ToolNode: it reads the tool calls in the latest AIMessage, runs the requested tools concurrently, and returns a separate ToolMessage for each call. This is useful in a StateGraph or MessageGraph when independent operations can be performed during the same model step.
new ToolNode(tools)A list of ToolMessages, one for each tool call.
import { ToolNode } from "@langchain/langgraph/prebuilt";
import { AIMessage, ToolMessage } from "@langchain/core/messages";
const tools = [
async function getWeather({ city }: { city: string }) {
return `${city}: sunny`;
},
async function getTime({ city }: { city: string }) {
return `${city}: 12:00`;
},
];
const toolNode = new ToolNode(tools);
const message = new AIMessage({
content: "",
tool_calls: [
{ name: "getWeather", args: { city: "Boston" }, id: "1", type: "tool_call" },
{ name: "getTime", args: { city: "Tokyo" }, id: "2", type: "tool_call" },
],
});
const result = await toolNode.invoke({ messages: [message] });
console.log(result.messages.map((m: ToolMessage) => m.content));ToolNode receives one AI message containing multiple tool calls and executes those calls in parallel, returning one ToolMessage for each result.
A model requests weather, calendar, and database operations in one response.
A graph needs standard tool execution without implementing dispatch logic.
A workflow uses message-based state or a messages field in graph state.
ToolNode is a prebuilt node that executes the tools requested in the most recent AIMessage. When that message contains several calls, ToolNode runs them in parallel and returns one ToolMessage per call.
Follow-up: How would you represent the input? Pass a graph state containing a messages key with the AIMessage, or use the corresponding MessageGraph input.
The node returns a list of ToolMessages. Each message contains the result of one requested tool call, allowing the graph or model to process all completed results.
Follow-up: What must downstream code avoid assuming? It must not treat the result as one tool output; it should iterate over the returned messages.
ToolNode can be used in either a StateGraph with a messages key or in a MessageGraph. The model first produces an AIMessage containing tool calls, and the node executes those calls.
Follow-up: When is this pattern most useful? When independent tools can answer different parts of a request during one model step.
ToolNodeAIMessageToolMessageStateGraphMessageGraph