Retrieval Configuration

Configure retrieval behaviour for AI agents with knowledge bases

Control how agents retrieve and inject knowledge through the knowledge parameter โ€” one surface for sources, chunking, reranking, and retrieval policy.

python
from praisonaiagents import Agent

agent = Agent(
    name="Research Agent",
    instructions="Answer questions based on the provided documents.",
    knowledge={
        "sources": ["docs/", "papers.pdf"],
        "retrieval_k": 5,
        "rerank": True,
    },
)
response = agent.start("What are the key findings?")

The user asks a question; the agent retrieves relevant chunks from configured knowledge sources before answering.

Diagram

Retrieval

๐Ÿ“‹ User Query

๐Ÿค– Agent

๐Ÿ” Retrieve Context

โœ… Answer + Citations

Quick Start

  1. Simple Usage
    python
    from praisonaiagents import Agent
    
    agent = Agent(
        name="Research Agent",
        instructions="Answer questions based on the provided documents.",
        knowledge={
            "sources": ["docs/"],
            "retrieval_k": 5,
            "rerank": True,
        },
    )
    response = agent.start("What are the key findings?")
  2. With Configuration
    python
    from praisonaiagents import Agent, KnowledgeConfig
    
    agent = Agent(
        name="Agent",
        instructions="You are a helpful assistant.",
        knowledge=KnowledgeConfig(
            sources=["documents/"],
            embedder="openai",
            chunking_strategy="semantic",
            chunk_size=1000,
            chunk_overlap=200,
            retrieval_k=5,
            retrieval_threshold=0.3,
            rerank=True,
            auto_retrieve=True,
            vector_store={"provider": "chroma", "collection_name": "default"},
        ),
    )

How It Works

The user asks a question; the agent embeds the query, retrieves the top-k chunks, then generates an answer with citations.

Diagram
KnowledgeAgentUserKnowledgeAgentUserstart(“What are the findings?”)Retrieve top-k chunksRelevant context + scoresGenerate answer with contextResponse + citations

Configuration Options

OptionTypeDefaultDescription
sourcesList[str][]Files, directories, or URLs to index
embedderstr"openai"Provider shorthand; "openai" means “no override”. See Use a non-OpenAI embedder.
embedder_configDict[str, Any]NoneFull embedder dict {"provider": ..., "config": {...}}
chunking_strategystr"semantic"Chunking method
chunk_sizeint1000Target chunk size in tokens
chunk_overlapint200Overlap between chunks
retrieval_kint5Number of chunks to retrieve
retrieval_thresholdfloat0.0Minimum relevance score (0.0โ€“1.0)
rerankboolFalseEnable reranking
rerank_modelstrNoneCustom rerank model
auto_retrieveboolTrueInject context automatically
vector_storedictNoneVector store provider config

For the retrieval-time tuning surface (top_k, rerank, citations, strategy, โ€ฆ), see the full RetrievalConfig field reference on the RAG page.


Pass tuning without wrapping in KnowledgeConfig

retrieval_config= is a shortcut for retrieval-time tuning on Agent(...) directly. A dict merges on top of what knowledge= derived; a RetrievalConfig instance replaces it wholesale.

python
from praisonaiagents import Agent

agent = Agent(
    name="Research Agent",
    instructions="Answer questions based on the provided documents.",
    knowledge={"sources": ["docs/"]},
    retrieval_config={"top_k": 5, "citations": True},
)
response = agent.start("What are the key findings?")

See RAG โ†’ Pass retrieval tuning directly to Agent for the merge-vs-replace rules and the full field table.


Agent Methods

agent.start() / agent.chat()

Conversation with automatic retrieval when auto_retrieve=True:

python
response = agent.start("What is the main topic?")
response = agent.start("Hello", force_retrieval=True)
response = agent.start("What is 2+2?", skip_retrieval=True)

agent.query()

Structured answer with citations:

python
result = agent.query("What are the main findings?")
print(result.answer)
for citation in result.citations:
    print(f"[{citation.id}] {citation.source} (score: {citation.score:.2f})")

agent.retrieve()

Retrieval only โ€” no LLM generation:

python
context_pack = agent.retrieve("What are the key points?")
print(f"Found {len(context_pack.citations)} sources")

Multi-Agent Shared Knowledge

python
from praisonaiagents import Agent, Knowledge

shared = Knowledge(config={"vector_store": {"provider": "chroma", "config": {"collection_name": "shared_docs"}}})
shared.add("company_docs/")

analyst = Agent(
    name="Analyst",
    instructions="Analyse data from documents.",
    knowledge={"sources": shared, "retrieval_k": 10},
)

summariser = Agent(
    name="Summariser",
    instructions="Summarise key points.",
    knowledge={"sources": shared, "retrieval_k": 5},
)

Best Practices

Set retrieval_threshold for noisy corpora

Use retrieval_threshold=0.3 or higher when low-scoring chunks pollute answers.

Retrieve more, rerank down

For large bases, set retrieval_k=20 with rerank=True โ€” cast a wide net, then keep the best chunks.

Use skip_retrieval for non-RAG prompts

Arithmetic, greetings, and meta questions do not need retrieval โ€” pass skip_retrieval=True.

Index once, share across agents

Create one Knowledge instance and pass it as sources to multiple agents to avoid re-indexing.


Related