Skip to main content

Unified Retrieval System

AgentOS provides a multi-source retrieval pipeline that automatically selects the best strategy for each query. The system unifies vector search, BM25 keyword matching, HyDE hypothesis generation, RAPTOR hierarchical summaries, GraphRAG entity traversal, and cognitive memory into a single query path.

Architecture

  User Query


┌──────────────────────────┐
│ QueryClassifier │ LLM-as-judge or heuristic
│ (tier + strategy) │ determines retrieval depth
└────────────┬─────────────┘

┌──────────────────────────┐
│ RetrievalPlan │ Pure data — describes what
│ strategy, sources, │ sources to query and how
│ hyde, memory, temporal │ to combine results
└────────────┬─────────────┘

┌──────────────────────────┐
│ UnifiedRetriever │ Executes plan across all
│ (parallel source fan) │ sources in parallel
│ │
│ ┌─────┐ ┌─────┐ ┌────┐ │
│ │Vec+ │ │RAPT-│ │Gra-│ │
│ │BM25 │ │OR │ │ph │ │
│ └──┬──┘ └──┬──┘ └─┬──┘ │
│ └───┬───┘ │ │
│ ┌──────┴──────────┐│ │
│ │ RRF Merge ││ │
│ └───────┬─────────┘│ │
│ └────┬─────┘ │
│ ┌────┴────┐ │
│ │ Rerank │ │
│ └────┬────┘ │
│ ▼ │
│ Merged Results │
└──────────────────────────┘


Memory Feedback Loop
(store as episodic memory)

Strategy Tiers

The classifier assigns one of four strategies based on query complexity:

StrategySourcesWhen to Use
noneSkip retrievalGreetings, trivial questions
simpleVector + BM25 + memoryDirect lookups, specific terms
moderate+ HyDE + GraphRAG + RAPTORAbstract questions, "how" / "why"
complex+ Decompose + Deep ResearchMulti-part, comparative analysis

none (Tier 0)

The query is trivial or conversational. No retrieval is performed — the LLM answers from its internal knowledge or conversation context.

simple (Tier 1)

Direct vector similarity search plus BM25 keyword matching. Fast and cheap. Best when the query vocabulary closely matches stored document vocabulary. Cognitive memory (episodic + semantic) is also consulted.

moderate (Tier 2)

All sources enabled. HyDE generates a hypothetical answer before embedding, bridging vocabulary gaps between questions and documents. GraphRAG traverses entity relationships from seed chunks. RAPTOR searches hierarchical summary layers for thematic matches.

complex (Tier 3)

Full pipeline including deep research decomposition. The query is broken into sub-queries, each processed at moderate depth, then results are merged and synthesised. Multi-hypothesis HyDE (3 hypotheses) improves recall. All four cognitive memory types are consulted.

Configuration Reference

All settings live under the rag key in agent.config.json:

{
"rag": {
"enabled": true,
"preset": "balanced",

"hyde": {
"enabled": true,
"hypothesisCount": 3,
"initialThreshold": 0.7
},

"hybrid": {
"enabled": true,
"denseWeight": 0.7,
"sparseWeight": 0.3
},

"raptor": {
"enabled": true,
"maxDepth": 4,
"clusterSize": 8
},

"chunking": {
"strategy": "semantic",
"targetSize": 1000,
"overlap": 100,
"preserveCodeBlocks": true
},

"queryRouter": {
"enabled": true,
"classifierMode": "hybrid",
"defaultStrategy": "moderate"
},

"memoryIntegration": {
"enabled": true,
"feedbackLoop": true,
"memoryTypes": ["episodic", "semantic"]
}
}
}

hyde

FieldTypeDefaultDescription
enabledbooleantrueEnable HyDE hypothesis generation
hypothesisCountnumber3Hypotheses per query (more = better recall, higher cost)
initialThresholdnumber0.7Initial similarity threshold
adaptiveThresholdbooleantrueAuto-lower threshold when no results

hybrid

FieldTypeDefaultDescription
enabledbooleantrueEnable BM25 alongside vectors
denseWeightnumber0.7Dense vector weight in RRF merge
sparseWeightnumber0.3BM25 sparse weight in RRF merge

raptor

FieldTypeDefaultDescription
enabledbooleantrueEnable RAPTOR summary tree
maxDepthnumber4Maximum tree depth (summary layers)
clusterSizenumber8Target cluster size per layer

chunking

FieldTypeDefaultDescription
strategystring"semantic""fixed" or "semantic"
targetSizenumber1000Target chunk size (characters)
overlapnumber100Overlap between chunks (characters)
preserveCodeBlocksbooleantrueKeep code blocks as atomic chunks

queryRouter

FieldTypeDefaultDescription
enabledbooleantrueEnable auto-classification
classifierModestring"hybrid""heuristic", "llm", or "hybrid"
defaultStrategystring"moderate"Fallback when classifier unavailable

memoryIntegration

FieldTypeDefaultDescription
enabledbooleantrueSearch cognitive memory
feedbackLoopbooleantrueStore retrieval as episodic memory
memoryTypesstring[]["episodic", "semantic"]Memory types to search

CLI Commands

Query with strategy selection

# Auto-detect strategy (default)
wunderland rag query "How does auth work?"

# Force a specific strategy
wunderland rag query "How does auth work?" --strategy moderate

# Enable specific sources
wunderland rag query "Compare approaches" --strategy complex --deep-research --hyde --hyde-count 5

# Search cognitive memory alongside documents
wunderland rag query "What did we discuss yesterday?" --memory --memory-types episodic,semantic

Ingest with semantic chunking

# Semantic chunking (default)
wunderland rag ingest ./docs/architecture.md

# Fixed chunking with custom size
wunderland rag ingest ./data.txt --chunking fixed --chunk-size 500 --chunk-overlap 50

# Build RAPTOR tree and BM25 index after ingestion
wunderland rag ingest ./docs --build-raptor --build-bm25

# Extract GraphRAG entities
wunderland rag ingest ./codebase --extract-entities

Preview a retrieval plan

wunderland rag plan "Compare all caching strategies"
# Output:
# Strategy: complex
# Sources: vector Y bm25 Y graph Y raptor Y memory Y multimodal N
# HyDE: 3 hypothesis(es)
# Deep Research: enabled
# Memory Types: episodic, semantic, procedural, prospective

Check system status

wunderland rag status
# Output:
# Vector Store: hnswlib (1,234 documents, 5,678 chunks)
# BM25 Index: enabled (5,678 documents)
# RAPTOR Tree: 3 layers (leaf: 5,678, L1: 710, L2: 89)
# GraphRAG: 89 entities, 145 relationships
# HyDE: enabled
# Reranker: lexical
# Embeddings: available
# Memory: connected (episodic: 45, semantic: 123)

When to Use Each Strategy

Use simple when:

  • The user asks a fact-based question with specific terms
  • Query vocabulary closely matches document vocabulary
  • Speed is critical (under 200ms target)
  • Example: "What port does the API run on?"

Use moderate when:

  • The question is abstract or uses different vocabulary than docs
  • You need cross-document synthesis
  • GraphRAG entity relationships would help
  • Example: "How does the authentication flow from frontend to backend?"

Use complex when:

  • The question has multiple parts requiring separate research
  • Comparative analysis across many sources is needed
  • Deep synthesis with external data would improve the answer
  • Example: "Compare all caching strategies in this codebase and recommend improvements"

Memory Integration

When memoryIntegration.enabled is true, the retrieval pipeline searches the agent's cognitive memory alongside the document corpus:

  • Episodic memory: Past interactions, events, conversation history
  • Semantic memory: Facts, knowledge, learned concepts
  • Procedural memory: Workflows, how-to knowledge, skills
  • Prospective memory: Upcoming intentions, reminders, planned actions

The feedback loop (feedbackLoop: true) stores every successful retrieval as a new episodic memory trace. This means frequently-retrieved information gets a "memory cache hit" shortcut on subsequent queries, progressively improving retrieval speed for repeated topics.

Performance Implications

StrategyTypical LatencyLLM CallsToken Cost
none0ms00
simple50-200ms0Embedding only
moderate200-800ms1 (HyDE)~500 tokens
complex1-5s3+ (HyDE + decompose)~2000 tokens

Tips for optimising:

  • Use heuristic classifier mode to avoid the classification LLM call
  • Set rag.hyde.hypothesisCount: 1 for moderate queries where speed matters
  • Disable RAPTOR and GraphRAG for small corpora (under 100 documents)
  • Use rag.preset: "fast" for latency-sensitive applications

Chat Runtime Integration

During wunderland chat, the query router automatically classifies each user message and retrieves context before the LLM generates its response. The configuration from agent.config.json is respected.

To see retrieval strategy details during chat:

wunderland chat --verbose

To disable the query router:

wunderland chat --no-query-router