Overview
LangGraph is a framework for building stateful LLM applications, making it ideal for constructing ReAct (Reasoning and Acting) agents. This starter shows how to build a custom ReAct agent with full control over state management, tool execution, and decision flow.Features
- Custom ReAct agent implementation
- Stateful graph-based architecture
- Weather forecasting with Open-Meteo API
- Conditional edge logic
- Message history management
- Full control over agent behavior
Prerequisites
- Python 3.10 or higher
- Nebius API key from Nebius Token Factory
Installation
Implementation
State Definition
Define the agent’s state structure:Tool Implementation
Create a weather forecasting tool:Model Setup
Initialize the LLM with Nebius AI:Node Implementation
Define agent nodes for tool execution and model calls:Graph Construction
Build the agent graph:Running the Agent
Usage
You can run this code in a Jupyter notebook or Python script:Technical Details
LangGraph Architecture
State
Shared data structure tracking conversation and steps
Nodes
Logic units for LLM calls and tool execution
Edges
Control flow between nodes (fixed or conditional)
Key Components
State Management- Uses
TypedDictfor state structure add_messagesreducer for message list management- Tracks conversation history and metadata
- LangChain tool decorator for easy definition
- Pydantic models for input validation
- Direct return option for immediate responses
Extending the Agent
Add More Tools
Add State Tracking
Add More Nodes
Use Checkpointing
Best Practices
State Design
State Design
- Keep state minimal and focused
- Use type hints for clarity
- Leverage reducers like
add_messages - Document state fields clearly
Node Implementation
Node Implementation
- Keep nodes focused on single responsibilities
- Handle errors gracefully within nodes
- Return state updates, not full state
- Use async nodes for I/O operations
Graph Design
Graph Design
- Start simple, add complexity as needed
- Use conditional edges for branching logic
- Test each node independently
- Visualize with
draw_mermaid_png()
Visualization
Visualize your graph structure:Comparison with Pre-built Agents
LangGraph offerscreate_react_agent for quick setup, but building from scratch gives you:
- Full control over state structure
- Custom node logic
- Flexible edge conditions
- Better debugging capabilities
- Easier customization
Next Steps
Advanced LangGraph
Build complex graph-based workflows
Multi-Agent Systems
Create agent teams with LangGraph