Cost-Optimized Retrieval Augmented Generation (RAG) Setup - n8n Workflow

Use this sophisticated n8n workflow to deploy a high-performance, cost-optimized RAG solution. It leverages multiple OpenAI models to handle complex reasoning and vector store retrieval efficiently.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

AI developers looking for advanced n8n templates for RAG.
Users who need to implement custom knowledge bases using vector stores.
Anyone seeking to reduce operational costs associated with powerful LLMs like GPT-4 in production RAG systems.
Technical users deploying custom language models and AI agents via an n8n workflow.

Overview

This powerful n8n workflow demonstrates a crucial technique for cost optimization in Retrieval-Augmented Generation (RAG) applications. It is split into two logical flows: a Data Loading flow and a Retriever flow. The core value of this n8n template lies in its intelligent use of two separate OpenAI models. The initial AI Agent reasoning and tool selection utilize a more powerful, 'Expensive model' (e.g., GPT-4) for high-quality decision-making. However, the heavy lifting of answering the query based on retrieved documents—which is called frequently—is delegated to a 'Cheap Model' (e.g., GPT-4-mini or similar low-cost model). This strategic separation significantly reduces token consumption costs while maintaining high quality results for the entire n8n workflow. The flow starts with an n8n trigger for data upload and uses a separate n8n trigger for the chat interface.

How it Works

This n8n workflow operates in two distinct phases:


  1. Data Loading (RAG Setup):

The process starts with the Upload your file here n8n trigger, which is a Form Trigger that accepts PDF or CSV files.
The file data is passed to the Default Data Loader n8n node, which prepares the documents.
The Embeddings OpenAI n8n node generates vector representations (embeddings) for the loaded documents.
Finally, the Insert Data to Store n8n node stores these embeddings and corresponding documents into a temporary in-memory vector store, indexed by vectorstorekey.


  1. Retriever Flow (Q&A and Cost Optimization):

The workflow is initiated by the When chat message received n8n trigger, designed specifically for chat interfaces.
The input message is sent to the AI Agent n8n node. This agent uses the designated Expensive model for reasoning and deciding which tools to use.
The agent is provided with the Answer questions with a vector store tool, which is customized to access the local data.
When the agent decides to query the knowledge base, the tool executes using the Query Data Tool n8n node.
* Crucially, the Answer questions with a vector store tool is itself powered by the Cheap Model. This means the expensive n8n node handles the high-level reasoning, but the actual retrieval and text generation from the context uses the low-cost n8n model, delivering massive savings on per-query execution.

Installation Guide

To deploy this comprehensive n8n workflow, follow these steps:


  1. Import the n8n template: Copy the provided JSON and import it into your n8n instance via the 'Workflows' section, using the 'New' button and selecting 'Import from JSON'.

  2. Set up OpenAI Credentials: You must configure credentials for the OpenAI Service. Both the Embeddings OpenAI, Expensive model, and Cheap Model n8n nodes require an active OpenAI API key credential (ID '14' in the provided JSON).

  3. Run the Load Data Flow: Click 'Execute Workflow' to activate the Upload your file here n8n trigger and provide your document(s) (PDF or CSV).

  4. Run the Retriever Flow: Once data is loaded, open the chat interface associated with the When chat message received n8n trigger and start asking questions about the document content. The overall n8n workflow is now active and ready to use.

Node Details

Upload your file here (n8n trigger): A Form Trigger that initiates the data loading pipeline. It is configured to accept .pdf and .csv files.
Embeddings OpenAI: Generates high-dimensional vector embeddings for the documents using the OpenAI service, essential for RAG.
Insert Data to Store: A LangChain vectorStoreInMemory n8n node. This stores the document chunks and their embeddings in a temporary, in-memory location for quick retrieval using the key vectorstorekey.
When chat message received (n8n trigger): A Chat Trigger that listens for incoming messages, kicking off the Q&A process. This is the primary n8n trigger for the retrieval flow.
Expensive model (LangChain LM): Uses a powerful model (gpt-4.1 placeholder) to provide high-quality reasoning to the AI Agent for complex tool use and planning.
AI Agent: The central decision-making n8n node. It determines when and how to use the available tools, driven by the Expensive model.
Answer questions with a vector store (LangChain Tool): A custom tool exposed to the AI Agent. Crucially, this tool's internal retrieval and response generation uses the designated Cheap Model (gpt-4.1-mini placeholder), enabling the cost-saving strategy of this specific n8n workflow.
Query Data Tool: Retrieves relevant document snippets from the in-memory vector store based on the user query embedding.

Related n8n Workflows

Free

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

Product Manager at n8n - Responsible for the creator program

Featured*