# toolsCondition

> **Function** in `@langchain/langgraph`

📖 [View in docs](https://reference.langchain.com/javascript/langchain-langgraph/prebuilt/toolsCondition)

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.

## Signature

```javascript
toolsCondition(state: BaseMessage<MessageStructure<MessageToolSet>, MessageType>[] | StateType<__type>): "tools" | "__end__"
```

## Description

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.

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `state` | `BaseMessage<MessageStructure<MessageToolSet>, MessageType>[] \| StateType<__type>` | Yes | 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 |

## Returns

`"tools" | "__end__"`

A string indicating the next node to route to:
  - `"tools"` - If the last message contains tool calls that need to be executed
  - `END` - If there are no tool calls, indicating the graph should terminate

## Examples

```typescript
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();
```

---

[View source on GitHub](https://github.com/langchain-ai/langgraphjs/blob/5e9f3cb532bd006012ec0eab2bb5b005b22c2ebb/libs/langgraph-core/src/prebuilt/tool_node.ts#L342)