Skip to main content

Module context

Module context 

Source
Expand description

Context assembly — builds LLM prompts with token budget management.

Manages the token budget for LLM context windows:

  • System prompt (~500 tokens)
  • User model snapshot (~300 tokens)
  • Conversation history (~2000 tokens)
  • Retrieved memories (remaining budget)
  • Response buffer (~400 tokens)

Structs§

Attachment
Path-attachment grounding for a chat turn. When the user references a local path in their message, the pipeline reads it on their behalf and hands the snapshot here so the LLM can see what’s actually there alongside memories and history. The SOUL prompt’s “ATTACHED_CONTENT” instructions explain how to read these blocks.
ContextAssembler
Context assembler — builds prompts respecting token budgets.
SkippedAttachment
A path the user referenced that couldn’t be attached (not found, outside security.allowed_paths, wrong file kind). Rendered as a <SKIPPED_PATH> tag so Brain can mention it instead of silently dropping the reference.
TokenBudget
Token budget allocation.
UserProfile
User profile data for context injection.

Constants§

CHARS_PER_TOKEN
Conservative chars-per-token ratio for the prompt estimator. English prose runs ~4 chars/token and code/JSON (common in attachments) ~3, so we use 3: packing never badly under-counts and overflows the model’s true window, while being far less wasteful than the old 2-chars/token guess (which threw away ~half the usable window). Deliberately a portable heuristic, not a model-specific BPE tokenizer — Brain is multi-provider (Ollama/qwen, OpenAI, …) and no single vocabulary is correct for all of them. Swapping in a real tokenizer later only needs to touch this module.
DEFAULT_CAPABILITIES
The always-on cognitive faculties, rendered as the fallback “Your Capabilities” section of the SOUL prompt. Used verbatim when no live capability digest is supplied (non-chat LLM paths, tests, custom prompts) and as the prefix of the live digest the chat path builds (see signal::pipeline::conversation). Keeping the wording in one place stops the static and live views from drifting.
ONBOARDING_ADDENDUM
System-prompt addendum injected while the user has fewer than 5 facts. Makes the LLM naturally curious and question-asking during onboarding.
ONBOARDING_GREETING
Hardcoded greeting for first-ever chat session (0 facts). Printed directly — no LLM call needed.
TOKEN_BUDGETS
Default token budgets.

Functions§

estimate_tokens
Estimate the token count of text for budgeting and packing decisions. Single source of the heuristic — every budget check in this module routes through here so they can’t drift.