1:1 mentoring with Big Tech AI engineers
LLM & Agentic

Tools & Routing

Define LangGraph tools with @tool, wire ToolNode and tools_condition into an agent loop, use the prebuilt create_agent, and write custom routers that branch on your own state.

Last updated

After this section you can

  • Wire tools into a graph with ToolNode and tools_condition, and explain how the messages map to Claude's tool_use and tool_result
  • Choose between create_agent and a hand-built graph, and write a router for rules the model must not decide
  • Configure ToolNode error handling so recoverable tool failures reach the model instead of ending the run
35

Tools & Routing

A tool is a typed function with a docstring. Wiring it into a graph takes one prebuilt node and one conditional edge, and those two edges are the whole agent loop. Then: the prebuilt agent, routing on your own rules, and what happens when a tool fails.

Key idea

The model never runs your tool. It returns an AIMessage with tool_calls; ToolNode runs them and appends one ToolMessage per call; a conditional edge sends the run back to the model or to END. The while loop from Part 3 becomes a cycle of two edges.

Two edges make the agent loop: a conditional one out of the agent, a fixed one back into it
THE WHOLE AGENT LOOP IS TWO EDGES START agent llm.bind_tools(tools) tools ToolNode(tools) END tool_calls no tool_calls: the model answered tools_condition one ToolMessage per call appended; the agent runs again The loop is not a for statement anywhere. It is the cycle agent → tools → agent, and it ends when the model stops asking. Each round is two super-steps, so recursion_limit=25 allows about a dozen tool rounds.

Related

More in LLM & Agentic

Get full access to all 74+ sections with code examples, diagrams, and interactive animations.

Unlock Premium