AI Hints
Home›LangGraph Tutorial›Tools in Graphs
LangGraph · Lesson 32 of 120

ToolNode

Running tool calls.

⏱ 2 min read▶ Video at 33:48🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Tool node is a ready made node that runs tool calls. It reads the last AI message, runs every tool it asks for, and adds a tool message for each result.

ToolNode
From the video: ToolNode
  • A ready-made node
  • Runs every tool call
  • Adds tool messages

Running calls by hand

To see it clearly, we write an AI message with two tool calls ourselves, no model needed. Tool node runs inside a graph, so we give it a tiny graph of its own. Run it. We get two tool messages back. Each has the result, and the ID of the call it answers.

tool_node.pypython
from langchain.messages import AIMessage
from langgraph.graph import MessagesState, StateGraph, START
from langgraph.prebuilt import ToolNode
from shop_tools import check_stock, get_price

ask = AIMessage("", tool_calls=[
    {"name": "check_stock", "args": {"title": "Dune"}, "id": "call_1"},
    {"name": "get_price", "args": {"title": "Dune"}, "id": "call_2"},
])
b = StateGraph(MessagesState); b.add_node("tools", ToolNode([check_stock, get_price]))
b.add_edge(START, "tools")
out = b.compile().invoke({"messages": [ask]})
for m in out["messages"][1:]:
    print(m.type, m.tool_call_id, "->", m.text)
Output
tool call_1 -> 3
tool call_2 -> 9.99
Also uses shop_tools.py
from langchain.tools import tool

BOOKS = {
    "The Hobbit": {"stock": 7, "price": 8.99, "author": "J.R.R. Tolkien"},
    "Dune": {"stock": 3, "price": 9.99, "author": "Frank Herbert"},
    "Circe": {"stock": 0, "price": 10.99, "author": "Madeline Miller"},
}

@tool
def check_stock(title: str) -> int:
    """Copies of a book in stock at Riverside Books."""
    return BOOKS.get(title, {}).get("stock", 0)

@tool
def get_price(title: str) -> float:
    """Price of a book in pounds."""
    return BOOKS.get(title, {}).get("price", 0.0)

@tool
def get_author(title: str) -> str:
    """Author of a book."""
    return BOOKS.get(title, {}).get("author", "unknown")

What ToolNode handles

Tool node runs several calls in parallel. It matches each result to its call ID. And if a tool raises an error, it can return the error as a tool message, so the model can try again.

keyvalue
Parallelseveral calls at once
IDsmatched for you
Errorsreturned as messages
Inputmessages state

Summary

Tool node runs every tool call in the last AI message, and returns tool messages. Next, deciding when to go to the tools.

Key takeaways
  • ToolNode([...])
  • Reads the last AI message
  • Adds ToolMessages
Next up: tools_condition — tool or finish