RAG Context
Add retrieval context to agent and completion workflows.
RAG context searches an index for each prompt and injects the matched documents into the model request.
1. Preprocess First
Build the index before the prompt runs.
const embedded = await embedDocuments(embeddings, documents, {
id: (doc) => doc.id,
content: (doc) => `${doc.title}\n${doc.body}`,
metadata: (doc) => ({ product: doc.product }),
});
const index = InMemoryVectorStore.fromDocuments(embedded).index(embeddings);Do not rebuild the index on every user prompt in production.
2. Attach Dynamic Context
const agent = new AgentBuilder("support", model)
.instructions("Use retrieved context when answering support questions.")
.dynamicContext(index, {
topK: 2,
})
.build();On each prompt, Anvia searches the index using the prompt text and adds matching documents to the completion request.
3. Format Retrieved Documents
const agent = new AgentBuilder("support", model)
.dynamicContext(index, {
topK: 2,
format: (result) => ({
id: result.id,
text: `${result.document.title}\n${result.document.body}`,
}),
})
.build();Use format(...) when your stored document is an object and the model should receive a specific text shape.
4. Add a Threshold
const agent = new AgentBuilder("support", model)
.dynamicContext(index, {
topK: 3,
threshold: 0.75,
})
.build();Thresholds prevent weak matches from entering the prompt.
5. Run the Agent
const response = await agent
.prompt("How long does a password reset link last?")
.send();Use dynamic context for automatic retrieval. Use a search tool when the model should decide whether to search.
6. Choose The Retrieval Slot
Retrieval can feed different parts of a model request.
| Pattern | Retrieves | Adds to | Use when |
|---|---|---|---|
.dynamicContext(...) | documents or facts | documents | Every prompt should receive relevant knowledge |
.dynamicTools(...) | tool definitions | tools | The agent has a large tool catalog and should see only relevant tools |
.tools([searchDocs]) | a search capability | tools | The model should decide whether and when to search |
Dynamic tools reuse vector search, but they retrieve capabilities instead of knowledge.
const toolIndex = await createToolIndex(embeddings, supportTools);
const agent = new AgentBuilder("support", model)
.dynamicContext(policyIndex, { topK: 3 })
.dynamicTools(toolIndex, { topK: 5 })
.build();