LangGraph Prebuilt Agents

1.0.8 · active · verified Sat Mar 28

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

Install

Imports

Quickstart

This quickstart demonstrates how to set up and use `create_react_agent` from `langgraph-prebuilt`. It initializes an OpenAI LLM, a Tavily search tool, and then creates an agent capable of using these tools. The agent is then invoked with a query, showcasing its ability to use tools for information retrieval and simple calculations. Ensure `OPENAI_API_KEY` and `TAVILY_API_KEY` are set in your environment for the example to run. [8, 13]

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)

view raw JSON →