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: TokenCountTotal input tokens across all turns (cumulative, for reference)
total_output_tokens: TokenCountTotal output tokens across all turns (cumulative, for reference)
context_window_size: u32Maximum context window size for the model
current_input_tokens: TokenCountCurrent context’s input tokens (from current_usage.input_tokens)
current_output_tokens: TokenCountCurrent context’s output tokens (from current_usage.output_tokens)
cache_creation_tokens: TokenCountTokens written to cache (from current_usage.cache_creation_input_tokens)
cache_read_tokens: TokenCountTokens read from cache - this is the bulk of context (from current_usage.cache_read_input_tokens)
Implementations§
Source§impl ContextUsage
impl ContextUsage
Sourcepub fn context_tokens(&self) -> TokenCount
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.
Sourcepub fn total_tokens(&self) -> TokenCount
pub fn total_tokens(&self) -> TokenCount
Calculates the total tokens used (cumulative, for reference).
Sourcepub fn usage_percentage(&self) -> f64
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%.
Sourcepub fn is_warning(&self) -> bool
pub fn is_warning(&self) -> bool
Returns true if context usage is above the warning threshold (80%).
Sourcepub fn is_critical(&self) -> bool
pub fn is_critical(&self) -> bool
Returns true if context usage is critical (>90%).
Sourcepub fn exceeds_200k(&self) -> bool
pub fn exceeds_200k(&self) -> bool
Returns true if exceeds 200K tokens (Claude Code’s extended context marker).
Sourcepub fn remaining_tokens(&self) -> TokenCount
pub fn remaining_tokens(&self) -> TokenCount
Returns the remaining tokens before hitting context limit.
Sourcepub fn format_compact(&self) -> String
pub fn format_compact(&self) -> String
Formats usage compactly (e.g., “45%”).
Trait Implementations§
Source§impl Clone for ContextUsage
impl Clone for ContextUsage
Source§fn clone(&self) -> ContextUsage
fn clone(&self) -> ContextUsage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more