Skip to main content
RAG (Retrieval-Augmented Generation) helps your AI agent find relevant information from documents to give better answers. It searches a knowledge base and uses that context to respond more accurately.

Architecture

The RAG pipeline flow:
1

Voice Input

STT converts speech to text.
2

Retrieval

The knowledge base fetches relevant documents based on the transcript.
3

Augmentation

Retrieved context is injected into the LLM prompt.
4

Generation

The LLM generates a grounded response using the context.
5

Voice Output

TTS converts the response to speech.

Managed RAG

Managed RAG lets you upload knowledge bases from the Zero runtime dashboard and connect them to your agent. During a conversation, the agent retrieves relevant information from these knowledge bases to provide more accurate, context-aware responses.
Managed RAG is supported in Python SDK 0.1.5+. JavaScript SDK support is coming soon.

Step 1: Upload a Knowledge Base

Upload your documents and create a knowledge base from the Zero runtime dashboard.
After the knowledge base is created, the dashboard provides a unique Knowledge ID.

Step 2: Attach the Knowledge Base

Pass the Knowledge ID to KnowledgeBase and set it as the pipeline’s knowledge_base. On every user turn the agent searches the attached knowledge bases and places the best-matching sections above the user’s message in the LLM prompt. Retrieval runs inside the agent process, so no extra code or hooks are needed.
You can attach multiple knowledge bases to the same agent. All of them are searched on every turn.
The knowledge_base pipeline parameter works with cascade pipelines (separate STT, LLM, and TTS) only. For a realtime pipeline, use the knowledge base as a function tool.

Configuration

Customize the context prefix

Use context_prefix to control how the LLM is told about the retrieved sections.
report_unanswered is deprecated and ignored. Unanswered questions are no longer filed to the dashboard, so remove it from your configuration.

Knowledge Base as a Function Tool

knowledge_base= searches on every user turn. To let the model decide when to search instead, call KnowledgeBase.retrieve() from a function tool. This works with any pipeline, cascade or realtime, and skips the lookup on turns that don’t need it (greetings, small talk).
retrieve() returns the best sections as text, under the knowledge base’s context_prefix. Override settings for a single call with keyword arguments:
Use either the tool or Pipeline(knowledge_base=...), not both, or each turn searches twice.
Say in the agent’s instructions when to call the tool. Models skip tools they aren’t told to use and answer from their own knowledge.

Custom RAG

For full control, Build your own RAG pipeline using any vector database (ChromaDB, Pinecone, etc.) with the user_turn_start hook.This hook fires when the user’s transcript is ready, before the LLM is called, giving you the perfect place to retrieve documents and inject context. 1. Set up the vector store. Create a collection to hold your document embeddings. ChromaDB ships a Python client. The snippet below keeps embeddings in a minimal in-memory index.
2. Implement retrieve(). Generate a query embedding and search the vector store for the top matching documents.
3. Inject context with the user_turn_start hook. Retrieve documents from the transcript and fold them into the system prompt with session.change_component(instructions=...) before the LLM is invoked. Rebuild from a base string each turn so the retrieved context doesn’t accumulate.
The LLM then sees the injected context and uses it to generate a grounded answer.

Best Practices

  • Start by retrieving k=2-3 documents and adjust based on performance.
  • Keep document chunk sizes between 300-800 words.
  • Use persistent storage for your vector database in production.
  • Cache embeddings for frequently asked questions to reduce latency.
  • Handle retrieval failures gracefully so the agent can still respond.

What’s Next

Function Tools

Add custom actions to your agent.

MCP

Connect external MCP servers as tools.