Anvia
Agents

Instructions and Context

Shape agent behavior with instructions and runtime context.

Instructions describe how the agent should behave. Context provides facts the agent should use while answering.

Instructions

Use .instructions(...) for stable behavior that should apply to every prompt.

const agent = new AgentBuilder("support", model)
  .instructions("Answer in a concise, friendly tone.")
  .instructions("Ask for missing details before guessing.")
  .instructions("Do not invent order status. Use tools when order data is needed.")
  .build();

Multiple instruction blocks are joined together. Keep each block focused so it is easy to move, remove, or test.

Static Context

Use .context(...) for short facts that should be included with every request.

const agent = new AgentBuilder("support", model)
  .instructions("Use the support policy when answering.")
  .context("Password reset links expire after 30 minutes.", "password-policy")
  .context("Priority support is available for enterprise plans.", "priority-support")
  .build();

Static context is a poor fit for large or frequently changing knowledge bases. Use retrieval for that.

Dynamic Context

Use .dynamicContext(...) when the relevant facts depend on the prompt.

const agent = new AgentBuilder("support", model)
  .instructions("Use retrieved context when it is relevant.")
  .dynamicContext(supportDocsIndex, {
    topK: 3,
    format: (result) => ({
      id: result.id,
      text: `${result.document.title}\n${result.document.body}`,
    }),
  })
  .build();

Anvia searches dynamic context at prompt time and adds matching documents to the completion request.

What Goes Where

Put it inWhen
InstructionsIt is a behavioral rule
Static contextIt is short and applies to every prompt
Dynamic contextIt is searched or changes often
ToolsThe agent must read or change application state
HistoryIt comes from the conversation

Keep Context Small

Prefer specific context over broad dumps.

const agent = new AgentBuilder("billing", model)
  .instructions("Answer only billing questions.")
  .context("Invoices are generated on the first day of each month.", "invoice-cycle")
  .build();

Large context makes answers slower, more expensive, and harder to debug. If a document can be searched, put it behind retrieval instead.