Skip to main content

Overview

In this tutorial, you will learn how to build an agent that can answer questions about a SQL database using LangChain agents. At a high level, the agent will:
1

Fetch the available tables and schemas from the database

2

Decide which tables are relevant to the question

3

Fetch the schemas for the relevant tables

4

Generate a query based on the question and information from the schemas

5

Double-check the query for common mistakes using an LLM

6

Execute the query and return the results

7

Correct mistakes surfaced by the database engine until the query is successful

8

Formulate a response based on the results

Building Q&A systems of SQL databases requires executing model-generated SQL queries. There are inherent risks in doing this. Make sure that your database connection permissions are always scoped as narrowly as possible for your agent’s needs. This will mitigate, though not eliminate, the risks of building a model-driven system.

Concepts

We will cover the following concepts:

Setup

Installation

LangSmith

Set up LangSmith to inspect what is happening inside your chain or agent. Then set the following environment variables:

1. Select an LLM

Select a model that supports tool-calling:
👉 Read the OpenAI chat model integration docs
The output shown in the examples below used OpenAI.

2. Configure the database

You will be creating a SQLite database for this tutorial. SQLite is a lightweight database that is easy to set up and use. We will be loading the chinook database, which is a sample database that represents a digital media store. For convenience, we have hosted the database (Chinook.db) on a public GCS bucket.

3. Add tools for database interactions

Use the SqlDatabase wrapper available in the langchain/sql_db to interact with the database. The wrapper provides a simple interface to execute SQL queries and fetch results:

4. Execute SQL queries

Before running the command, do a check to check the LLM generated command in _safe_sql:
Then, use run from SQLDatabase to execute commands with an execute_sql tool:

5. Use createAgent

Use createAgent to build a ReAct agent with minimal code. The agent will interpret the request and generate a SQL command. The tools will check the command for safety and then try to execute the command. If the command has an error, the error message is returned to the model. The model can then examine the original request and the new error message and generate a new command. This can continue until the LLM generates the command successfully or reaches an end count. This pattern of providing a model with feedback - error messages in this case - is very powerful. Initialize the agent with a descriptive system prompt to customize its behavior:
Now, create an agent with the model, tools, and prompt:

6. Run the agent

Run the agent on a sample query and observe its behavior:
The agent correctly wrote a query, checked the query, and ran it to inform its final response.
You can inspect all aspects of the above run, including steps taken, tools invoked, what prompts were seen by the LLM, and more in the LangSmith trace.

(Optional) Use Studio

Studio provides a “client side” loop as well as memory so you can run this as a chat interface and query the database. You can ask questions like “Tell me the scheme of the database” or “Show me the invoices for the 5 top customers”. You will see the SQL command that is generated and the resulting output. The details of how to get that started are below.
In addition to the previously mentioned packages, you will need to:
In directory you will run in, you will need a langgraph.json file with the following contents:

Next steps

For deeper customization, check out this tutorial for implementing a SQL agent directly using LangGraph primitives.