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.

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 Python and JavaScript clients; in Go, use any vector store 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.update_instructions(...) (updateInstructions in JavaScript, UpdateInstructions in Go) 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.