Skip to main content

SQL Agent Toolkit

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

import { OpenAI } from "langchain";
import { SqlDatabase } from "langchain/sql_db";
import { createSqlAgent, SqlToolkit } from "langchain/agents";
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 tookit = new SqlToolkit(db);
const model = new OpenAI({ temperature: 0 });
const executor = createSqlAgent(model, tookit);

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

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

const result = await{ input });

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

`Got intermediate steps ${JSON.stringify(

await datasource.destroy();