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:- Indexing: Documents are processed and converted to vector embeddings
- Query: User question is embedded using same model
- Retrieval: Most relevant document chunks are retrieved via semantic search
- Context Injection: Retrieved content is added to agent’s context
- Generation: Agent generates response using retrieved information
- 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
Hybrid Search
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:4. Legal Document Analysis
Search and analyze contracts:5. Medical Information System
Healthcare knowledge base (HIPAA-compliant):6. Product Catalog Search
E-commerce product information:Best Practices
Document Preparation
Structure Your Documents
Structure Your Documents
Add Metadata
Add Metadata
Optimize Chunk Size
Optimize Chunk Size
Keep Documents Current
Keep Documents Current
Query Optimization
Citation and Sources
Integration with Other Primitives
With Memory
Combine RAG with conversation memory:With Custom Tools
Combine RAG with live data:With Multi-Agent
Specialized agents with different knowledge bases: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
Poor Retrieval Results
Poor Retrieval Results
Problem: RAG returns irrelevant documentsSolutions:
- Improve document structure and headings
- Add more descriptive metadata
- Adjust chunk size
- Use hybrid search mode
- Refine query phrasing
Slow Query Performance
Slow Query Performance
Problem: Queries taking too longSolutions:
- Add metadata filters to narrow search
- Reduce datastore size
- Optimize chunk size
- Enable caching
Missing Recent Updates
Missing Recent Updates
Problem: New documents not being foundSolutions:
- Verify document upload completed
- Check indexing status
- Allow time for indexing (can take minutes)
- Refresh datastore
Incorrect Citations
Incorrect Citations
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:Related Primitives
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