| Approach | How It Works | When to Use |
|---|---|---|
| Path shorthand | Pass a directory path string — auto-configures full RAG pipeline | Quick setup from local files |
| KnowledgeMiddleware | Automatically retrieves context before every model call | Always-on RAG with custom config |
| KnowledgeToolkit | Agent decides when to search | Selective, multi-step reasoning |
Path Shorthand (Quickest)
Pass a directory path as a string to auto-configure the full RAG pipeline with sensible defaults (InMemoryVectorDB + OpenAIEmbedder + RecursiveChunker). All supported files in the directory are recursively loaded.Agent(knowledge=True) raises ValueError — unlike memory=True, knowledge requires either a path string or a Knowledge instance.KnowledgeMiddleware (Automatic)
The middleware automatically searches the knowledge base using the user’s message and injects relevant context into the system prompt. The agent doesn’t need to do anything special — the context is always there.Knowledge Parameters
These parameters are passed directly to theKnowledge constructor:
The vector database backend for storing and searching documents.
The embedder for generating vector representations. If
None, uses the vector DB’s default.Number of documents to retrieve.
Whether to apply reranking to search results.
Template for formatting retrieved documents in the prompt.
Where to inject context:
"system" (in the system prompt) or "before_user" (before the user message).Which message to use as the search query.
"last_user" uses the latest user message.When to activate retrieval.
"always" retrieves on every call; "auto" does a lightweight model pre-check; "never" disables retrieval even if configured.Custom YES/NO prompt for
trigger="auto". If None, uses the default prompt asking whether the query needs the knowledge base.Model to use for the
trigger="auto" gate call. Defaults to the agent’s main model. Pass a cheap model (e.g. OpenAIChat(id="gpt-4o-mini")) to reduce latency and cost.Description shown in the layer guide injected into the system prompt. If
None, uses the default description.Scoring & Hybrid Search
These optional parameters enable advanced retrieval strategies. See Hybrid Search & Scoring for full documentation.Full-text search index for hybrid vector + BM25 keyword search.
Merge strategy and weights for combining vector and text search results.
Exponential score decay based on document age. Useful for time-sensitive content.
Maximal Marginal Relevance — balances relevance with diversity to reduce duplicate results.
Trigger Modes
Thetrigger parameter controls when the knowledge base is queried:
| Mode | Behavior | When to Use |
|---|---|---|
"always" | Retrieves on every call (default) | Most RAG use cases |
"auto" | Routing model gates retrieval; only queries the knowledge base when relevant | Mixed-topic agents where knowledge is needed for some queries but not others |
"never" | Retrieval disabled, even if knowledge is configured | Temporarily disabling without removing configuration |
trigger="auto" with a routing model
When trigger="auto", a routing call decides whether retrieval is needed before the main model call. Use a cheap model to minimize cost and latency:
trigger="auto", the system prompt layer guide also reflects the actual retrieval state: [retrieved this turn] if knowledge was fetched, or [available, not retrieved this turn] if the gate decided to skip it.
KnowledgeToolkit (On-Demand)
With the toolkit approach, the agent has asearch_knowledge tool and decides when to use it:
Choosing Between Middleware and Toolkit
Use Middleware when...
- Every question needs knowledge context
- You want zero-configuration retrieval
- The knowledge base is focused on a single domain
- You want the simplest setup
Use Toolkit when...
- Not every question needs retrieval
- The agent should reason about when to search
- The agent needs to search with different queries
- You want the agent to explain its search process