Session 8· 00· 15 min
Your First Agent (ReAct Pattern)
What you'll learn
- ▸Create a ReAct agent with create_agent + one tool
- ▸Watch the model loop: reason → call tool → read result → answer
- ▸Understand the difference between invoke-once (Session 6) and an autonomous agent loop
Before you start
Create Session8/.venv, install langchain langchain-openai langgraph python-dotenv, copy your .env with OPENAI_API_KEY. Confirm the venv is active.
What you will build
A weather assistant that autonomously decides when to call get_weather, reads the result, and formulates a natural-language answer. Unlike Session 6 where YOU wrote the tool loop, here create_agent handles the loop for you.
The ReAct loop — what happens under the hood
User message
LLM reasons
Tool call?
Execute tool
Loop back
Final answer
The code
00_first_agent.py
from langchain.agents import create_agent ①
from langchain.tools import tool
@tool ②
def get_weather(city: str) -> str:
"""Get the current weather for a city (mock data)."""
data = {"London": "Rainy, 12°C", "Tokyo": "Sunny, 25°C"}
return data.get(city, f"No data for {city}")
agent = create_agent( ③
model=f"openai:{model_name}",
tools=[get_weather],
system_prompt="You are a helpful weather assistant.",
)
result = agent.invoke( ④
{"messages": [{"role": "user", "content": "Weather in Tokyo?"}]}
)①create_agent builds a graph-based ReAct agent using LangGraph under the hood.
②@tool from Session 6 — type hints + docstring = auto-generated schema.
③Three args: model (provider prefix), tools list, system prompt. The agent handles the loop.
④invoke() sends a user message and returns the FULL message trace including tool calls and the final answer.
Run it
$ python 00_first_agent.py
Session 6 vs Session 8 — what changed
Session 6
manual loop
- •YOU wrote the while loop
- •YOU checked msg.tool_calls
- •YOU appended ToolMessage
- •YOU decided when to stop
Session 8
create_agent
- •create_agent handles the loop
- •Automatic tool dispatch
- •Automatic message management
- •Stops when model emits final text
Common errors
Knowledge check
Knowledge Check
What does create_agent give you that Session 6's manual code did not?
Code Check
What does result["messages"] contain after agent.invoke()?
Recap — what you just learned
- ✓create_agent builds an automatic ReAct loop from model + tools + prompt
- ✓invoke() returns the full message trace including tool calls
- ✓You no longer write the while loop — the agent handles it
- ✓Under the hood it is a LangGraph graph with model and tools nodes