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
- Define a tool whose docstring and type hints drive model selection
- Wire ToolNode and tools_condition into a working agent loop
- Choose between the prebuilt create_agent and a hand-built graph
- Write a custom router that branches on your own state, not just tool calls
- Handle a failing tool instead of assuming ToolNode already did
Tools & Routing
A tool is a typed Python function with a docstring. Wiring it into a graph takes one prebuilt node and one conditional edge — and those two lines are the entire agent loop.
The model never runs your tool. It emits a tool_call; ToolNode executes it and appends the result; tools_condition decides whether to go round again. Everything you would hand-write in a while-loop is those two edges.
A tool is a function the model can read
The docstring is not a comment — it is the tool description the model selects on, and the type hints become the JSON schema.
from langchain_core.tools import tool
@tool
def get_order_status(order_id: str) -> str:
"""Look up the current delivery status of a customer order by its ID.
Use for questions about where an order is or when it arrives.
Not for refunds or cancellations.
"""
return f"Order {order_id}: shipped, arriving Tuesday."
# what the model actually sees:
get_order_status.args_schema.model_json_schema()