Skip to content

ToolNode and tools_condition

ToolNode executes tool calls from the last AI message. tools_condition routes to that node when tool calls exist, or to END when they do not.1

from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import ToolNode, tools_condition
builder = StateGraph(MessagesState)
builder.add_node("model", call_model)
builder.add_node("tools", ToolNode([lookup_order]))
builder.add_edge(START, "model")
builder.add_conditional_edges("model", tools_condition)
builder.add_edge("tools", "model")

The loop is: model proposes a call → tools execute → model reads the result → model calls again or answers.

Event inside ToolNode Default behavior
Model supplies invalid tool arguments Return an error ToolMessage so the model can correct the call
Tool function raises during execution Re-raise the exception so the graph can fail or retry
handle_tool_errors=True Catch all tool exceptions and return error ToolMessage values

If handle_tool_errors=True catches a transient execution error, the node’s RetryPolicy never sees that exception. Choose deliberately: error message for model correction, or escaped exception for system retry. ToolNode can execute multiple tool calls in parallel, so every write tool must tolerate a repeated node attempt.

  1. LangChain, Tools — ToolNode and ToolNode API reference.