How to Intermediate · 3 min read

How to add tools to LangGraph agent

Quick answer
To add tools to a LangGraph agent, define your tool functions or wrappers and register them with the agent's tool registry. Then instantiate the agent with these tools so it can invoke them during execution using LangChain or the LangGraph API.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install langchain_openai langchain_community openai>=1.0

Setup environment

Install the necessary Python packages and set your OpenAI API key as an environment variable.

bash
pip install langchain_openai langchain_community openai>=1.0

# On Linux/macOS
export OPENAI_API_KEY=os.environ["OPENAI_API_KEY"]

# On Windows (PowerShell)
setx OPENAI_API_KEY os.environ["OPENAI_API_KEY"]

Step by step example

This example shows how to create a simple tool, add it to a LangGraph agent, and run the agent to invoke the tool.

python
import os
from openai import OpenAI
from langchain_openai import ChatOpenAI
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import TextLoader
from langchain_core.prompts import ChatPromptTemplate

# Define a simple tool function
class CalculatorTool:
    def run(self, query: str) -> str:
        try:
            # Evaluate simple math expressions safely
            result = str(eval(query, {"__builtins__": None}, {}))
            return f"Result: {result}"
        except Exception as e:
            return f"Error: {str(e)}"

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Instantiate the tool
calc_tool = CalculatorTool()

# Simulate LangGraph agent tool registry
class LangGraphAgent:
    def __init__(self, tools):
        self.tools = {tool.__class__.__name__: tool for tool in tools}

    def run_tool(self, tool_name, input_text):
        if tool_name in self.tools:
            return self.tools[tool_name].run(input_text)
        else:
            return f"Tool {tool_name} not found."

# Create agent with the calculator tool
agent = LangGraphAgent(tools=[calc_tool])

# Run the agent with a tool invocation
output = agent.run_tool("CalculatorTool", "2 + 3 * 4")
print(output)
output
Result: 14

Common variations

  • Use async tool functions for I/O-bound tasks.
  • Integrate external APIs as tools by wrapping their calls.
  • Use different LLM models like gpt-4o or claude-3-5-sonnet-20241022 for agent reasoning.
  • Combine multiple tools for complex workflows.

Troubleshooting

  • If the agent cannot find a tool, verify the tool is registered in the agent's tool dictionary.
  • For API errors, check your OPENAI_API_KEY environment variable is set correctly.
  • Ensure your tool methods handle exceptions to avoid agent crashes.

Key Takeaways

  • Define tools as classes or functions with a standard interface for LangGraph agents.
  • Register tools in the agent's tool registry to enable invocation during execution.
  • Use the OpenAI SDK and LangChain libraries to build and integrate tools seamlessly.
  • Handle errors inside tools to maintain agent stability.
  • Experiment with different LLM models and async tools for advanced workflows.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗