Corrective Agentic RAG with Self-Reflection
Autonomous retrieval pipeline combining dense vector embeddings, knowledge graph traversal, and an iterative self-correction loop to diagnose and eliminate hallucinations.
1. System Requirements
- Decompose complex multi-hop queries into targeted sub-questions with contextual dependency graphs.
- Perform hybrid retrieval combining dense vector similarity (pgvector/Qdrant), keyword BM25, and entity graph traversal (Neo4j).
- Grade document relevance autonomously with self-reflection: if chunks lack relevance, rewrite queries or trigger web search tools.
- Zero hallucination tolerance: every generated claim must map directly to an attributed source citation chunk.
- P95 query response time under 1.8 seconds including reflection and self-correction loops.
- Automatic fallback to external search tools (MCP Google Search / Tavily) when internal corpora lack coverage.
2. Scale & Resource Estimates
Throughput: 10,000 hybrid queries/minute across 50M document chunks.
Storage & RAM: 120 GB HNSW dense vector index + 45 GB Neo4j graph cluster + 20 GB Redis embedding cache.
3. System API Endpoints
POST /v1/agentic-rag/query
Content-Type: application/json
{
"query": "How did Q3 cloud infrastructure spend change compared to our original 2025 budget?",
"max_reflection_hops": 3,
"confidence_threshold": 0.85
}
Response (200 OK):
{
"answer": "Cloud infrastructure spend increased by 14.2% in Q3 2025 ($1.37M vs. budgeted $1.20M), primarily driven by GPU cluster provisioning for agent model inference.",
"citations": [
{ "source": "2025_Q3_Financial_Review.pdf", "page": 14, "chunk_id": "chk-994", "score": 0.94 },
{ "source": "2025_Annual_Operating_Plan.xlsx", "sheet": "CapEx", "chunk_id": "chk-112", "score": 0.91 }
],
"reasoning_trajectory": [
"Step 1: Extracted budgeted Q3 cloud spend -> $1.20M",
"Step 2: Extracted actual Q3 cloud spend -> $1.37M",
"Step 3: Computed variance -> +14.2% ($170,000 increase)"
],
"retrieval_hops": 2
}
4. Database Schema Design
CREATE TABLE rag_chunks (
chunk_id VARCHAR(64) PRIMARY KEY,
document_id VARCHAR(64) NOT NULL,
text_content TEXT NOT NULL,
embedding VECTOR(1536), -- pgvector HNSW index
entities TEXT[],
metadata JSONB,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE INDEX idx_rag_hnsw ON rag_chunks USING hnsw (embedding vector_cosine_ops);
CREATE INDEX idx_rag_doc ON rag_chunks(document_id);
5. Interactive High-Level Architecture
6. Scalability Deep Dive & Bottlenecks
Standard Naive RAG is brittle: it blindly injects the top-K retrieved text chunks into the prompt context, leading to hallucinations when chunks are tangential or contradictory. Agentic RAG introduces an active decision loop: an evaluator LLM grades each chunk's relevance. If the chunks fail the relevance threshold, the agent rewrites the search query (query expansion, entity extraction) and issues a secondary retrieval or falls back to live web search tools before drafting the final answer.
7. Production Scaling Strategies
Context Caching: Cache pre-computed embeddings and document index catalogs in memory using pgvector and Qdrant.
Reciprocal Rank Fusion (RRF): Merge rankings from sparse keyword search, dense semantic search, and knowledge graph neighbors with low CPU overhead.
Speculative Verification: Run hallucination checks asynchronously or in parallel with streaming to provide zero perceived latency overhead for end users.
8. Runnable Production Starter Code
Scaffold