Skip to content

Dynamic context ​

Dynamic context automatically retrieves relevant documents before every agent turn. It gives the model focused knowledge without placing the full corpus in every request.

Explore dynamic context ​

PageLearn how to
Add contextAttach a prepared vector index to an agent.
FormattingTurn search results into concise model-ready documents.
Filters and permissionsScope retrieval with trusted application state.
Multiple indexesCombine independent knowledge sources without flooding the prompt.

How it works ​

text
Current prompt → vector search → filter and rank → format documents → model turn

For each turn, Anvia searches every registered dynamic-context index with the current runtime prompt. Matching results are converted into documents and sent with that turn's model request.

Retrieval runs again after a tool call. A later turn can therefore receive different documents as the conversation and tool results change.

Add automatic retrieval ​

ts
import { AgentBuilder } from '@anvia/core'
import { vectorFilter } from '@anvia/core/vector-store'

const agent = new AgentBuilder('docs-support', model)
  .instructions('Use retrieved documentation when it is relevant.')
  .dynamicContext(docsIndex, {
    topK: 5,
    threshold: 0.72,
    filter: vectorFilter.eq('published', true),
  })
  .build()

docsIndex must implement VectorSearchIndex. Prepare and populate the index outside the request path; see Knowledges for ingestion, embeddings, and vector stores.

Choose the right source ​

RequirementUse
Small facts safe for every runStatic .context(...)
Relevant documents selected every turn.dynamicContext(...)
Optional, model-directed searchindex.asTool(...)
Live or permissioned product dataA scoped tool
A large searchable tool catalog.dynamicTools(...)

Dynamic context is read-only evidence. It should not replace service calls for current account state or actions that need validation, authorization, and audit.

Built for Anvia.