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:
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.Maximal Marginal Relevance — balances relevance with diversity to reduce duplicate results.
Trigger Modes
Thetrigger parameter controls when the knowledge base is queried:
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