Multi-Agent RAG System

n8n Building Code RAG

A sophisticated multi-agent Retrieval-Augmented Generation system for the New Zealand Building Code, built entirely in n8n. Each specialized agent handles specific code sections with dedicated vector stores, while an orchestration layer intelligently routes queries for accurate, code-compliant responses.

n8n Multi-Agent RAG System

Project Overview

This portfolio project demonstrates a production-grade multi-agent RAG system specifically designed for the New Zealand Building Code. Built entirely within n8n's visual workflow environment, it showcases advanced knowledge retrieval architecture using specialized agents, each responsible for different code sections (B, C, D, … H).

The system features intelligent document ingestion with OCR processing, vector embeddings for semantic search, and an orchestration layer that routes user queries to the most appropriate specialist agent. This approach ensures accurate, contextual responses while maintaining separation of concerns across different regulatory domains.

Key Capabilities

  • Multi-Agent Architecture – Specialized agents for each Building Code section with dedicated knowledge bases and prompt-engineered expertise
  • Intelligent Query Routing – Orchestration agent with custom prompt engineering determines the most appropriate specialist for each query
  • Advanced Image Ingestion – Image processing that reformats text and images with annotations for vector storage, enabling LLM to render relevant images in chat responses
  • Automated Document Processing – OCR-powered ingestion pipeline with text and image extraction, maintaining visual context
  • Vector Search & Retrieval – Semantic search using OpenAI embeddings and Supabase vector storage for both text and image content
  • Real-Time Updates – Google Drive integration for automatic document synchronization
  • Contextual Memory – PostgreSQL-backed conversation memory for follow-up queries
  • Prompt-Engineered Agents – All agents including orchestration are specifically prompt-engineered for their specialized tasks

System Architecture

The system employs a sophisticated multi-layered architecture with specialized components for document ingestion, vector storage, and intelligent query routing:

1

Document Ingestion

Google Drive triggers detect new PDFs, triggering parallel OCR processing via Mistral.ai for text extraction and image annotation with bounding boxes.

2

Content Processing

Pages are split and processed, with text chunked using RecursiveCharacterTextSplitter and images uploaded to Supabase Storage with metadata preservation.

3

Vector Embedding

OpenAI embeddings are generated for both text chunks and image captions, stored in clause-specific Supabase vector tables for efficient retrieval.

4

Query Orchestration

The orchestration workflow analyzes user queries and routes them to appropriate clause agents using GPT-4.1 with conversation context preservation.

5

Specialized Retrieval

Each clause agent performs semantic search within its dedicated vector store, retrieving the most relevant chunks for accurate, contextual responses.

6

Response Generation

Retrieved context is synthesized by the specialist agent's OpenAI model to generate comprehensive, Building Code-compliant answers.

Key Features

Multi-Agent System

Specialized agents for each Building Code section (B-H) with dedicated vector stores, ensuring focused expertise and preventing topic cross-contamination.

Prompt-Engineered Agents

All agents including the orchestration layer are specifically prompt-engineered for their specialized tasks, ensuring precise, domain-expert responses with optimal reasoning patterns.

Advanced Image Ingestion

Sophisticated image processing that reformats text and images with annotations for vector storage, enabling the LLM to render relevant images directly in chat responses with full context preservation.

Vector Search

OpenAI embeddings with Supabase vector storage enable semantic search across both textual content and image descriptions for comprehensive retrieval.

Intelligent Query Routing

GPT-4.1 orchestration agent with custom prompt engineering analyzes query intent and routes to the most appropriate specialist, ensuring accurate domain-specific responses.

Conversation Memory

PostgreSQL-backed chat memory maintains context across interactions, enabling natural follow-up questions and complex query sequences.

n8n Workflow Examples

Explore the actual n8n workflows that power this multi-agent RAG system

Complete RAG Ingestion + Multi-Agent System

Comprehensive n8n workflows covering document ingestion, vector processing, and intelligent query routing across specialized Building Code agents

Multiple
Complex Workflows
Advanced
Processing Nodes
Dedicated
Vector Stores

Advanced Data Ingestion Workflows CORE FEATURE

Sophisticated multi-modal document processing pipelines designed for enterprise-grade knowledge extraction and vector storage across all Building Code sections.

Multi-Pipeline Architecture

Multi-Modal
Processing
Image-Ready
Vector Storage
Real-time
Processing

Advanced Image Ingestion & OCR Pipeline

Multi-modal document processing with image annotation and LLM-ready formatting

Processing Flow
1. Multi-Modal Document Detection
Google Drive triggers monitor code-specific folders for PDF/image uploads with intelligent pre-processing
2. Advanced Image Ingestion & OCR
Mistral OCR with image reformatting, text/image annotation, and vector storage optimization
3. Intelligent Chunking
Context-aware text splitting with optimized chunk sizes and semantic overlap preservation
4. Vector Embedding
OpenAI text-embedding-3-large generates high-dimensional semantic vectors for search
5. Supabase Storage
Section-specific vector stores with optimized indexing and similarity search capabilities
Technical Specifications
  • OCR Engine: Mistral-OCR-Latest
  • Supported Formats: PDF, Images
  • Image Annotation: Full Metadata Preservation
  • LLM Integration: Image Rendering in Chat
Error Handling & Resilience
  • Health Monitoring: Real-time Alerts
  • Rollback Capability: Version Control

Intelligent Text Processing Pipeline

Advanced semantic processing for structured documents

Semantic Processing Features
Context-Aware Chunking

LangChain RecursiveCharacterTextSplitter preserves semantic meaning across chunk boundaries

Metadata Enrichment

Automatic extraction of document structure, section headers, and regulatory references

Quality Validation

Content filtering, duplicate detection, and relevance scoring before storage

Vector Storage Optimization
Section-Specific Stores

Separate vector databases for each code section enabling optimal query performance

Hybrid Search

Combines semantic similarity with keyword matching for precise retrieval

Performance Tuning

Custom indexing strategies and query optimization for ultra-fast response times

Advanced Multi-Agent Orchestration System CORE FEATURE

Intelligent query routing and coordination system managing specialized Building Code agents with advanced memory management and context preservation.

Orchestration Architecture

Multi-Agent
Specialist Network
Fast Routing
Decision Making
High Accuracy
Routing Precision
Prompt-Tuned
Agent System

Central Orchestration Agent

Prompt-engineered AI system for query analysis and intelligent routing with specialized task optimization

Query Analysis Engine
Intent Recognition

Advanced NLP analysis identifies query intent, topic domain, and complexity level using GPT-4.1

Context Extraction

Extracts Building Code section references, regulatory keywords, and cross-domain dependencies

Multi-Agent Coordination

Orchestrates parallel agent queries for complex cross-sectional regulatory questions with prompt-engineered coordination logic

Intelligent Routing Logic
Response Synthesis

Aggregates multi-agent responses with conflict resolution and coherence validation

Technical Specifications
  • LLM Engine: GPT-4.1 (LLM of Choice)
  • Memory System: PostgreSQL Chat Memory
  • Prompt Engineering: Task-Specific Optimization
  • Agent Specialization: Custom Prompt Templates
Performance Capabilities
  • Response Time: Fast Processing

Advanced Memory Management

Persistent context and conversation state management

PostgreSQL Chat Memory
Session Tracking

Maintains conversation context across multiple queries with automatic session management

Context Compression

Intelligent summarization of long conversations to maintain relevant context within token limits

Temporal Context Awareness
Conversation Flow

Tracks question progression and maintains semantic coherence across related queries

Specialist Agent Network

Code B Agent

Stability & Durability

Specialized in structural integrity, load calculations, seismic requirements, and material durability standards across construction phases.

Vector Store GPT-4.1
Code C Agent

Protection from Fire

Expert in fire safety systems, evacuation planning, sprinkler requirements, and passive fire protection across all building types.

Vector Store GPT-4.1
Code D Agent

Access Routes

Specializes in accessibility compliance, universal design principles, lift requirements, and barrier-free access solutions.

Vector Store GPT-4.1
Code E Agent

Moisture Management

Expert in waterproofing systems, vapor barriers, drainage solutions, and moisture control across building envelopes.

Vector Store GPT-4.1
Code F Agent

Safety of Users

Specializes in occupational safety, hazard mitigation, fall protection, and emergency response systems for building users.

Vector Store GPT-4.1
Code G Agent

Services & Facilities

Expert in HVAC systems, plumbing standards, electrical safety, and building services integration across all facility types.

Vector Store GPT-4.1
Code H Agent

Energy Efficiency

Specializes in thermal performance, insulation standards, energy modeling, and sustainable building practices for residential projects.

Vector Store GPT-4.1

Interactive n8n Workflow Previews

View-only visual representations of the actual n8n workflows powering this RAG system

Code Orchestration Agent Workflow

Complete multi-agent orchestration system with 7 specialized Building Code agents

Use mouse wheel to zoom, drag to pan
graph TD A["🔔 Chat Trigger
When message received
Webhook endpoint"] --> ORCH["🧠 Orchestration Agent
GPT-4.1 Main Controller
Query analysis & routing"] B["🤖 OpenAI GPT-4.1
Language Model
Temperature: 0.4"] --> ORCH C["💾 PostgreSQL Memory
Chat History Storage
Table: orch_agent_history"] --> ORCH ORCH --> D["🏗️ Code-B Tool
Stability & Durability
Structural integrity, loads,
seismic, materials"] ORCH --> E["🔥 Code-C Tool
Protection from Fire
Fire prevention, spread,
evacuation, firefighting"] ORCH --> F["♿ Code-D Tool
Access Routes
Accessibility, movement,
disabilities, lifts"] ORCH --> G["💧 Code-E Tool
Moisture Management
Surface water, external,
internal moisture"] ORCH --> H["🛡️ Code-F Tool
Safety of Users
Hazards, falling, warnings,
emergency systems"] ORCH --> I["🔧 Code-G Tool
Services & Facilities
Hygiene, ventilation,
utilities, lighting"] ORCH --> J["🌡️ Code-H Tool
Energy Efficiency
Thermal resistance,
HVAC, hot water"] D --> D1["🔍 Code-B Agent
OpenAI GPT-4.1
Specialized prompts"] E --> E1["🔍 Code-C Agent
OpenAI GPT-4.1
Specialized prompts"] F --> F1["🔍 Code-D Agent
OpenAI GPT-4.1
Specialized prompts"] G --> G1["🔍 Code-E Agent
OpenAI GPT-4.1
Specialized prompts"] H --> H1["🔍 Code-F Agent
OpenAI GPT-4.1
Specialized prompts"] I --> I1["🔍 Code-G Agent
OpenAI GPT-4.1
Specialized prompts"] J --> J1["🔍 Code-H Agent
OpenAI GPT-4.1
Specialized prompts"] D1 --> D2["📊 Supabase Vector Store
Code-B Knowledge Base
+ OpenAI Embeddings"] E1 --> E2["📊 Supabase Vector Store
Code-C Knowledge Base
+ OpenAI Embeddings"] F1 --> F2["📊 Supabase Vector Store
Code-D Knowledge Base
+ OpenAI Embeddings"] G1 --> G2["📊 Supabase Vector Store
Code-E Knowledge Base
+ OpenAI Embeddings"] H1 --> H2["📊 Supabase Vector Store
Code-F Knowledge Base
+ OpenAI Embeddings"] I1 --> I2["📊 Supabase Vector Store
Code-G Knowledge Base
+ OpenAI Embeddings"] J1 --> J2["📊 Supabase Vector Store
Code-H Knowledge Base
+ OpenAI Embeddings"] D2 --> RESP["💬 Specialized Response
Building Code compliant
Context-aware answer"] E2 --> RESP F2 --> RESP G2 --> RESP H2 --> RESP I2 --> RESP J2 --> RESP style A fill:#e1f5fe,stroke:#0277bd,stroke-width:2px style ORCH fill:#fff3e0,stroke:#f57c00,stroke-width:4px style B fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px style C fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px style D fill:#e8f5e8,stroke:#2e7d32,stroke-width:2px style E fill:#ffebee,stroke:#c62828,stroke-width:2px style F fill:#e3f2fd,stroke:#1565c0,stroke-width:2px style G fill:#e0f2f1,stroke:#00695c,stroke-width:2px style H fill:#fff8e1,stroke:#f57f17,stroke-width:2px style I fill:#fce4ec,stroke:#ad1457,stroke-width:2px style J fill:#e8f5e8,stroke:#388e3c,stroke-width:2px style D1 fill:#f1f8e9,stroke:#689f38,stroke-width:2px style E1 fill:#f1f8e9,stroke:#689f38,stroke-width:2px style F1 fill:#f1f8e9,stroke:#689f38,stroke-width:2px style G1 fill:#f1f8e9,stroke:#689f38,stroke-width:2px style H1 fill:#f1f8e9,stroke:#689f38,stroke-width:2px style I1 fill:#f1f8e9,stroke:#689f38,stroke-width:2px style J1 fill:#f1f8e9,stroke:#689f38,stroke-width:2px style D2 fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px style E2 fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px style F2 fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px style G2 fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px style H2 fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px style I2 fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px style J2 fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px style RESP fill:#e8f5e8,stroke:#2e7d32,stroke-width:4px

RAG Document Ingestion Pipeline

Automated document processing with OCR, chunking, and vector storage across all Building Code sections

Use mouse wheel to zoom, drag to pan
graph TD START["📁 Google Drive Trigger
Monitor Building Code folders
Auto-detect new PDFs"] --> DOC["📄 Document Detection
New file uploaded
Trigger processing pipeline"] DOC --> OCR["🔍 Mistral OCR Engine
mistral-ocr-latest
Text extraction & image annotation
Bounding box processing"] OCR --> EXTRACT["📝 Content Extraction
Page-by-page processing
Text & image separation
Metadata preservation"] EXTRACT --> SPLIT["✂️ Text Chunking
LangChain RecursiveCharacterTextSplitter
Context-aware splitting
Optimal chunk sizes"] EXTRACT --> IMG["🖼️ Image Processing
Supabase Storage upload
Caption generation
Metadata tagging"] SPLIT --> EMBED_TEXT["🔢 Text Embeddings
OpenAI text-embedding-3-large
High-dimensional semantic vectors
1536 dimensions"] IMG --> EMBED_IMG["📊 Image Embeddings
Caption vectorization
Visual content representation
Searchable image metadata"] EMBED_TEXT --> STORE_B["📊 Code-B Vector Store
Stability & Durability
Supabase pgvector
Structural requirements"] EMBED_TEXT --> STORE_C["📊 Code-C Vector Store
Protection from Fire
Supabase pgvector
Fire safety standards"] EMBED_TEXT --> STORE_D["📊 Code-D Vector Store
Access Routes
Supabase pgvector
Accessibility standards"] EMBED_TEXT --> STORE_E["📊 Code-E Vector Store
Moisture Management
Supabase pgvector
Water protection"] EMBED_TEXT --> STORE_F["📊 Code-F Vector Store
Safety of Users
Supabase pgvector
User safety requirements"] EMBED_TEXT --> STORE_G["📊 Code-G Vector Store
Services & Facilities
Supabase pgvector
Building services"] EMBED_TEXT --> STORE_H["📊 Code-H Vector Store
Energy Efficiency
Supabase pgvector
Energy performance"] EMBED_IMG --> STORE_B EMBED_IMG --> STORE_C EMBED_IMG --> STORE_D EMBED_IMG --> STORE_E EMBED_IMG --> STORE_F EMBED_IMG --> STORE_G EMBED_IMG --> STORE_H STORE_B --> READY["✅ Knowledge Base Ready
Multi-modal RAG system
Text + Image vectors
Section-specific expertise"] STORE_C --> READY STORE_D --> READY STORE_E --> READY STORE_F --> READY STORE_G --> READY STORE_H --> READY READY --> SYNC["🔄 Real-time Sync
Google Drive monitoring
Automatic updates
Version control"] SYNC --> RETRIEVAL["🔍 Ready for Queries
Semantic search enabled
Agent tool integration
Building Code expertise"] style START fill:#e8f5e8,stroke:#2e7d32,stroke-width:3px style DOC fill:#fff3e0,stroke:#f57c00,stroke-width:2px style OCR fill:#e3f2fd,stroke:#1565c0,stroke-width:3px style EXTRACT fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px style SPLIT fill:#fff8e1,stroke:#f57f17,stroke-width:2px style IMG fill:#fce4ec,stroke:#ad1457,stroke-width:2px style EMBED_TEXT fill:#e1f5fe,stroke:#0277bd,stroke-width:3px style EMBED_IMG fill:#e1f5fe,stroke:#0277bd,stroke-width:2px style STORE_B fill:#ffebee,stroke:#c62828,stroke-width:2px style STORE_C fill:#e8f5e8,stroke:#2e7d32,stroke-width:2px style STORE_D fill:#e3f2fd,stroke:#1565c0,stroke-width:2px style STORE_E fill:#e0f2f1,stroke:#00695c,stroke-width:2px style STORE_F fill:#fff8e1,stroke:#f57f17,stroke-width:2px style STORE_G fill:#fce4ec,stroke:#ad1457,stroke-width:2px style STORE_H fill:#e8f5e8,stroke:#388e3c,stroke-width:2px style READY fill:#f1f8e9,stroke:#689f38,stroke-width:3px style SYNC fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px style RETRIEVAL fill:#e8f5e8,stroke:#2e7d32,stroke-width:3px

Technology Stack

Layer Technology
Orchestration n8n (JavaScript workflows)
LLM & Embeddings OpenAI GPT-4.1, OpenAI Embeddings API
OCR & Processing Mistral.ai OCR (mistral-ocr-latest)
Vector Database Supabase Vector Store
Storage Supabase Storage Buckets
Memory PostgreSQL (n8n memoryPostgresChat)
Document Source Google Drive (triggered ingestion)
Code Processing n8n Code nodes (JS for markdown & images)
Text Splitting LangChain RecursiveCharacterTextSplitter
Deployment Cloud-native with auto-scaling capabilities

Why This Architecture Matters

  • Scalability – Adding new Building Code sections requires simple workflow duplication
  • Separation of Concerns – Each agent maintains its own knowledge domain without cross-contamination
  • Extensibility – Modular design allows easy integration of new OCR providers, LLMs, or vector databases
  • Production-Ready – Automated monitoring and updates ensure system reliability for regulatory compliance