A sophisticated agentic system built with LangGraph that demonstrates advanced routing, execution planning, and reflection-based improvements.
This POC (Proof of Concept) showcases a multi-layered agent architecture that:
- Intelligently Routes queries to different processing paths (direct LLM, simple ReAct, or complex planning)
- Executes Plans with tool integration and step-by-step execution tracking
- Reflects & Improves through self-reflection mechanisms for better results
- Persists State using PostgreSQL checkpoint storage for conversation continuity
-
Classifier Node: Determines query complexity and routes accordingly
direct_llm: Simple questions answered directlysimple_react: React pattern for straightforward tool usecomplex_planning: Multi-step planning with reflection
-
Planning System: For complex queries
- Planner: Creates action plans
- Executor: Executes planned steps with tool integration
- Reflection: Analyzes results and prevents infinite loops
-
Tool Integration: Extensible tool system with built-in tools
add_numbers: Basic arithmeticcalculate_fuel_efficiency: Domain-specific calculation
-
State Management: Dual-state architecture
ConversationState: Long-lived chat historyExecutionState: Per-run execution context
Uses PostgreSQL with LangGraph's checkpoint system for:
- Thread-based conversation persistence
- State recovery across sessions
- Complete execution history
- Python 3.10+
- PostgreSQL database (for checkpointing)
- Google API Key (for Gemini LLM)
-
Clone the repository
git clone <repository-url> cd langgraph-agent-system
-
Create a virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Configure environment variables
cp .env.example .env
Edit
.envwith your actual credentials:# Required: Google API key for Gemini LLM GOOGLE_API_KEY=your_actual_api_key_here # Required: PostgreSQL credentials DB_PASSWORD=your_secure_database_password DB_USER=admin DB_HOST=localhost DB_PORT=5432 DB_NAME=langgraph_db⚠️ Important: Never commit.envfile to version control (already in.gitignore) -
Set up PostgreSQL
# Create database createdb langgraph_db # Test connection psql -U admin -d langgraph_db -c "SELECT 1;"
All sensitive data must be configured via environment variables in .env file:
| Variable | Required | Description | Example |
|---|---|---|---|
GOOGLE_API_KEY |
Yes | Google API key for Gemini LLM | AIzaSy... |
DB_PASSWORD |
Yes | PostgreSQL admin password | Your secure password |
DB_USER |
No | PostgreSQL user (default: admin) | admin |
DB_HOST |
No | Database host (default: localhost) | localhost |
DB_PORT |
No | Database port (default: 5432) | 5432 |
DB_NAME |
No | Database name (default: langgraph_db) | langgraph_db |
See .env.example for full reference with comments.
langgraph-agent-system/
├── main.py # Entry point demonstrating usage
├── db.py # PostgreSQL checkpointer setup
├── agent/
│ ├── __init__.py # Package exports
│ ├── config.py # LLM configuration
│ ├── state.py # State definitions (TypedDict)
│ ├── graph.py # Graph construction and routing
│ ├── nodes.py # Node implementations
│ ├── router.py # Conditional routing logic
│ └── tools.py # Tool definitions
├── requirements.txt # Python dependencies
├── README.md # This file
└── .env.example # Environment variables template
python main.pyThis executes an example query demonstrating:
- Query classification
- Plan generation
- Tool execution
- Reflection and refinement
- Result aggregation
The system outputs:
- Message Trace: Full conversation history
- Structured Memory: Step results from execution
- Tool Results: Outputs from executed tools
- Final Response: Agent's final answer
- Queries are classified before processing
- Simple queries bypass expensive planning
- Complex queries get dedicated planning and reflection
- Plans broken into tasks with indices
- Context maintained across steps
- Tool results accumulated in state
- Self-reflection on execution quality
- Learns from tool results
- Prevents infinite loops with attempt counting
- Conversation state (messages) - append-only
- Execution state (plans, results) - reset per run
- Thread-based session management
- PostgreSQL checkpoint storage
Defines the TypedDict schemas for:
ConversationState: Manages chat history with append-only semanticsExecutionState: Tracks execution progress, plans, and tool resultsAgentState: Combined state for the graph
Constructs the LangGraph StateGraph with:
- Node registrations
- Entry point configuration
- Conditional routing edges
Implements all processing nodes:
- Classifier, routers, planners, executors
- Tool invocation logic
- Reflection mechanisms
Conditional routing functions:
- Path selection based on query classification
- Tool usage decisions
- Reflection triggers
- API Key: Currently hardcoded in
config.py- move to environment variable for production - Database: PostgreSQL dependency for checkpointing - can be replaced with file-based storage for simple demos
- LLM: Uses Gemini 2.5 Flash - can be swapped for other LangChain-compatible models
- Tool Execution: Synchronous execution - can be extended to async
- Async tool execution
- Additional domain-specific tools
- Web interface for testing
- Metrics and performance tracking
- Multi-agent collaboration
- RAG integration for knowledge-based queries
This project is licensed under the MIT License - see the LICENSE file for details.
For questions, issues, or contributions, please open an issue on the repository or contact the project maintainers.