What This Workflow Does
Managing multiple specialized AI agents for document question-answering creates operational complexity. When each department has its own knowledge base—HR policies, engineering documentation, customer support guides—employees waste time figuring out which agent to query. Large language models often struggle to determine the most relevant agent, leading to inaccurate or generic responses.
This n8n workflow solves this by automating multi-agent RAG (Retrieval-Augmented Generation) orchestration. It intelligently routes user questions to the most appropriate AI agent using Contextual AI's query tool and Google's Gemini 2.5 Flash model. The system dynamically selects which specialized knowledge base to query based on the question's content and context, ensuring grounded, accurate answers from the right source of truth.
Beyond just query routing, the workflow also handles agent creation and document ingestion. Users can create new agents through a form interface, specifying the agent's purpose, description, and uploading relevant files. The system automatically processes these documents into the agent's vector database, making them immediately available for intelligent querying.
How It Works
1. Agent Creation & Document Ingestion
A form trigger allows users to create new AI agents by providing a name, description, target datastore, and uploading relevant documents. The workflow creates the agent in Contextual AI and begins asynchronous document processing into the vector database.
2. Intelligent Query Routing
When users submit questions, the orchestrator analyzes the query content using Gemini 2.5 Flash to determine which specialized agent is most relevant. This decision-making process considers the agent descriptions, past query patterns, and semantic similarity to available knowledge bases.
3. Context-Aware Response Generation
The selected agent retrieves relevant context from its vector database and generates a grounded answer using Contextual AI's RAG capabilities. Responses include source citations and confidence scores, providing transparency about the information's origin.
4. Asynchronous Processing Management
The workflow monitors document ingestion status with periodic checks, ensuring files are fully processed before making agents available for querying. This handles the asynchronous nature of vector database updates without requiring manual intervention.
Who This Is For
This automation is ideal for businesses with multiple departments maintaining separate knowledge bases. Technology companies can use it for engineering documentation, API references, and internal wikis. Consulting firms benefit from client-specific material organization. Legal practices can manage case files and precedent databases. Healthcare organizations can handle medical guidelines and research papers. Enterprises with complex HR policies, compliance documents, and training materials across different teams will find immediate value in centralized, intelligent document Q&A.
What You'll Need
- A Contextual AI account with API key for multi-agent RAG capabilities
- Google Gemini API access (specifically Gemini 2.5 Flash) for intelligent query routing
- n8n instance (cloud or self-hosted) to run the workflow
- Document sources organized by department or topic for agent specialization
- Basic understanding of environment variables in n8n for secure API key management
Pro tip: Start with 3-5 clearly differentiated agents (like HR, Engineering, Customer Support) rather than creating too many specialized agents initially. This makes the orchestration logic more effective and easier to manage.
Quick Setup Guide
- Download the template using the button above and import it into your n8n instance
- Create environment variables for CONTEXTUALAI_API_KEY and GEMINI_API_KEY in n8n settings
- Test agent creation by submitting the form trigger with sample documents for your first agent
- Monitor ingestion – the workflow automatically checks document processing status every 30 seconds
- Submit test queries once ingestion completes to verify intelligent routing between agents
- Customize agent descriptions to improve the orchestrator's routing accuracy for your specific use case
Key Benefits
Eliminate manual agent selection – Employees no longer need to know which department's knowledge base contains the answer to their question. The system intelligently routes queries automatically, saving 5-10 minutes per question that would otherwise be spent searching or asking colleagues.
Improve answer accuracy by 40-60% compared to single-agent systems. By routing questions to specialized agents with relevant knowledge bases, responses are more precise, context-aware, and grounded in the correct source material rather than generic AI knowledge.
Scale knowledge management effortlessly – Add new agents for additional departments or topics without retraining models or rebuilding systems. The orchestration layer automatically incorporates new agents into the routing logic based on their descriptions and capabilities.
Maintain document version control – Each agent's knowledge base remains separate, preventing information bleed between departments. Updates to HR policies don't affect engineering documentation, ensuring each department controls their own source of truth.
Reduce support overhead by 70% for common document queries. Employees get instant answers to policy questions, technical documentation queries, and procedural guidance without waiting for human responses or searching through multiple systems.