By the end, you can chat with a shop database. You ask a question in plain words. An agent writes the SQL and shows it to you. It runs the SQL only after you say yes, and it can only read. Then it answers, with a chart when one helps.
- Data and backend engineers who want people to ask their database questions in plain words.
- People who know some SQL and some Python, and have called a model API once or twice.
- Not for you if you want a product to put in front of users today. This is a small agent you can read in one sitting. It has one user, one SQLite file and no sign-in.
- uv, which installs Python 3.11 and every package for you.
make, which comes with macOS and most Linux systems.- An Anthropic API key. Each data question makes three or four calls to a small model, plus one for each SQL fix.
- For the real data: a link to kaggle.com, and about 120 MB of disk for the files. The download is free.
- The kagglehub package can fetch a public data set like this one without a sign-in. If it asks you to sign in, follow the steps it prints.
- Docker, only if you want to run the app in a container.
- Clone the repo, install the packages and make your settings file.
git clone https://github.com/learnwithparam/text-to-sql-agentic-rag-chatbot.git
cd text-to-sql-agentic-rag-chatbot
make setupExpect: uv sync lists the packages it installed, then Created .env. Paste your ANTHROPIC_API_KEY into it.
- Open
.envand paste your key afterANTHROPIC_API_KEY=.
Expect: the line reads ANTHROPIC_API_KEY= with your key after it. Git never sees this file.
- Build the tiny sample database. It has the same tables as the real data, with a few rows each.
make sample-dataExpect: Built sample.db with 9 tables.
- Run the tests. They use a fake model, so they cost nothing.
make testExpect: OK after a line that says how many tests ran.
- Ask one question in the terminal, on the sample database.
DB_PATH=sample.db make ask Q="How many orders were delivered?"Expect: the SQL the agent wants to run, then Run it? [y/N]. Type y. Then you see SQL:, Answer: and the number of model calls with a rough cost.
- Get the real data. This step needs kaggle.com.
make setup-dataExpect: one copied line for each CSV file, then a line per table, then Built ecommerce.db with 9 tables.
- Start the chat app.
make runExpect: Your app is available at http://localhost:8000. Open it and ask a question. Each SQL query waits for you to click Approve or Reject.
- Walk through the lesson in the notebook.
make notebookExpect: JupyterLab opens multi-agent-chatbot.ipynb. It uses ecommerce.db if you built it, and sample.db if not.
To run the app in Docker instead, run make dev.
Expect: the same app at http://localhost:8000. The container is named text-to-sql-agentic-rag-chatbot.
Read the picture from left to right. The agent does each step in turn. The orange box is you. Nothing reaches the database until you say yes.
The diagram below is the same graph as it is in the code. Each box is a node in text2sql_agent.py.
flowchart LR
Q[Question] --> G[guardrails_agent]
G -->|greeting or off topic| E[End]
G -->|in scope| F[find_examples]
F --> S[sql_agent]
S --> A{approve_sql}
A -->|reject| E
A -->|approve| X[execute_sql]
X -->|error| R[error_agent]
R --> A
X -->|rows| N[analysis_agent]
N --> D[decide_graph_need]
D -->|chart helps| C[draw_chart]
D -->|no chart| E
C --> E
A few words you will see:
- Agent: a program that asks a model what to do next, step by step.
- RAG: retrieval augmented generation. You find useful text first and put it in the prompt. Here, that text is the checked questions and SQL in
checked_queries.json. They are found by keywords, with no embeddings. - Interrupt: a LangGraph call that pauses the graph and waits for a person.
- Checkpointer: the part that saves a paused graph, so it can go on later.
The real chat app. It shows the SQL and waits for your click before it touches the database.
The notebook builds the agent one step at a time. The app and the command line use the same agent from text2sql_agent.py.
| # | Lesson | Where | What you learn |
|---|---|---|---|
| 1 | The Knowledge Base: Database Schema | multi-agent-chatbot.ipynb |
Give the model your tables as text |
| 2 | Architecture: The State and the Agents | multi-agent-chatbot.ipynb |
Shared state, and replies with a fixed shape through schema= |
| 3 | Step 1: The Guardrails Agent | multi-agent-chatbot.ipynb |
Turn away off-topic questions and greet people |
| 4 | Step 2: Look Up Checked Examples | multi-agent-chatbot.ipynb, checked_queries.json |
Find good SQL by keyword and add it to the prompt |
| 5 | Step 3: The SQL Agent | multi-agent-chatbot.ipynb |
Write SQL from the question, the tables and the examples |
| 6 | Step 4: A Person Approves the SQL | multi-agent-chatbot.ipynb |
Pause with interrupt, resume with Command |
| 7 | Step 5: Run the SQL on a Read-Only Database | multi-agent-chatbot.ipynb |
Open SQLite with mode=ro, see a write fail, fix failed SQL |
| 8 | Step 6: Analysis and Visualization | multi-agent-chatbot.ipynb |
Explain the rows, and draw a chart without running model code |
| 9 | Building the Graph | multi-agent-chatbot.ipynb |
Join the steps with edges and a checkpointer |
| 10 | Let's Test It! | multi-agent-chatbot.ipynb |
Ask, approve, reject, and see the cost |
| 11 | The chat app | app.py |
Show each step live and add Approve and Reject buttons |
| 12 | The command line | text2sql_agent.py |
Ask one question, and approve in the terminal |
Files you will use:
llm.pyis the one file that calls a model. It holds the model names and counts the cost.sample_data.pybuildssample.db.download_data.pyanddb_init.pybuildecommerce.db.tests/test_agent.pychecks the agent with a fake model.
The model name lives in llm.py. The default is anthropic/claude-haiku-5-5. To use another model, set LITELLM_MODEL in .env and add the key for that provider. env.example has a commented line for each one.
LITELLM_MODEL=openrouter/nvidia/nemotron-3.5-lightning
OPENROUTER_API_KEY=<your key>Pick a model that supports structured output, since each step asks for a fixed shape. If the provider says no to the model or the key, llm.py stops with a short message that says so.
This agent reads one SQLite file. A team version would connect to a warehouse with a user that can only read. It would log each approved query, and load the checked examples from a store you can grow over time. The Agentic RAG Masterclass goes further.
