Conversational AI Agent for SQLite Database Queries with Memory - n8n Workflow

Use this powerful n8n workflow to build a LangChain SQL AI Agent that interacts conversationally with a SQLite database. Leverage OpenAI for complex data queries using this crucial n8n template.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Data Scientists and Analysts seeking natural language access to SQL data.

  • Developers interested in deploying sophisticated, stateful AI agents using n8n.

  • Users looking for advanced n8n templates integrating LangChain with local databases.

  • Anyone needing a robust n8n workflow to handle complex, multi-step data queries.

Overview

Interacting with databases often requires writing specific SQL queries, which can be time-consuming. This powerful n8n workflow solves this by deploying an intelligent LangChain SQL Agent. This agent allows users to ask complex questions about their SQLite database using plain English. The n8n node setup handles the entire lifecycle: downloading a sample database (chinook.db), loading it into the execution context, and passing the user input to the AI Agent. Because this n8n template utilizes a memory component, the AI Agent can maintain context across multiple turns in the conversation, making the interaction intuitive and stateful. This specific n8n workflow is highly valuable for building customer-facing tools or internal analytics portals, leveraging the reliability of the n8n platform.

How it Works

This n8n workflow operates in two distinct phases: a one-time setup phase and the continuous interaction phase.

Setup Phase (One-Time Execution)


  1. The workflow begins with the When clicking "Test workflow" manual n8n trigger, initiating the setup.

  2. The Get chinook.zip example n8n node downloads the sample chinook.zip file.

  3. The Extract zip file n8n node decompresses the archive.

  4. The Save chinook.db locally n8n node saves the extracted SQLite database file locally on the n8n instance.

Interaction Phase (Per Chat Message)


  1. The flow is initiated by the Chat Trigger n8n trigger, which captures the user's conversational input (the question).

  2. The Load local chinook.db n8n node loads the local database file into memory.

  3. The Combine chat input with the binary n8n node combines the text input (from the Chat Trigger) with the binary database file data. This ensures the AI Agent has both the question and the data source.

  4. The core of the n8n workflow is the AI Agent (LangChain SQL Agent). This n8n node takes the input and uses the linked OpenAI Chat Model (configured to use GPT-4 Turbo) to determine the best sequence of SQL queries needed to answer the question.

  5. The Window Buffer Memory n8n node ensures that the agent remembers previous turns in the conversation, allowing for follow-up questions.

  6. The AI Agent executes the necessary queries against the loaded database and returns the final, conversational answer back to the user via the Chat Trigger interface. This robust combination of n8n node types provides a seamless experience.

Installation Guide

To use this n8n workflow, follow these steps:


  1. Import the n8n template: Copy the provided JSON code and import it into your n8n instance using the 'New' > 'Import from JSON' option.

  2. Configure Credentials: You must configure the OpenAI Chat Model n8n node with valid OpenAI API credentials that have access to gpt-4-turbo.

  3. Run Setup: Activate the n8n workflow and execute the initial setup path by clicking the 'Test Workflow' button connected to the When clicking "Test workflow" n8n trigger. This downloads and saves the chinook.db file.

  4. Start Chatting: Once the setup is complete, you can use the Chat Trigger URL provided in the Chat Trigger n8n node's settings (or the built-in chat UI in n8n) to begin asking questions to your database. This completes the setup of this complex n8n workflow.

Node Details

This advanced n8n workflow relies on several specialized n8n node types:

Chat Trigger (n8n trigger):
Function: Serves as the interactive starting point for the conversational flow, receiving user messages.
Key Configuration: Provides a webhook endpoint for real-time interaction.

Read/Write File (n8n node):
Function: Used both to save the database file locally (setup phase) and later to Load local chinook.db for every conversation turn.
Key Configuration: Reads/Writes to the local path ./chinook.db.

Set (n8n node - Combine chat input with the binary):
Function: Merges the conversational text input from the Chat Trigger with the binary database file data required by the SQL agent.
Key Configuration: Includes binary data and uses an expression (={{ $('Chat Trigger').item.json }}) to pull in the chat payload.

AI Agent (n8n node):
Function: The core intelligence of the n8n workflow. It is configured as a LangChain SQL Agent, translating natural language into SQL queries.
Key Configuration: Agent type set to sqlAgent, Data Source set to sqlite.

OpenAI Chat Model (n8n node):
Function: Provides the Large Language Model capabilities for the AI Agent (the reasoning engine).
Key Configuration: Model set to gpt-4-turbo, Temperature set to 0.
3.

Window Buffer Memory (n8n node):
Function: Stores the history of the conversation (inputs and outputs) so the AI Agent maintains context, crucial for making this an effective n8n workflow.
Key Configuration: Context Window Length set to 10.

Related n8n Workflows

Free

Nodes: 10 Nodes
Updated: December 26 2025
View all
Created by

Let's connect!

Featured*