RAG + RLM: The Complete Knowledge Base Architecture
Here’s a question that breaks most knowledge base chatbots:
“Compare customer complaints across all our regional offices and identify which issues are systemic versus location-specific.”
Your RAG system retrieves the top-k most similar documents. But “most similar” isn’t “all relevant.” It misses complaints that don’t match the query embedding. It can’t reason across hundreds of documents simultaneously. It stuffs what it finds into context and hopes for the best.
The answer comes back incomplete, or worse, confidently wrong.
This is the gap between retrieval and reasoning. RAG solves one. Recursive Language Models (RLMs) solve the other. A complete knowledge base architecture needs both.
The Retrieval-Reasoning Spectrum
Knowledge base queries fall on a spectrum:
Focused retrieval (RAG territory)
- “What’s our return policy?”
- “Find the latest pricing for Enterprise tier”
- “What did Sarah say in the Q3 planning doc?”
These queries need to find specific information quickly. Similarity search works. One or a few documents contain the answer.
Exhaustive reasoning (RLM territory)
- “Summarize all customer feedback from the past quarter”
- “What patterns appear across our technical incident reports?”
- “Compare how different teams documented their OKRs”
These queries require processing a large swath of the knowledge base — potentially all of it. No single document contains the answer. The answer emerges from synthesis across many documents.
Most chatbots only handle the first category. When users ask the second, they get partial answers or hallucinations.
Why RAG Alone Falls Short
RAG (Retrieval Augmented Generation) follows a simple pattern:
- Embed the query
- Search vector database for similar chunks
- Retrieve top-k results
- Stuff into LLM context
- Generate answer
The failure modes are predictable:
Similarity ≠ relevance. A complaint about “shipping delays in Texas” might not embed close to “logistics problems in the Southwest,” even though they’re describing the same issue.
Top-k is arbitrary. Why 5 chunks? Why 20? The right number depends on the query, but RAG uses a fixed retrieval count.
Context rot persists. Even if you retrieve the right chunks, stuffing 50 of them into context degrades LLM performance. The model struggles to synthesize information spread across a long prompt.
No exhaustive option. Some queries genuinely need to examine everything. RAG has no mechanism for this — it’s designed for selective retrieval, not comprehensive analysis.
Why RLM Alone Is Overkill
Recursive Language Models solve the context problem by treating the knowledge base as an environment the LLM navigates programmatically. Instead of retrieving chunks and stuffing them into context, the LLM writes code to examine documents, spawns sub-calls to analyze subsets, and aggregates results.
This handles exhaustive queries beautifully. But it’s expensive:
Multiple LLM calls. A single RLM query might spawn dozens of recursive calls, each incurring API costs and latency.
Slower responses. What RAG answers in 500ms might take RLM 10-30 seconds as it navigates the corpus.
Unnecessary for simple lookups. “What’s the refund policy?” doesn’t need recursive decomposition. It needs fast retrieval.
Using RLM for every query is like using a excavator to plant a flower. It works, but there’s a better tool.
The Hybrid Architecture
The solution is a router that classifies queries and dispatches to the appropriate system:
┌─────────────────────────────────────────────────────┐
│ User Query │
└─────────────────────┬───────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────┐
│ Query Router │
│ • Classifies: focused vs. exhaustive │
│ • Considers: scope, complexity, aggregation needs │
└──────────┬─────────────────────────────┬────────────┘
│ │
▼ ▼
┌─────────────────────┐ ┌─────────────────────────┐
│ RAG │ │ RLM │
│ • Vector search │ │ • Programmatic nav │
│ • Top-k retrieval │ │ • Recursive calls │
│ • Fast, cheap │ │ • Exhaustive analysis │
└──────────┬──────────┘ └────────────┬────────────┘
│ │
└──────────────┬──────────────┘
│
▼
┌─────────────────────────────────────────────────────┐
│ Answer │
└─────────────────────────────────────────────────────┘
The Router
The query router is itself an LLM call (or a lightweight classifier). It examines the query and determines:
- Scope: Does this need one document or many?
- Aggregation: Does the answer require synthesis across sources?
- Specificity: Is there a clear target, or is it exploratory?
ROUTER_PROMPT = """
Classify this query for a knowledge base:
Query: {query}
Categories:
- FOCUSED: Needs specific information from one or few documents
- EXHAUSTIVE: Needs analysis across many/all documents
Consider:
- Does it ask about "all," "every," "compare across," "patterns"?
- Does it reference a specific document, person, or topic?
- Does it require aggregation or synthesis?
Respond with just: FOCUSED or EXHAUSTIVE
"""
In practice, ~90% of queries route to RAG. The 10% that need RLM are the ones that would otherwise fail.
RAG Path
Standard retrieval-augmented generation:
def rag_answer(query: str, k: int = 10) -> str:
# Embed and retrieve
query_embedding = embed(query)
chunks = vector_db.search(query_embedding, top_k=k)
# Build context
context = "\n\n".join([c.text for c in chunks])
# Generate
return llm.complete(f"Context:\n{context}\n\nQuestion: {query}")
Optimizations like reranking, hybrid search, and query expansion improve RAG quality, but the fundamental pattern remains: retrieve, stuff, generate.
RLM Path
Recursive analysis using DSPy or the official RLM library:
import dspy
# Configure LLM
dspy.configure(lm=dspy.LM('anthropic/claude-sonnet-4-5'))
# Create RLM with signature
rlm = dspy.RLM('documents, question -> analysis: str')
def rlm_answer(query: str, corpus: str) -> str:
# RLM treats corpus as environment variable
# LLM writes code to navigate and analyze
return rlm(documents=corpus, question=query).analysis
The RLM handles decomposition automatically. For a query like “compare complaints across regions,” it might:
- Write code to group documents by region
- Spawn sub-calls to summarize each region’s complaints
- Aggregate summaries into comparative analysis
- Return synthesized answer
No manual chunking. No arbitrary top-k. The model decides how to explore the data.
When Each Path Wins
| Query Type | Route | Why |
|---|---|---|
| ”What’s the pricing for Pro tier?” | RAG | Specific lookup, one doc |
| ”Find docs mentioning the Chicago office” | RAG | Targeted retrieval |
| ”What did the CEO say about Q4?” | RAG | Specific source |
| ”Summarize all engineering postmortems” | RLM | Exhaustive synthesis |
| ”Compare onboarding docs across departments” | RLM | Cross-corpus analysis |
| ”What issues appear in >3 support tickets?” | RLM | Pattern detection |
| ”List every mention of compliance” | RLM | Exhaustive search |
The pattern: if the query implies “all,” “every,” “across,” or “compare,” it probably needs RLM. If it’s looking for something specific, RAG is faster and cheaper.
The Hybrid Payoff
This architecture delivers what neither system achieves alone:
Speed where it matters. Simple queries get sub-second RAG responses. Users aren’t waiting for recursive calls when they don’t need them.
Completeness where it matters. Complex queries get thorough analysis. Users don’t get partial answers to questions that span the knowledge base.
Cost efficiency. RAG handles the query volume at low cost. RLM’s higher cost is reserved for queries that justify it.
User trust. When someone asks “analyze everything,” they get an answer that actually analyzed everything — not a best-effort sample.
Implementation Notes
Start with RAG. Build a solid retrieval system first. Most queries will use it, and it’s simpler to debug.
Add routing second. A basic LLM-based router is ~50 lines of code. Start simple; tune thresholds based on observed queries.
Add RLM for specific use cases. Identify the query patterns that break your RAG system. Implement RLM for those.
Consider the hybrid case. Some queries benefit from RAG pre-filtering before RLM analysis. “Analyze all support tickets about billing” could use RAG to find billing tickets, then RLM to synthesize them.
Monitor and iterate. Track which queries route where, measure answer quality, adjust the router.
The Takeaway
RAG and RLM aren’t competing approaches — they’re complementary tools for different parts of the query spectrum.
RAG asks: “What’s similar to this query?” RLM asks: “How should I explore this corpus?”
A knowledge base that only does RAG will fail on comprehensive queries. One that only does RLM will be slow and expensive for simple lookups. The architecture that handles both — with intelligent routing between them — is the one that actually works.
Your users don’t think about retrieval strategies. They just ask questions. Some questions need a quick lookup. Others need deep analysis. Build a system that handles both, and the chatbot finally feels as capable as they expect it to be.
For more on RAG patterns, see our 7 RAG Patterns You Need to Know in 2026. For RLM deep-dive, see Recursive Language Models: The Next Frontier in Inference-Time Scaling.