Skip to main content

SQL Agent Toolkit

This example shows how to load and use an agent with a SQL toolkit.


You'll need to first install typeorm:

npm install typeorm


npm install @langchain/openai
import { OpenAI } from "@langchain/openai";
import { SqlDatabase } from "langchain/sql_db";
import { createSqlAgent, SqlToolkit } from "langchain/agents/toolkits/sql";
import { DataSource } from "typeorm";

/** This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc.
* To set it up follow the instructions on, placing the .db file
* in the examples folder.
export const run = async () => {
const datasource = new DataSource({
type: "sqlite",
database: "Chinook.db",
const db = await SqlDatabase.fromDataSourceParams({
appDataSource: datasource,
const model = new OpenAI({ temperature: 0 });
const toolkit = new SqlToolkit(db, model);
const executor = createSqlAgent(model, toolkit);

const input = `List the total sales per country. Which country's customers spent the most?`;

console.log(`Executing with input "${input}"...`);

const result = await executor.invoke({ input });

console.log(`Got output ${result.output}`);

`Got intermediate steps ${JSON.stringify(

await datasource.destroy();

API Reference:

Was this page helpful?

You can also leave detailed feedback on GitHub.