Skip to main content

Context Budget

When multiple memory stores are enabled, the combined context string injected into the system prompt can grow large. The context budget system caps the total token count and distributes the budget proportionally across memory sections so the most important context always makes it in.

The Problem

A fully-loaded memory config (summaries, user facts, entities, learnings, graph, decisions, procedures) can produce thousands of tokens of context. Without a budget, all of it is injected — potentially blowing past the model’s context window or crowding out the actual conversation.

Configuration

Add contextBudget to your memory config:
When maxTokens is set, buildContext() allocates tokens to each section based on its priority weight. Sections that exceed their allocation are trimmed line-by-line; sections that fit are included in full.

Default Priorities

Each memory section has a default priority that determines what share of the budget it receives. Higher values get more tokens. Priorities are relative — they are normalized against the sum of all active sections. If you only enable summaries (0.25) and userFacts (0.15), summaries receive 62.5% of the budget and userFacts receive 37.5%.

How Budget Allocation Works

buildContext() follows these steps:
  1. Gather — Fetch context strings from every enabled store.
  2. Measure — Count the tokens in each section.
  3. Check — If total tokens are under maxTokens, return everything as-is.
  4. Allocate — Assign each section a token budget proportional to its priority weight.
  5. Trim — Sort sections by priority (lowest first). Starting from the highest priority, include sections that fit. If a section exceeds its remaining budget, trim it line-by-line until it fits. Sections below the cutoff are dropped entirely.
  6. Assemble — Join the surviving sections in priority order (highest first).
Lower-priority sections (learnings, procedures) are trimmed or dropped first, ensuring summaries and user context survive.

Custom Priorities

Override any priority to shift the budget toward what matters most for your use case:
Only the keys you specify are overridden; unmentioned sections keep their defaults.

Priority presets by use case


Inspecting Token Usage

Call buildContext() directly and measure the result to see exactly how many tokens are being used:
This is useful for tuning maxTokens — start with a generous budget, inspect the output, then tighten it based on what you actually see.

Without a Budget

If you omit contextBudget, all sections are concatenated without any trimming. This is fine when memory stores are small or the model has a large context window, but you should add a budget once context grows beyond a few thousand tokens.

Cross-References