LangGraph Prebuilt Agents
LangGraph Prebuilt is a Python library offering high-level APIs for creating and executing LangGraph agents and tools. It simplifies building complex agentic workflows by providing pre-packaged components and architectures, reducing the need for low-level graph construction. Currently at version 1.0.8, this library is part of the broader LangGraph ecosystem, which undergoes frequent updates, with `langgraph-prebuilt` releases typically aligning with major feature additions to its high-level components. [2, 18]
Warnings
- gotcha LangGraph is a low-level framework, while `langgraph-prebuilt` offers higher-level abstractions. Mixing low-level graph construction (e.g., `StateGraph`, `add_node`) with prebuilt agents (like `create_react_agent`) without understanding LangGraph's state management can lead to unexpected behavior or difficult-to-debug issues. Stick to one paradigm or thoroughly understand how state is managed across both. [3, 5, 6]
- gotcha Prebuilt agents often rely on external services (LLMs, search tools). Failing to set required API keys as environment variables (e.g., `OPENAI_API_KEY`, `TAVILY_API_KEY`) or passing incorrect values will cause agents to fail silently or with authentication errors. [1, 6, 13]
- deprecated The LangGraph ecosystem, including `langgraph-prebuilt`, is actively developed. Older patterns for agents or tool integration, while functional, might be superseded by more efficient, flexible, or recommended approaches in newer releases. [7]
Install
-
pip install langgraph-prebuilt langchain-openai tavily-python
Imports
- create_react_agent
from langgraph.prebuilt import create_react_agent
- ToolNode
from langgraph.prebuilt import ToolNode
- tools_condition
from langgraph.prebuilt import tools_condition
- MessagesState
from langgraph.graph import MessagesState
Quickstart
import os
from langchain_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langgraph.prebuilt import create_react_agent
from typing import Annotated, Sequence, TypedDict
from langchain_core.messages import BaseMessage
# Set API keys (replace with actual keys or set as environment variables)
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "YOUR_OPENAI_API_KEY")
os.environ["TAVILY_API_KEY"] = os.environ.get("TAVILY_API_KEY", "YOUR_TAVILY_API_KEY")
# Define the agent state (LangGraph requires a defined state)
class AgentState(TypedDict):
messages: Annotated[Sequence[BaseMessage], lambda x, y: x + y]
# Initialize LLM and tools
llm = ChatOpenAI(model="gpt-4o-mini") # or any other tool-calling capable LLM
search_tool = TavilySearchResults(max_results=3)
tools = [search_tool]
# Create the prebuilt ReAct agent
agent_executor = create_react_agent(llm, tools=tools)
# Example usage
# Ensure OPENAI_API_KEY and TAVILY_API_KEY are set
if os.environ["OPENAI_API_KEY"] == "YOUR_OPENAI_API_KEY" or os.environ["TAVILY_API_KEY"] == "YOUR_TAVILY_API_KEY":
print("Please set OPENAI_API_KEY and TAVILY_API_KEY environment variables or replace placeholders.")
else:
response = agent_executor.invoke(
{"messages": [("human", "What is the weather in London and what is 123 + 456?")]}
)
print(response["messages"][-1].content)