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

The following tutorial covers the following concepts:

Setup

1

Install dependencies

2

Set up LangSmith

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

Build your SQL agent

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

The following database tools are minimal wrappers for demonstration purposes only. They are not intended to be secure or used in production. Use narrowly scoped database permissions and add application-specific validation before executing model-generated SQL.
We will use the sqlite3 library to query the database and fetch schemas:
4

Create the agent

Before running the command, do a check to check the LLM generated command in _safe_sql:
Then, execute commands with an execute_sql tool:
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:
5

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.
6

(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:
Create a file sqlAgent.ts and insert this:
7

Implement human-in-the-loop review

It can be prudent to check the agent’s SQL queries before they are executed for any unintended actions or inefficiencies.LangChain agents feature support for built-in human-in-the-loop middleware to add oversight to agent tool calls. Let’s configure the agent to pause for human review on calling the execute_sql tool:
We’ve added a checkpointer to our agent to allow execution to be paused and resumed. See the human-in-the-loop guide for detalis on this as well as available middleware configurations.
On running the agent, it will now pause for review before executing the execute_sql tool:
We can resume execution, in this case accepting the query, using Command:
Refer to the human-in-the-loop guide for details.

Next steps

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