Skip to main content
Retrieval-Augmented Generation (RAG) lets your agents answer questions grounded in your own documents rather than relying solely on the model’s training data. Anvil’s Retriever handles the full pipeline: splitting documents into chunks, embedding them, storing vectors, and retrieving the most similar chunks for any query. The retrievalContext step wires this directly into the agent loop so relevant chunks appear in the system prompt automatically.

Core concepts

Retriever

Orchestrates chunking, embedding, and indexing. Call index() to add documents and retrieve() to query them.

Embedder

Converts text to vectors. Use HashEmbedder for dev/tests, or plug in an OpenAI/Gemini provider embedder for semantic similarity.

VectorStore

Persists and queries (id, text, embedding) records. Choose MemoryVectorStore for tests or SqliteVectorStore for persistence.

retrievalContext

A ContextStep that auto-retrieves chunks for the current query and appends them to the agent’s system prompt before the model call.

Setting up a Retriever

Import the pieces you need and assemble a Retriever:

Constructor options

Indexing documents

Call retriever.index(docs) to chunk, embed, and store documents. It returns the number of chunks added.

IndexDoc fields

Retrieving chunks

Call retriever.retrieve(query, options?) to get the top-K most similar chunks for a query string.
Results are VectorQueryResult[] sorted by descending cosine similarity:

RetrieveOptions

Embedders

HashEmbedder (dev/test)

HashEmbedder is a zero-dependency, deterministic embedder that uses a hashed bag-of-tokens approach. It requires no API key and no network calls, making it ideal for local development and CI. Because it is not semantic, replace it with a provider embedder before going to production.

Provider embedders

Any object that implements the Embedder interface works as a drop-in replacement:

Vector stores

MemoryVectorStore (tests)

In-process, zero-dependency brute-force cosine search. Data is lost when the process exits.

SqliteVectorStore (production)

Persistent SQLite-backed store. Requires the better-sqlite3 optional peer dependency. Similarity search runs in JS over all stored records — appropriate for small-to-medium corpora embedded in an application.
Install better-sqlite3 when you use SqliteVectorStore. The package loads lazily so it never breaks builds that use only MemoryVectorStore.

Custom chunking

chunkText(text, options?) splits text into overlapping chunks, preferring natural paragraph and sentence boundaries. You can call it yourself before indexing if you need fine-grained control.
Pass the same options directly to the Retriever via the chunk constructor option:

Using RAG in an agent

retrievalContext from anvil/agent is a ContextStep that retrieves chunks for the current user query and appends them to the agent’s system prompt before each model call. This is the recommended way to integrate RAG into an agent loop.

Complete example

Custom chunk rendering

Override how retrieved chunks appear in the system prompt with the template option:
Pass the request’s trace handle to retriever.retrieve() if you call it manually — this records a retrieval span in the Anvil dashboard so you can see exactly which chunks influenced each response.