examples/text-to-sql-agent/README.md
A natural language to SQL query agent powered by LangChain's Deep Agents framework. This is an advanced version of a text-to-SQL agent with planning, filesystem, and subagent capabilities.
Deep Agents is a sophisticated agent framework built on LangGraph that provides:
write_todos toolUses the Chinook database - a sample database representing a digital media store.
git clone https://github.com/langchain-ai/deepagents.git
cd deepagents/examples/text-to-sql-agent
# Download the SQLite database file
curl -L -o chinook.db https://github.com/lerocha/chinook-database/raw/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite
# Using uv (recommended)
uv venv --python 3.11
source .venv/bin/activate # On Windows: .venv\Scripts\activate
uv pip install -e .
cp .env.example .env
# Edit .env and add your API keys
Required in .env:
ANTHROPIC_API_KEY=your_anthropic_api_key_here
Optional:
LANGCHAIN_TRACING_V2=true
LANGSMITH_ENDPOINT=https://api.smith.langchain.com
LANGCHAIN_API_KEY=your_langsmith_api_key_here
LANGCHAIN_PROJECT=text2sql-deepagent
Run the agent from the command line with a natural language question:
python agent.py "What are the top 5 best-selling artists?"
python agent.py "Which employee generated the most revenue by country?"
python agent.py "How many customers are from Canada?"
You can also use the agent in your Python code:
from agent import create_sql_deep_agent
# Create the agent
agent = create_sql_deep_agent()
# Ask a question
result = agent.invoke({
"messages": [{"role": "user", "content": "What are the top 5 best-selling artists?"}]
})
print(result["messages"][-1].content)
User Question
↓
Deep Agent (with planning)
├─ write_todos (plan the approach)
├─ SQL Tools
│ ├─ list_tables
│ ├─ get_schema
│ ├─ query_checker
│ └─ execute_query
├─ Filesystem Tools (optional)
│ ├─ ls
│ ├─ read_file
│ ├─ write_file
│ └─ edit_file
└─ Subagent Spawning (optional)
↓
SQLite Database (Chinook)
↓
Formatted Answer
Deep Agents uses progressive disclosure with memory files and skills:
AGENTS.md (always loaded) - Contains:
skills/ (loaded on-demand) - Specialized workflows:
The agent sees skill descriptions in its context but only loads the full SKILL.md instructions when it determines which skill is needed for the current task. This progressive disclosure pattern keeps context efficient while providing deep expertise when needed.
"How many customers are from Canada?"
The agent will directly query and return the count.
"Which employee generated the most revenue and from which countries?"
The agent will:
write_todos to plan the approachThe Deep Agent shows its reasoning process:
Question: Which employee generated the most revenue by country?
[Planning Step]
Using write_todos:
- [ ] List tables in database
- [ ] Examine Employee and Invoice schemas
- [ ] Plan multi-table JOIN query
- [ ] Execute and aggregate by employee and country
- [ ] Format results
[Execution Steps]
1. Listing tables...
2. Getting schema for: Employee, Invoice, InvoiceLine, Customer
3. Generating SQL query...
4. Executing query...
5. Formatting results...
[Final Answer]
Employee Jane Peacock (ID: 3) generated the most revenue...
Top countries: USA ($1000), Canada ($500)...
text-to-sql-agent/
├── agent.py # Core Deep Agent implementation with CLI
├── AGENTS.md # Agent identity and general instructions (always loaded)
├── skills/ # Specialized workflows (loaded on-demand)
│ ├── query-writing/
│ │ └── SKILL.md # SQL query writing workflow
│ └── schema-exploration/
│ └── SKILL.md # Database structure discovery workflow
├── chinook.db # Sample SQLite database (downloaded, gitignored)
├── pyproject.toml # Project configuration and dependencies
├── uv.lock # Locked dependency versions
├── .env.example # Environment variable template
├── .gitignore # Git ignore rules
├── text-to-sql-langsmith-trace.png # LangSmith trace example image
└── README.md # This file
All dependencies are specified in pyproject.toml:
.env file:LANGCHAIN_TRACING_V2=true
LANGSMITH_ENDPOINT=https://api.smith.langchain.com
LANGCHAIN_API_KEY=your_langsmith_api_key_here
LANGCHAIN_PROJECT=text2sql-deepagent
When configured, every query is automatically traced:
You can view:
View your traces at: https://smith.langchain.com/
MIT
Contributions are welcome! Please feel free to submit a Pull Request.