Large Context Knowledge Handling

Complete guide to handling large knowledge bases efficiently

PraisonAI Agents provides a comprehensive system for handling large knowledge bases efficiently, with automatic strategy selection, token budgeting, and intelligent compression.

python
from praisonaiagents import Agent

agent = Agent(
    name="KnowledgeAgent",
    instructions="Answer from a large document corpus",
    knowledge={"sources": ["./docs"]},
)
agent.start("What changed in the API this quarter?")

The user queries a huge knowledge base; retrieval, budgeting, and compression keep answers within context limits.

Diagram

👤 User question

🤖 Agent

📊 Token budget

🔍 Smart retrieval

✅ Grounded answer

How It Works

Diagram
Large Context Knowledge HandlingAgentUserLarge Context Knowledge HandlingAgentUserRequestProcess requestResult Agent–>>User: Response

Large Context Knowledge Handling

Overview

The large context handling system includes:

FeatureDescription
Token BudgetingDynamic budget management for context windows
Incremental IndexingEfficient file tracking and updates
Retrieval StrategiesAutomatic strategy selection by corpus size
Smart RetrievalHybrid search with reranking
Context CompressionIntelligent compression to fit budgets
Hierarchical SummariesMulti-level summaries for large corpora

Quick Start

  1. Create an agent with knowledge
    python
    from praisonaiagents import Agent
    
    agent = Agent(
        name="KnowledgeAgent",
        instructions="Answer questions using the knowledge base.",
        knowledge={"sources": ["./docs"]},
        memory={"user_id": "my_user"},
    )
    response = agent.chat("What are the main features?")
  2. Index and search via CLI
    bash
    praisonai knowledge index ./docs --user-id myuser
    praisonai knowledge search "query" --strategy hybrid --compress

Architecture

┌─────────────────────────────────────────────────────────────┐
│                     Agent Query                              │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                  Strategy Selection                          │
│  (DIRECT → BASIC → HYBRID → RERANKED → COMPRESSED → HIER)   │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                   Smart Retrieval                            │
│         (Keyword + Semantic + Reranking)                     │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                 Context Compression                          │
│      (Deduplication + Extraction + Truncation)               │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                   Token Budgeting                            │
│           (Enforce limits, reserve response)                 │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                      LLM Response                            │
└─────────────────────────────────────────────────────────────┘

CLI Commands

bash
# Index documents
praisonai knowledge index ./docs --user-id myuser

# Get corpus statistics
praisonai knowledge stats ./docs

# Search with options
praisonai knowledge search "query" --strategy hybrid --rerank --compress

# Build hierarchical summaries
praisonai knowledge summarize ./docs --levels 3

# Clear knowledge store
praisonai knowledge clear --yes

Configuration

RetrievalConfig

python
from praisonaiagents.rag import RetrievalConfig, RetrievalStrategy

config = RetrievalConfig(
    # Strategy selection
    strategy=RetrievalStrategy.AUTO,

    # Retrieval settings
    top_k=10,
    min_score=0.5,

    # Token budget
    max_context_tokens=4000,
    reserved_response_tokens=1000,

    # Features
    use_hybrid=True,
    use_reranking=True,
    use_compression=True,
    compression_ratio=0.5,
)

agent = Agent(
    name="ConfiguredAgent",
    knowledge={
        "sources": ["./docs"],
        "retrieval_k": 10,
        "retrieval_threshold": 0.5,
        "rerank": True,
    }
)

Examples

Context-Required Q&A

python
# Example with unique codes that require retrieval
from praisonaiagents import Agent

agent = Agent(
    name="PolicyExpert",
    instructions="Answer based ONLY on provided knowledge.",
    knowledge={"sources": ["./policies"]},
    memory={"user_id": "demo_user"},
)

# This question requires retrieval - answer cannot be guessed
response = agent.chat("What is the manager approval code?")
# Agent retrieves: "ZEBRA-71" from policy document

Large Corpus Handling

python
from praisonaiagents import Agent
from praisonaiagents.rag import RetrievalConfig, RetrievalStrategy

# For large corpora, use hierarchical strategy
agent = Agent(
    name="LargeCorpusAgent",
    knowledge={
        "sources": ["./large_docs"],  # 100k+ files
        "retrieval_k": 10,
    }
)

Scope Isolation

python
# Different users have isolated knowledge
agent_user1 = Agent(
    name="Agent",
    knowledge={"sources": ["./docs"]},
    memory={"user_id": "user1"},
)

agent_user2 = Agent(
    name="Agent",
    knowledge={"sources": ["./docs"]},
    memory={"user_id": "user2"},
)

# Each user's indexed content is isolated

Performance

The system is designed for zero performance impact when not in use:

  • Lazy imports – Heavy dependencies loaded only when needed
  • Incremental indexing – Only changed files re-indexed
  • Automatic strategy – Simpler strategies for smaller corpora
  • Token budgeting – Prevents context overflow

Best Practices

Use incremental indexing

Re-index only changed files in large, frequently updated corpora to save time and cost.

Set token budgets per model

Match budgets to your model window so retrieval and history share space safely.

Enable reranking for relevance

Hybrid search plus reranking improves answer quality on noisy document sets.

Use hierarchical summaries at scale

For corpora above ~100k tokens, summarise in layers instead of stuffing raw chunks into context.

Related