Skip to main content
RAG enables agents to retrieve and reference information from your documents, knowledge bases, and data sources, providing accurate, contextual responses grounded in your content.

Overview

The RAG (Retrieval-Augmented Generation) primitive empowers agents to access and utilize information from your document collections, knowledge bases, and proprietary data. By combining semantic search with generation, agents can provide accurate answers grounded in your specific content rather than relying solely on training data. RAG is essential for:
  • Knowledge Base Access: Answer questions from documentation, manuals, and guides
  • Document Search: Find and reference specific information across large document sets
  • Contextual Accuracy: Provide responses grounded in verified sources
  • Domain Expertise: Specialize agents in specific knowledge domains
  • Citation Support: Back up responses with source references
  • Up-to-Date Information: Access latest documentation without retraining

Semantic Search

Find relevant information using natural language queries with vector embeddings

Source Attribution

Automatically cite sources and provide references for generated responses

Multi-Format Support

Index PDFs, Word docs, text files, markdown, HTML, and more

Real-Time Updates

Update knowledge base in real-time as documents change

How RAG Works

When RAG is enabled for an agent:
  1. Indexing: Documents are processed and converted to vector embeddings
  2. Query: User question is embedded using same model
  3. Retrieval: Most relevant document chunks are retrieved via semantic search
  4. Context Injection: Retrieved content is added to agent’s context
  5. Generation: Agent generates response using retrieved information
  6. Citation: Sources are cited in the response
Semantic Understanding: RAG uses vector embeddings to understand meaning, not just keyword matching. Questions like “How do I reset my password?” will match “Password recovery steps” even without exact word overlap.

Code Examples

Basic RAG Setup

Multiple Datastores

Filtered RAG Queries

RAG with Custom Chunking

Use Cases

1. Customer Support Knowledge Base

Answer customer questions from help docs:

2. Technical Documentation Assistant

Help developers with API documentation:

3. Internal Company Handbook

Company policies and procedures:
Search and analyze contracts:

5. Medical Information System

Healthcare knowledge base (HIPAA-compliant):
E-commerce product information:

Best Practices

Document Preparation

Query Optimization

Specific Questions Work Best: RAG performs best with specific, targeted questions rather than broad, open-ended queries.

Citation and Sources

Always Verify Sources: While RAG provides citations, always verify critical information, especially for medical, legal, or financial content.

Integration with Other Primitives

With Memory

Combine RAG with conversation memory:
Learn more: Memory Primitive

With Custom Tools

Combine RAG with live data:
Learn more: MCP Primitive

With Multi-Agent

Specialized agents with different knowledge bases:
Learn more: Multi-Agent Primitive

Performance Considerations

Indexing Time

  • Small docs (< 100 pages): ~1-2 minutes
  • Medium docs (100-1000 pages): ~5-15 minutes
  • Large docs (> 1000 pages): ~30-60 minutes

Query Performance

  • Cold query: ~500-1000ms (first query in session)
  • Warm query: ~200-400ms (subsequent queries)
  • Optimization: Use filters to narrow search space

Cost Optimization

Troubleshooting

Problem: RAG returns irrelevant documentsSolutions:
  • Improve document structure and headings
  • Add more descriptive metadata
  • Adjust chunk size
  • Use hybrid search mode
  • Refine query phrasing
Problem: Queries taking too longSolutions:
  • Add metadata filters to narrow search
  • Reduce datastore size
  • Optimize chunk size
  • Enable caching
Problem: New documents not being foundSolutions:
  • Verify document upload completed
  • Check indexing status
  • Allow time for indexing (can take minutes)
  • Refresh datastore
Problem: Agent cites wrong sourcesSolutions:
  • Improve source documents quality
  • Add unique identifiers to sections
  • Use structured document format
  • Verify embedding quality

Advanced Features

Reranking

Improve retrieval quality with reranking:

Custom Embeddings

Use domain-specific embeddings:

Multi-Modal RAG

Index images and diagrams:

Web Search

Search the web in addition to internal docs

Memory

Remember user preferences and history

MCP

Combine docs with live data from APIs

Custom Tools

Integrate RAG with custom tools

Additional Resources

API Reference

Datastore API documentation

RAG Guide

RAG optimization guide

Examples

RAG implementation examples
Remember: RAG is most effective when your documents are well-structured, current, and organized with meaningful metadata. Invest time in document preparation for best results.