How to add tools to LangGraph agent
Quick answer
To add tools to a
LangGraph agent, define tool functions and register them as nodes or call them within node functions in the graph. Use the StateGraph API to add nodes that invoke these tools and connect them to the graph flow.PREREQUISITES
Python 3.8+pip install langgraphBasic knowledge of Python functions and typing
Setup
Install the langgraph package via pip and import the necessary classes. Ensure you have Python 3.8 or higher.
pip install langgraph output
Collecting langgraph Downloading langgraph-0.1.0-py3-none-any.whl (10 kB) Installing collected packages: langgraph Successfully installed langgraph-0.1.0
Step by step
Define your tool as a Python function, then create a StateGraph and add nodes that call this tool. Connect nodes to control the flow and compile the graph into an app. Finally, invoke the app with initial state.
from langgraph.graph import StateGraph, END
from typing import TypedDict
# Define the state type
class State(TypedDict):
messages: list[str]
# Define a tool function
def my_tool(input_text: str) -> str:
# Example tool: reverses the input string
return input_text[::-1]
# Define a node that uses the tool
def tool_node(state: State) -> State:
input_text = state["messages"][-1] if state["messages"] else ""
tool_result = my_tool(input_text)
new_messages = state["messages"] + [f"Tool output: {tool_result}"]
return {"messages": new_messages}
# Create the graph
graph = StateGraph(State)
graph.add_node("tool_node", tool_node)
graph.set_entry_point("tool_node")
graph.add_edge("tool_node", END)
# Compile the graph into an app
app = graph.compile()
# Invoke the app
initial_state = {"messages": ["Hello LangGraph"]}
result = app.invoke(initial_state)
print(result["messages"]) output
['Hello LangGraph', 'Tool output: hpargnaL olleH']
Common variations
- Use async functions for nodes if your tools require asynchronous calls.
- Chain multiple tool nodes by adding edges between them.
- Use more complex state types with additional fields for richer context.
- Integrate external APIs or SDKs inside tool functions for extended capabilities.
import asyncio
from langgraph.graph import StateGraph, END
from typing import TypedDict
class State(TypedDict):
messages: list[str]
async def async_tool(input_text: str) -> str:
await asyncio.sleep(0.1) # simulate async operation
return input_text.upper()
async def async_node(state: State) -> State:
input_text = state["messages"][-1] if state["messages"] else ""
tool_result = await async_tool(input_text)
new_messages = state["messages"] + [f"Async tool output: {tool_result}"]
return {"messages": new_messages}
graph = StateGraph(State)
graph.add_node("async_node", async_node)
graph.set_entry_point("async_node")
graph.add_edge("async_node", END)
app = graph.compile()
import asyncio
result = asyncio.run(app.invoke(initial_state={"messages": ["hello async"]}))
print(result["messages"]) output
['hello async', 'Async tool output: HELLO ASYNC']
Troubleshooting
- If you see
TypeErrorabout state mismatch, ensure yourTypedDictmatches the node return type. - If nodes do not execute, verify you set the entry point correctly with
set_entry_point(). - For async nodes, use
asyncio.run()or an async event loop to invoke the app. - Check for missing edges if the graph does not terminate properly.
Key Takeaways
- Define tools as Python functions and call them inside LangGraph node functions.
- Add nodes to the graph with
add_node()and connect them with edges. - Use
set_entry_point()to specify the starting node of the agent. - Compile the graph with
compile()and invoke it with initial state. - Async tools require async node functions and proper event loop handling.