Skip to main content
In this tutorial we will build a custom agent that can answer questions about a SQL database using LangGraph. LangChain offers built-in agent implementations, implemented using LangGraph primitives. If deeper customization is required, agents can be implemented directly in LangGraph. This guide demonstrates an example implementation of a SQL agent. For a practical introduction, see building a SQL agent using higher-level LangChain abstractions.
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.
The prebuilt agent lets us get started quickly, but we relied on the system prompt to constrain its behavior—for example, we instructed the agent to always start with the “list tables” tool, and to always run a query-checker tool before executing the query. We can enforce a higher degree of control in LangGraph by customizing the agent. Here, we implement a simple ReAct-agent setup, with dedicated nodes for specific tool-calls. We will use the same [state] as the prebuilt agent.

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.
We will use Python’s built-in sqlite3 module to interact with the database:

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 can implement database tools as thin wrappers using the @tool decorator from langchain.tools:

4. Define application steps

We construct dedicated nodes for the following steps:
  • Listing DB tables
  • Calling the “get schema” tool
  • Generating a query
  • Checking the query
Putting these steps in dedicated nodes lets us (1) force tool-calls when needed, and (2) customize the prompts associated with each step.

5. Implement the agent

We can now assemble these steps into a workflow using the Graph API. We define a conditional edge at the query generation step that will route to the query checker if a query is generated, or end if there are no tool calls present, such that the LLM has delivered a response to the query.
We visualize the application below:
SQL agent graph We can now invoke the graph:
See LangSmith trace for the above run.

6. 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. Here we leverage LangGraph’s human-in-the-loop features to pause the run before executing a SQL query and wait for human review. Using LangGraph’s persistence layer, we can pause the run indefinitely (or at least as long as the persistence layer is alive). Let’s wrap the sql_db_query tool in a node that receives human input. We can implement this using the interrupt function. Below, we allow for input to approve the tool call, edit its arguments, or provide user feedback.
The above implementation follows the tool interrupt example in the broader human-in-the-loop guide. Refer to that guide for details and alternatives.
Let’s now re-assemble our graph. We will replace the programmatic check with human review. Note that we now include a checkpointer; this is required to pause and resume the run.
We can invoke the graph as before. This time, execution is interrupted:
We can accept or edit the tool call using Command:
Refer to the human-in-the-loop guide for details.

Next steps

Check out the Evaluate a graph guide for evaluating LangGraph applications, including SQL agents like this one, using LangSmith.