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§
- Context
Budget - 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§
- Pressure
Level - 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)
ContextBudgetwill 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.