Skip to main content

Module context_budget

Module context_budget 

Source
Expand description

Unified context-budget math for the TUI.

Given a model’s context window, the current input token estimate, and a configured output cap, ContextBudget derives the four numbers the rest of the app needs to reason about a turn:

  • available input budget — how many input tokens may still be spent after reserving room for the model’s output;
  • output token cap — the output reservation actually used to compute that budget (clamped so it never starves the window);
  • compaction trigger — the input-token level at which compaction should be suggested (default: ~75% of the full window, clamped to the spendable input ceiling);
  • PressureLevel — a coarse Low/Medium/High/Critical signal the UI can render without re-deriving thresholds.

This module is the budget-math foundation. It is intentionally pure (no I/O, no clock, no engine/config types) so it can be unit-tested in isolation. Its consumers live outside it and call in, never the reverse: route_budget and core::engine::context for ContextBudget, context_report for PressureLevel.

§Route ceilings stay independent

A route can publish a total context window, an output ceiling, and a separate input ceiling. ContextBudget::new_with_input_limit intersects all three without treating one as a substitute for another: the output reservation is the route-effective wire request (a quarter of the window, at most 64K and never more than that request, on a route that shares one window between input and output), the total-window arithmetic remains saturating, and a concrete input limit clamps the final spendable ceiling and compaction trigger.

Structs§

ContextBudget
A computed snapshot of how a turn’s input sits against a model’s context window, plus the derived output cap, compaction trigger, and pressure level.

Enums§

PressureLevel
Coarse, UI-facing description of how full the context window is.

Constants§

CONTEXT_CRITICAL_THRESHOLD_PERCENT
CONTEXT_HEADROOM_TOKENS
Safety headroom (tokens) subtracted from the window in addition to the reserved output, to avoid bumping a provider’s hard limit. Matches the engine’s CONTEXT_HEADROOM_TOKENS.
CONTEXT_WARNING_THRESHOLD_PERCENT
CRITICAL_PRESSURE_PERCENT
Percentage of the window at or above which pressure is PressureLevel::Critical.
DEFAULT_COMPACTION_TRIGGER_PERCENT
Fraction of the window, expressed as a percentage, at or above which compaction should be suggested. Mirrors the “high” pressure boundary the existing context report uses for its diagnostic label, rounded up to the conventional three-quarters-full trigger.
HIGH_PRESSURE_PERCENT
Percentage of the window at or above which pressure is PressureLevel::High. Pressure and the requested compaction trigger are window-relative. The trigger is clamped to the spendable input ceiling, so it can fire before this UI boundary when output reservation consumes substantial window space.
MEDIUM_PRESSURE_PERCENT
Percentage of the window at or above which pressure is PressureLevel::Medium. Matches the “moderate” boundary of the existing diagnostic report.
MIN_INPUT_BUDGET_TOKENS
Smallest input budget (tokens) ContextBudget will report for any window large enough to hold it. The output cap is clamped down as needed to preserve this much input room, so a generous configured output cap can never drive the available input budget to zero on a usable window.

Functions§

usage_percent
Window usage as a percentage in 0.0..=100.0. Zero window -> 0.0.