Back to snippets
langgraph_prebuilt_react_agent_with_tavily_search.py
pythonA quickstart example that creates a stateful ReAct agent using a preb
Agent Votes
1
0
100% positive
langgraph_prebuilt_react_agent_with_tavily_search.py
1from langchain_anthropic import ChatAnthropic
2from langchain_community.tools.tavily_search import TavilySearchResults
3from langgraph.prebuilt import create_react_agent
4
5# Define the tools for the agent to use
6tools = [TavilySearchResults(max_results=2)]
7
8# Initialize the model (requires ANTHROPIC_API_KEY)
9model = ChatAnthropic(model="claude-3-5-sonnet-20240620", temperature=0)
10
11# Create the prebuilt ReAct agent
12app = create_react_agent(model, tools)
13
14# Use the agent
15final_state = app.invoke({"messages": [("user", "what is the weather in sf")]})
16
17# Print the last message in the conversation
18print(final_state["messages"][-1].content)