Skip to main content

ContextUsage

Struct ContextUsage 

Source
pub struct ContextUsage {
    pub total_input_tokens: TokenCount,
    pub total_output_tokens: TokenCount,
    pub context_window_size: u32,
    pub current_input_tokens: TokenCount,
    pub current_output_tokens: TokenCount,
    pub cache_creation_tokens: TokenCount,
    pub cache_read_tokens: TokenCount,
}
Expand description

Context window usage information.

Tracks token counts and calculates usage percentage based on current_usage from Claude Code’s status line. The current_usage object reflects the actual tokens being sent to the API in the current context window.

When current_usage is null (e.g., during /clear), all current_* fields are 0, which correctly shows 0% context usage.

Fields§

§total_input_tokens: TokenCount

Total input tokens across all turns (cumulative, for reference)

§total_output_tokens: TokenCount

Total output tokens across all turns (cumulative, for reference)

§context_window_size: u32

Maximum context window size for the model

§current_input_tokens: TokenCount

Current context’s input tokens (from current_usage.input_tokens)

§current_output_tokens: TokenCount

Current context’s output tokens (from current_usage.output_tokens)

§cache_creation_tokens: TokenCount

Tokens written to cache (from current_usage.cache_creation_input_tokens)

§cache_read_tokens: TokenCount

Tokens read from cache - this is the bulk of context (from current_usage.cache_read_input_tokens)

Implementations§

Source§

impl ContextUsage

Source

pub fn new(context_window_size: u32) -> Self

Creates a new ContextUsage with default values.

Source

pub fn context_tokens(&self) -> TokenCount

Calculates the context tokens currently in use.

This is the actual context being sent to the API, calculated from:

  • cache_read_tokens: Previously cached context being reused
  • current_input_tokens: New input tokens in this turn
  • cache_creation_tokens: New tokens being written to cache

When current_usage is null (e.g., after /clear), these are all 0.

Source

pub fn total_tokens(&self) -> TokenCount

Calculates the total tokens used (cumulative, for reference).

Source

pub fn usage_percentage(&self) -> f64

Returns the percentage of context window used (0.0 to 100.0).

Uses context_tokens() which reflects the actual tokens in the current context window (from current_usage). When current_usage is null, this returns 0%.

Source

pub fn is_warning(&self) -> bool

Returns true if context usage is above the warning threshold (80%).

Source

pub fn is_critical(&self) -> bool

Returns true if context usage is critical (>90%).

Source

pub fn exceeds_200k(&self) -> bool

Returns true if exceeds 200K tokens (Claude Code’s extended context marker).

Source

pub fn remaining_tokens(&self) -> TokenCount

Returns the remaining tokens before hitting context limit.

Source

pub fn format(&self) -> String

Formats usage for display (e.g., “45.2% (26.4K/200K)”).

Source

pub fn format_compact(&self) -> String

Formats usage compactly (e.g., “45%”).

Trait Implementations§

Source§

impl Clone for ContextUsage

Source§

fn clone(&self) -> ContextUsage

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ContextUsage

Source§

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

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

impl Default for ContextUsage

Source§

fn default() -> ContextUsage

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for ContextUsage

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for ContextUsage

Source§

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

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

impl PartialEq for ContextUsage

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for ContextUsage

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for ContextUsage

Source§

impl StructuralPartialEq for ContextUsage

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<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> 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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,