LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangGraph
  • Web
  • Channels
  • Pregel
  • Prebuilt
  • Remote
  • Stream
LangGraph SDK
  • Client
  • Auth
  • React
  • Logging
  • React Ui
  • Server
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
LangGraph Checkpoint Redis
  • Shallow
  • Store
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
  • Cli
LangGraph API
LangGraph CLI
LangGraph CUA
  • Utils
LangGraph Supervisor
LangGraph Swarm
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangGraph
WebChannelsPregelPrebuiltRemoteStream
LangGraph SDK
ClientAuthReactLoggingReact UiServer
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
LangGraph Checkpoint Redis
ShallowStore
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
Cli
LangGraph API
LangGraph CLI
LangGraph CUA
Utils
LangGraph Supervisor
LangGraph Swarm
Language
Theme
JavaScript@langchain/langgraphprebuilttoolsCondition
Functionā—Since v1.0

toolsCondition

Copy
toolsCondition(
  state: BaseMessage<MessageStructure<MessageToolSet>, MessageType>[] | StateType<__type>
):
View source on GitHub
"tools"
|
"__end__"

Parameters

NameTypeDescription
state*BaseMessage<MessageStructure<MessageToolSet>, MessageType>[] | StateType<__type>

Example

A conditional edge function that determines whether to route to a tools node or end the graph.

This function is designed to be used as a conditional edge in a LangGraph state graph to implement the common pattern of checking if an AI message contains tool calls that need to be executed.

The function checks the last message in the state for the presence of tool_calls. If the message is an AIMessage with one or more tool calls, it returns "tools", indicating that the graph should route to a tools node (typically a ToolNode) to execute those tool calls. Otherwise, it returns END to terminate the graph execution.

This is a common pattern in agentic workflows where an AI model decides whether to use tools or provide a final response.

The current state of the graph, which can be either:

  • An array of BaseMessage objects, where the last message is checked for tool calls
  • A state object conforming to MessagesAnnotation.State, which contains a messages array
Copy
import { StateGraph, MessagesAnnotation, END, START } from "@langchain/langgraph";
import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt";

const graph = new StateGraph(MessagesAnnotation)
  .addNode("agent", agentNode)
  .addNode("tools", new ToolNode([searchTool, calculatorTool]))
  .addEdge(START, "agent")
  .addConditionalEdges("agent", toolsCondition, ["tools", END])
  .addEdge("tools", "agent")
  .compile();