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.

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?")

The user asks a question; PraisonAI selects the best retrieval strategy for the indexed corpus size.

Diagram

Indexed Corpus

Auto Select

DIRECT

BASIC

HYBRID

RERANKED

COMPRESSED

HIERARCHICAL

Agent Response

How It Works

Diagram
Retrieval StrategiesAgentUserRetrieval StrategiesAgentUserRequestProcess requestResult Agent–>>User: Response

Quick Start

  1. 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?")
  2. 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 countStrategyTechnique
< 10DIRECTLoad all content
< 100BASICSemantic search
< 1,000HYBRIDKeyword + semantic
< 10,000RERANKEDHybrid + reranking
< 100,000COMPRESSEDReranked + compression
≥ 100,000HIERARCHICALMulti-level summaries
python
from praisonaiagents.rag import select_strategy
from praisonaiagents import CorpusStats

stats = CorpusStats(file_count=500)
strategy = select_strategy(stats)
print(strategy.value)  # hybrid

Configuration Options

OptionTypeDefaultDescription
strategystr"auto"auto, direct, basic, hybrid, reranked, compressed, hierarchical
top_kint5Chunks to retrieve
rerankboolFalseEnable cross-encoder reranking
hybridboolFalseCombine keyword and semantic search
compressboolFalseCompress retrieved context

CLI Usage

bash
praisonai knowledge search "query" --strategy hybrid
praisonai knowledge search "query" --rerank --compress --max-context-tokens 4000

Best 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.


Related