Skip to main content
Build an AI agent that retrieves relevant information before answering, reducing hallucinations and providing accurate, sourced responses.

The Pattern

Give the AI a search tool backed by vector embeddings to retrieve semantically similar information, then let it synthesize a response from the results.

Complete Example: Documentation Assistant

A full RAG agent with vector search that answers questions about your documentation:

Using Different Embedding Providers

You can use any supported embedding provider:

Multi-Source RAG

Search multiple sources and combine results:

With Citations

Track which documents were used and include citations:
Combine keyword and vector search for better results:

Tips

Embed your documents once at startup using embed_many() and cache the results. This avoids re-computing embeddings on every query.
OpenAI’s v3 models support custom dimensions. Use 256 or 512 dimensions instead of 1536 to reduce storage and improve search speed:
Split long documents into smaller chunks (e.g., 500-1000 tokens) before embedding. This improves retrieval precision.
Don’t pre-search—let the AI formulate its own queries based on the user’s question for better results.
Include document titles, URLs, and IDs so the AI can cite sources.
Combine vector similarity with keyword matching for better results, especially for exact term matching.

Next Steps

Embeddings Guide

Learn more about generating embeddings

embed() Reference

Full API reference for embed functions