An agentic LLM-powered SQL reasoning framework that follows a Planner → Tool (SQL) → Synthesizer architecture to answer complex natural-language questions over relational databases with dependency-aware planning, optimized SQL generation, and grounded natural-language explanations.
This project is designed for:
- NL → SQL with multi-step reasoning
- Dependency-aware analytical queries
- Explainable answers grounded only in database results
- Extensible skill-based agent systems (future: charts, forecasting, web search, etc.)
User Question
↓
[Planner LLM]
- Refines intent
- Reads schema
- Decides: A valid SQL query
↓
[SQL Tool]
- Executes SQL via LLM SQL Agent
↓
[Result]
- Produces final grounded answer
| Component | Role |
|---|---|
| Planner LLM | Intent refinement, task decomposition, dependency planning |
| SQL Tool | Executes schema-aware SQL using LLMs (Gemma/Qwen/DeepSeek) |
| Synthesizer LLM | Converts structured results into answer |
| Orchestrator | LangChain (now), LangGraph (future) |
agentic-sql-reasoner/
│
├── main.py # CLI entry point
├── data/
│ └── chinook.db # Sample SQLite database
│
├── src/
│ ├── agent.py # SQL Agent (LLM + create_sql_agent)
│ ├── config.py # Model, temperature, DB URI
│ └── database.py # SQLAlchemy / LangChain DB wrapper
│
├── requirements.txt
└── README.md
- RAM: 16 GB minimum (32 GB recommended)
- VRAM: 6 GB+ GPU (for local LLMs via Ollama)
- OS: Windows 11 / Linux / macOS
- Python 3.10+
- Ollama (for local LLM inference)
Install Ollama:
Pull a supported model (example):
ollama pull gemma:4b-it
# or
ollama pull qwen2.5:7b
# or
ollama pull deepseek-r1:7b| Model | Role |
|---|---|
| Gemma 2/3 (4B–7B) | SQL generation (tool LLM) |
| Qwen 2.5 / Qwen3 | Planner reasoning |
| DeepSeek-R1 | Multi-step planning + synthesis |
git clone https://github.com/yourname/agentic-sql-reasoner.git
cd agentic-sql-reasoneruv venv
source .venv/bin/activate # Linux / macOS
.venv\Scripts\activate # Windowsuv pip install -r requirements.txtEdit src/config.py:
uv run main.pyThen ask questions like these below:
1. how many customers whose email contains gmail?
2. Tell me first name of all customers whose email contains gmail?
3. Give me the City name of the first two customer?
-
Planner analyzes intent and schema
-
Chooses:
- optimized SQL based on LLM reasoning
-
SQL Tool executes queries
-
Results stored in structured state
-
Synthesizer LLM generates grounded explanation
- No hallucination: answers only from SQL results
- Schema-aware reasoning
- Dependency-safe planning
- Explainable intermediate steps
- Extensible agent-tool architecture
- Not fully language aware and depend on the LLMS, hence using large LLM will give awesome result while small LMs like gemma3:4b etc only answer simple query.
- Highly complex queries involve CTEs, Subquery etc
