Skip to main content

ContextBudget

Struct ContextBudget 

Source
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: u64

Total context window for the active route (input + output), in tokens.

§input_tokens: u64

Current estimated input tokens already committed to the turn.

§output_cap_tokens: u64

Output 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: u64

Spendable 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: u64

Input tokens still available before hitting the reserved boundary (input_budget_ceiling - input, saturating at 0).

§compaction_trigger_tokens: u64

Input-token level at or above which compaction should be suggested (DEFAULT_COMPACTION_TRIGGER_PERCENT of the window, clamped to the input budget ceiling).

§pressure: PressureLevel

Coarse pressure level derived from window usage.

Implementations§

Source§

impl ContextBudget

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn should_compact(&self) -> bool

Whether current input has reached the compaction trigger and compaction should be suggested.

Source

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for ContextBudget

Source§

impl Debug for ContextBudget

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ContextBudget

Source§

impl PartialEq for ContextBudget

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ContextBudget

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more