I Built the ULTIMATE n8n RAG AI Agent Template
Cole Medin demonstrates how to build an advanced Agentic RAG agent in n8n that overcomes traditional RAG limitations by intelligently switching between different tools to explore knowledge bases.
Key Insights
0:00
Shortcomings of RAG
“Retrieval augmented generation is the most popular tool to give AI agents access to your knowledge base, essentially making them domain experts for your documents. But I'm going to be honest, a lot of times RAG sucks.”

Cole begins by introducing Retrieval Augmented Generation (RAG) as the most popular method for giving AI agents access to knowledge bases. While acknowledging its widespread adoption and ease of implementation in no-code tools like n8n, he immediately highlights its significant limitations.

The main shortcomings identified include: RAG's inability to properly analyze trends in spreadsheets when it only retrieves fragments of tables; difficulty in finding specific documents when context is in metadata (like dates); failure to connect information across different documents; inability to "zoom out" to see entire documents; and lack of proper data analysis capabilities. These limitations form the foundation for why an improved approach is needed.

Takeaways
  • Traditional RAG is widely adopted but has serious limitations in practical applications
  • RAG struggles with tabular data analysis as it typically retrieves only fragments of tables
  • Context from document metadata (like dates) is often missed in standard RAG implementations
  • RAG fails to connect information across documents and provide broader context
  • Standard RAG can't dynamically switch between document lookup and data analysis
1:32
Introducing the n8n Agentic RAG Template
“All agentic RAG is is giving agents the ability to reason about how they explore the knowledge base instead of just giving it a single tool. It also includes agents being able to improve the RAG lookup queries and choose different tools to answer different user questions.”

Cole introduces his solution to RAG limitations: Agentic RAG. He shows a bird's-eye view of his n8n workflow template, which appears complex but contains all the necessary components for an effective Agentic RAG system. He compares this to a previous, simpler implementation he'd created that relied solely on RAG lookups.

The key advancement in this template is the inclusion of multiple tools beyond basic RAG lookup. The agent now has several PostgreSQL tools that enable different ways of exploring the knowledge base: listing all available documents, retrieving entire file contents for specific documents, and directly querying tabular data using SQL. This gives the agent flexibility to choose the most appropriate method based on the user's question.

Takeaways
  • Agentic RAG gives AI agents multiple tools to explore knowledge bases beyond simple vector lookups
  • The workflow template includes tools for RAG lookups, document listing, full content retrieval, and SQL queries
  • The agent can reason about which method is most appropriate for answering different types of questions
  • Previous RAG implementations were limited by having only a single lookup tool available
  • The template is available for download and immediate use in n8n
5:14
Agentic RAG Agent Demo
“It used the tool to write a SQL query... and that is the right answer. So we got everything back.”

Cole demonstrates the capabilities of his Agentic RAG agent by testing it with several questions that highlight its ability to choose different tools based on the nature of the query. He shows how the agent handles both tabular data and text documents using the most appropriate tools for each situation.

In the first example, when asked "which month did we get the most new customers," the agent automatically writes a SQL query to analyze the revenue metrics spreadsheet rather than relying on RAG. In another example, it appropriately uses RAG to extract information about areas for improvement from a text document. Finally, Cole demonstrates how the agent can retrieve entire file contents when needed and provide source citations with clickable links to the original documents.

Takeaways
  • The agent intelligently chooses SQL queries for numerical analysis of spreadsheet data
  • For text-based information, the agent appropriately uses RAG lookups
  • When necessary, the agent can retrieve and analyze entire documents
  • The implementation includes source citation capabilities with clickable links
  • The agent can handle different question types without explicit instructions about which tool to use
9:28
Unstract
“Unstract is an open-source no-code LLM platform to create APIs and ETL pipelines to turn unstructured documents into structured data.”

This section features a sponsorship segment about Unstract, an open-source tool for handling unstructured data. Cole explains that Unstract helps transform complex document types into structured data, which is particularly valuable for RAG implementations that need to work with diverse document formats.

Unstract consists of three main components: a Prompt Studio for engineering prompts that extract information from unstructured documents, Workflows for building automated extraction processes, and deployment options for creating data APIs and ETL pipelines. Cole demonstrates how easily Unstract can extract structured information from documents like receipts, making it a valuable complement to RAG systems dealing with complex document types.

Takeaways
  • Unstract is an open-source tool for converting unstructured documents into structured data
  • It features a Prompt Studio for engineering extraction prompts, Workflows for automation, and deployment options
  • The tool is particularly useful for RAG implementations that need to process complex document types
  • Unstract can extract structured information from documents like receipts that standard text extraction wouldn't handle well
11:30
Setting up Supabase for Our Agent
“You need to use the transaction pooler method for connecting to Postgres. This will save you a huge headache, by the way.”

Cole explains the database setup required for the Agentic RAG agent, focusing on creating three essential tables in Supabase (PostgreSQL). He walks through the SQL statements that create each table and explains their purpose in the overall system.

The three tables include: (1) a documents table that stores embeddings for RAG, metadata, and the contents of each chunk; (2) a document_metadata table for high-level information about documents including titles, URLs for source citation, and schema information for tabular data; and (3) a document_rows table that stores individual rows from spreadsheets in a JSONB format that allows SQL querying without creating separate tables for each file. Cole also provides a critical tip about using the transaction pooler connection method for Supabase to avoid connection issues.

Takeaways
  • The system requires three Supabase tables: documents, document_metadata, and document_rows
  • The documents table stores embeddings, metadata, and content chunks for RAG
  • The document_metadata table contains high-level information and schema definitions for tabular data
  • The document_rows table uses JSONB to store spreadsheet rows in a queryable format
  • When connecting to Supabase, the transaction pooler method must be used (port 6543) to avoid connection issues
14:23
RAG Pipeline Deep Dive
“The most Surefire way that's generally recommended is just to delete everything. So we're deleting all of the document rows specifically for this file ID.”

Cole provides a detailed walkthrough of the RAG pipeline that processes documents from Google Drive into the Supabase knowledge base. The workflow begins with Google Drive triggers that detect new or updated files, then processes them through a loop to handle multiple files that might be updated simultaneously. For each file, the pipeline extracts content differently based on file type, with special handling for tabular data.

A key improvement in this pipeline is the complete deletion of existing data for a file before inserting updated content. This ensures no outdated chunks remain in the knowledge base when documents are modified. For tabular data (CSV/Excel files), the pipeline performs dual processing: it converts tables to text for RAG lookups while also storing structured data in the document_rows table for SQL querying. The pipeline also extracts schema information from spreadsheets to enable proper SQL queries later.

Takeaways
  • The pipeline detects new and updated files in Google Drive using triggers
  • A loop structure handles multiple files that may be updated simultaneously
  • Before updating, all existing data for a file is deleted to prevent outdated information
  • Different file types are processed through specialized extraction paths
  • Tabular data is dual-processed: as text chunks for RAG and as structured data for SQL querying
26:26
Building Our Agent and its RAG Tools
“The AI decides the file ID, so it picks that out from the metadata table and then passes that into this tool. You'll see the agent every single time that calls get file contents, it always called list documents first.”

This section focuses on building the agent itself and configuring its tools for exploring the knowledge base. Cole explains the agent setup, which includes both webhook and chat triggers to enable API and direct interaction. The agent uses GPT-4o Mini (though Cole notes this can be upgraded) and is configured with a system prompt that guides its use of the various tools.

The four key tools provided to the agent include: (1) a RAG lookup tool that returns both content matches and metadata for source citation; (2) a document listing tool that retrieves all available documents with their IDs and metadata; (3) a file content retrieval tool that gets the full text of specific documents; and (4) a SQL query tool for analyzing tabular data. Cole explains how the agent reasons through tool selection, typically starting with RAG and progressing to other tools if needed. The SQL query tool is the most complex, requiring specific instructions on how to query the JSONB structure in the document_rows table.

Takeaways
  • The agent can be triggered via webhook (API) or directly through n8n's chat interface
  • Four tools give the agent different ways to explore the knowledge base: RAG, document listing, content retrieval, and SQL
  • The system prompt instructs the agent to begin with RAG before using other tools
  • The agent maintains conversation history in a PostgreSQL table
  • The SQL query tool requires detailed instructions to help the agent understand how to query the JSONB structure
32:36
Last Demo
“I can say 'what employees are at the company' just something super generic. In this case it performed RAG and decided that it didn't get what it needed so decided to list a few of the documents as well.”

Cole performs a final demonstration of the agent in action, asking it to identify employees at the company. This example effectively showcases the agent's ability to switch between different tools when one approach doesn't yield results.

The demo shows the agent first attempting a RAG lookup, determining that it didn't find what it needed, then listing available documents to identify which might contain employee information. After identifying the product team meeting minutes as a likely source, it retrieves the full content of that document to answer the question. Cole notes that this spontaneous example worked perfectly to demonstrate the key advantage of Agentic RAG: the ability to seamlessly switch between different knowledge retrieval methods when one approach fails.

Takeaways
  • The agent first attempts RAG lookups before trying other methods
  • When RAG fails, the agent can list documents and select the most relevant ones
  • The agent can retrieve full document contents when needed
  • The seamless switching between tools demonstrates the core advantage of Agentic RAG
  • No user intervention is needed to guide the agent in selecting different tools
33:29
Outro
“I hope that this template can get you started super fast with Agentic RAG in n8n. A completely local version of this Agentic RAG agent built with the LocalAI package is coming soon.”

In the closing segment, Cole summarizes the value of the template he's created and hints at future content. He emphasizes that while the template is advanced, it provides a solid starting point for implementing Agentic RAG in n8n, and encourages viewers to ask questions in the comments if they encounter issues while building out the workflow.

Cole also announces an upcoming video featuring a completely local version of the Agentic RAG agent built with the LocalAI package, suggesting that those interested in self-hosted solutions should stay tuned. He concludes by asking viewers to like and subscribe if they found the content valuable and are looking forward to more content on AI agents and n8n.

Takeaways
  • The template is available for immediate use but may require customization for specific use cases
  • Viewers are encouraged to ask questions in the comments about implementing the workflow
  • A local version using the LocalAI package is coming in a future video
  • The template represents an advanced implementation of RAG concepts in n8n
Conclusion

The Agentic RAG approach demonstrated in this video represents a significant evolution in how AI agents interact with knowledge bases. By moving beyond simple vector lookups to a multi-tool approach, Cole's template enables more reliable, versatile, and intelligent information retrieval that overcomes many limitations of traditional RAG implementations.

The true power of this approach lies in its adaptability. When faced with tabular data, the agent can perform precise SQL queries instead of relying on fragmented text chunks. When context from an entire document is needed, it can retrieve and analyze complete files. When documents need to be selected based on metadata, it can browse and reason about the available options. This flexibility results in a much more capable knowledge assistant that can handle a diverse range of queries with greater accuracy.

So what? For developers and no-code builders working with AI agents, this template provides not just a ready-to-use solution but a framework for thinking about knowledge retrieval in a more sophisticated way. Rather than forcing all queries through a single retrieval method, the Agentic RAG approach encourages us to give our AI agents a toolbox of complementary methods and the reasoning ability to select the right tool for each job. This paradigm shift—from rigid, single-path knowledge retrieval to flexible, multi-tool exploration—represents the future of building truly helpful AI assistants that can work effectively with diverse knowledge bases.