Multi-Agent RAG Orchestration with Contextual AI and Gemini - n8n Workflow

Use this powerful n8n workflow to automatically deploy and orchestrate multiple RAG agents via Contextual AI and Gemini. This advanced n8n template leverages sophisticated n8n nodes, memory, and specialized tools, starting with the n8n trigger.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

AI Developers needing robust RAG orchestration.
Businesses aiming to automate support or research using multiple specialized knowledge bases.
n8n users looking for advanced examples of LangChain integration and custom tool usage.
Engineers wanting to deploy a dynamic, contextual AI system using an n8n trigger and n8n node logic.

Overview

Managing specialized knowledge across multiple Retrieval-Augmented Generation (RAG) agents can be challenging. This innovative n8n workflow solves this by providing a two-part solution: agent creation and agent orchestration. The first part uses an n8n trigger (Form) to define a new agent, upload documents, and wait for ingestion completion. The second part uses a Chat n8n trigger combined with an advanced Agent Orchestrator n8n node, powered by Gemini 2.5 Flash, to intelligently determine which Contextual AI agent holds the relevant information for a user query. This setup ensures grounded, context-aware responses without manual agent selection, making this n8n template highly valuable for scaling AI operations.

How it Works

This n8n workflow operates in two distinct phases:

Phase 1: Agent Creation and Document Ingestion


  1. Form Submission (n8n trigger): The process begins with the Submit Agent Information form n8n trigger, capturing the agent's name, description, datastore name, and binary files (documents).

  2. Preprocessing: A Code n8n node extracts the necessary binary keys for file handling.

  3. Agent Creation: The Create Agent Contextual AI n8n node creates the new agent and initiates document ingestion.

  4. Status Polling Loop: The workflow splits the results for each uploaded file (Split Out and Iterate over each files). It then enters a loop using the Wait n8n node (30-second delay) and the Get Document Ingestion Status n8n node. An If n8n node checks if the document status is 'completed'. If not, the loop repeats until all files are successfully ingested and the agent is ready for querying.

Phase 2: Query Orchestration


  1. Chat Input (n8n trigger): The Chat n8n trigger receives a user question.

  2. Agent Orchestrator: The core Agent Orchestrator n8n node takes the query. It is configured with a system message that directs it to use the List Agents Tool to identify available agents and the Query Agent Tool to interact with the selected agent.

  3. Model and Memory: The orchestrator utilizes the Google Gemini Chat Model as the foundational LLM and maintains context across the conversation using the Simple Memory n8n node (Buffer Window).

  4. Tool Execution: Based on the query's intent, the orchestrator selects the appropriate Contextual AI agent, executes the Query Agent Tool with the user's question, and returns the grounded answer.

Installation Guide

To deploy this comprehensive RAG multi-agent n8n workflow, follow these setup steps:


  1. Import the n8n template: Copy the provided JSON and import it directly into your n8n instance.

  2. Contextual AI Credentials: Sign up for a Contextual AI account and obtain your CONTEXTUALAIAPIKEY. Add this API key as a credential named 'Contextual AI account' in your n8n setup.

  3. Google Gemini Credentials: Obtain a Gemini API key. Configure a corresponding 'Google Gemini(PaLM) Api account' credential within n8n.

  4. Activate: Ensure the n8n workflow is set to active. The top path will create agents via the form interface, and the bottom path will enable chat querying via its dedicated webhook n8n trigger.

  5. Customize: You can customize the Submit Agent Information n8n trigger or swap the Form Trigger with a Webhook Trigger for seamless external integration.

Node Details

This n8n workflow utilizes several specialized n8n node components:

Submit Agent Information (Form Trigger): The initial n8n trigger, collecting agent configuration (Name, Description, Datastore) and required binary files.
Create Agent (ContextualAi n8n node): Creates the agent structure in Contextual AI and handles the initial document upload. Key configuration links agent details using expressions from the trigger data.
Wait (Wait n8n node): Implements a necessary 30-second pause to create a polling loop for checking asynchronous document ingestion status.
Get Document Ingestion Status (ContextualAi n8n node): Retrieves the status of document processing using the document ID and datastore ID, essential for the RAG setup.
Chat (ChatTrigger n8n trigger): The conversational n8n trigger that initiates the RAG query process.
Agent Orchestrator (Langchain Agent n8n node): The central logic n8n node. It dictates tool selection, utilizing the system prompt to guide dynamic agent choice based on user intent. It requires connections to the LLM, memory, and tools.
Google Gemini Chat Model (Langchain n8n node): Provides the LLM reasoning capability (Gemini 2.5 Flash) for the orchestrator to decide which agent to query.
Query Agent Tool & List Agents Tool (ContextualAi Tool n8n nodes): These are the callable functions used by the Agent Orchestrator to interact directly with the underlying Contextual AI agents and their RAG knowledge bases.

Related n8n Workflows

Free

Nodes: 13 Nodes
Updated: December 26 2025
View all
Created by
Jinash Rouniyar
Jinash Rouniyar

DevRel at Contextual AI

Featured*