pub struct ContextBudget {
pub window_tokens: u64,
pub input_tokens: u64,
pub output_cap_tokens: u64,
pub input_budget_ceiling: u64,
pub available_input_tokens: u64,
pub compaction_trigger_tokens: u64,
pub pressure: PressureLevel,
}Expand description
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.
Construct via ContextBudget::new. All fields are token counts unless the
name says otherwise. The struct is Copy and holds no borrowed data so it
can be cached on UI state cheaply.
Fields§
§window_tokens: u64Total context window for the active route (input + output), in tokens.
input_tokens: u64Current estimated input tokens already committed to the turn.
output_cap_tokens: u64Output tokens reserved (and thus the effective output cap) for the turn.
Derived from the configured cap, clamped to fit the window while leaving
at least MIN_INPUT_BUDGET_TOKENS of input room.
input_budget_ceiling: u64Spendable input ceiling for the turn (window - output_cap - headroom,
saturating at 0). Compaction percentages use the full window and clamp
their resulting token threshold to this ceiling.
available_input_tokens: u64Input tokens still available before hitting the reserved boundary
(input_budget_ceiling - input, saturating at 0).
compaction_trigger_tokens: u64Input-token level at or above which compaction should be suggested
(DEFAULT_COMPACTION_TRIGGER_PERCENT of the window, clamped to the
input budget ceiling).
pressure: PressureLevelCoarse pressure level derived from window usage.
Implementations§
Source§impl ContextBudget
impl ContextBudget
Sourcepub fn new(
window_tokens: u64,
input_tokens: u64,
configured_output_cap: u64,
) -> Self
pub fn new( window_tokens: u64, input_tokens: u64, configured_output_cap: u64, ) -> Self
Build a budget snapshot for a route.
window_tokens— the route-effective context window (input + output).input_tokens— current estimated input tokens for the turn.configured_output_cap— the route-effective output request the caller needs to reserve. It is clamped down so it never consumes the headroom or the minimum input budget; on a window too small to hold even the minimum input budget plus headroom, the cap collapses to whatever is left (possibly zero).
Never panics and never underflows: all arithmetic saturates.
Sourcepub fn new_with_input_limit(
window_tokens: u64,
input_tokens: u64,
configured_output_cap: u64,
input_limit_tokens: Option<u64>,
) -> Self
pub fn new_with_input_limit( window_tokens: u64, input_tokens: u64, configured_output_cap: u64, input_limit_tokens: Option<u64>, ) -> Self
Build a budget snapshot and intersect it with a provider’s independent hard input ceiling when one is published by the resolved route.
Sourcepub fn compaction_trigger_for_percent(&self, percent: f64) -> u64
pub fn compaction_trigger_for_percent(&self, percent: f64) -> u64
Derive a compaction trigger from a window percentage.
The percentage means what the user-facing context meter says it means:
a fraction of the full route window (80 on a 1M window is 800K input
tokens before the route ceiling clamp). One internal clamp applies:
trigger = min(window × percent, window − output reservation − headroom)so a late percentage can never push the trigger past the spendable input ceiling and overflow the provider window. Previously the percentage was applied to the ceiling itself, which silently pulled an “80%” setting down to ~60% of the window on routes with large output reservations; UI surfaces should disclose the clamp when it engages instead of redefining what the percentage means.
Sourcepub fn usage_percent(&self) -> f64
pub fn usage_percent(&self) -> f64
Fraction of the window currently consumed by input, as a percentage in
0.0..=100.0. A zero window reports 0.0 rather than dividing by zero.
Sourcepub fn should_compact(&self) -> bool
pub fn should_compact(&self) -> bool
Whether current input has reached the compaction trigger and compaction should be suggested.
Sourcepub fn fits_additional(&self, additional_input_tokens: u64) -> bool
pub fn fits_additional(&self, additional_input_tokens: u64) -> bool
Whether another additional_input_tokens of input would fit within the
available budget (i.e. not exceed the reserved boundary).
Trait Implementations§
Source§impl Clone for ContextBudget
impl Clone for ContextBudget
impl Copy for ContextBudget
Source§impl Debug for ContextBudget
impl Debug for ContextBudget
impl Eq for ContextBudget
Source§impl PartialEq for ContextBudget
impl PartialEq for ContextBudget
impl StructuralPartialEq for ContextBudget
Auto Trait Implementations§
impl Forget for ContextBudget
impl Freeze for ContextBudget
impl Move for ContextBudget
impl RefUnwindSafe for ContextBudget
impl Send for ContextBudget
impl Sync for ContextBudget
impl Unpin for ContextBudget
impl UnsafeUnpin for ContextBudget
impl UnwindSafe for ContextBudget
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.