Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Context Window

Context usage is tracked by ContextWindowTracker (crates/core/src/context_window.rs) and emitted to frontends via Event::ContextUpdate (crates/protocol/src/protocol.rs).

Token Accounting

After each completed turn, core updates cumulative token usage (crates/core/src/submission/turn_lifecycle.rs):

  • input_tokens
  • output_tokens

The tracker computes:

  • used_tokens (input + output)
  • max_tokens (model context limit)
  • usage_ratio (used / max, clamped)

Model Limits

Limits are resolved from provider model metadata (swarmie_provider::models::get_model_info):

  • context window fallback: 200_000
  • max output tokens fallback: 16_384

Auto-Compaction

Before a new user turn, core checks needs_auto_compact() (crates/core/src/submission/op_handlers.rs).

Auto-compaction threshold is:

threshold = context_window - max_output_tokens - safety_margin
safety_margin = max(context_window / 10, 4096)

If usage crosses threshold, core summarizes older history and emits Event::ContextCompacted (crates/core/src/handlers/compaction.rs).

Manual Compaction

Protocol supports Op::Compact, handled by run_manual_compaction() in crates/core/src/handlers/compaction.rs.

In current TUI slash handling (crates/tui/src/app/update/commands.rs), /compact reports current context usage text; it does not directly dispatch Op::Compact.

Related Budgeting

Context window pressure (tokens) and session budget (USD) are separate systems:

  • context controls prompt/token capacity
  • budget controls turn admission by cost (hard_cap, soft_cap, warnings)