Retrieval Strategies
Automatic strategy selection based on corpus size and complexity
PraisonAI picks the best retrieval strategy from your indexed corpus size — from direct loading for tiny folders to hierarchical search for massive libraries.
from praisonaiagents import Agent
agent = Agent(
name="Researcher",
instructions="Answer from the knowledge base.",
knowledge={"sources": ["docs/"], "retrieval_k": 10, "rerank": True},
)
response = agent.start("What are the key features?")The user asks a question; PraisonAI selects the best retrieval strategy for the indexed corpus size.
How It Works
Quick Start
- Simple Usage
Strategy selection is automatic when you attach knowledge to an agent:
python from praisonaiagents import Agent agent = Agent( name="Researcher", instructions="Answer from the knowledge base.", knowledge={"sources": ["docs/"], "retrieval_k": 10, "rerank": True}, ) response = agent.start("What are the key features?") - With Configuration
Override the auto-selected strategy via
KnowledgeConfig.config:python from praisonaiagents import Agent, KnowledgeConfig agent = Agent( name="Researcher", instructions="Answer from documents.", knowledge=KnowledgeConfig( sources=["docs/"], rerank=True, config={"strategy": "hybrid", "top_k": 10, "hybrid": True}, ), )
Strategy Selection
| File count | Strategy | Technique |
|---|---|---|
| < 10 | DIRECT | Load all content |
| < 100 | BASIC | Semantic search |
| < 1,000 | HYBRID | Keyword + semantic |
| < 10,000 | RERANKED | Hybrid + reranking |
| < 100,000 | COMPRESSED | Reranked + compression |
| ≥ 100,000 | HIERARCHICAL | Multi-level summaries |
from praisonaiagents.rag import select_strategy
from praisonaiagents import CorpusStats
stats = CorpusStats(file_count=500)
strategy = select_strategy(stats)
print(strategy.value) # hybridConfiguration Options
| Option | Type | Default | Description |
|---|---|---|---|
strategy | str | "auto" | auto, direct, basic, hybrid, reranked, compressed, hierarchical |
top_k | int | 5 | Chunks to retrieve |
rerank | bool | False | Enable cross-encoder reranking |
hybrid | bool | False | Combine keyword and semantic search |
compress | bool | False | Compress retrieved context |
CLI Usage
praisonai knowledge search "query" --strategy hybrid
praisonai knowledge search "query" --rerank --compress --max-context-tokens 4000Best Practices
Start with auto-selection
Let the SDK pick the strategy from corpus size unless you have measured latency or quality requirements.
Enable rerank for large corpora
Set rerank=True when retrieval_k is high — reranking improves relevance on noisy result sets.
Use hybrid for mixed terminology
Technical docs with exact identifiers benefit from strategy="hybrid" or hybrid=True.
Profile before overriding
Run a few queries with auto-selection first; only pin a strategy when benchmarks show a clear win.