pub struct ContextBudget {
pub system_prompt_static_tokens: usize,
pub system_prompt_dynamic_tokens: usize,
pub tool_schema_tokens: usize,
pub deferred_tool_tokens: usize,
pub skill_listing_tokens: usize,
pub conversation_tokens: usize,
pub tool_result_tokens: usize,
pub context_window_size: usize,
pub static_prefix_cache_hit: bool,
pub static_prefix_hash: Option<String>,
}Expand description
Tracks token usage by component for context optimization.
Each field represents the estimated token count for a specific component of the prompt. This enables:
- Monitoring which components consume the most context
- Triggering compaction when utilization exceeds thresholds
- Informing deferred loading decisions (tools, skills)
- API-side prompt caching optimization
Fields§
§system_prompt_static_tokens: usizeStatic system prompt tokens (cacheable across sessions)
system_prompt_dynamic_tokens: usizeDynamic system prompt tokens (per-session: env, memory, hooks)
tool_schema_tokens: usizeTool schema tokens (full schemas for core tools)
deferred_tool_tokens: usizeDeferred tool listing tokens (name + description only)
skill_listing_tokens: usizeSkill listing tokens in system prompt
conversation_tokens: usizeConversation history tokens (all messages)
tool_result_tokens: usizeTool result tokens in current turn
context_window_size: usizeTotal estimated context window size for the model
static_prefix_cache_hit: boolWhether the static prompt prefix hash has changed (cache bust)
static_prefix_hash: Option<String>Hash of the static system prompt prefix for cache tracking
Implementations§
Source§impl ContextBudget
impl ContextBudget
Sourcepub fn total_tokens(&self) -> usize
pub fn total_tokens(&self) -> usize
Total tokens currently consumed across all components
Sourcepub fn utilization(&self) -> f64
pub fn utilization(&self) -> f64
Context utilization as a percentage (0.0 - 1.0)
Sourcepub fn remaining_tokens(&self) -> usize
pub fn remaining_tokens(&self) -> usize
Remaining tokens available in the context window
Sourcepub fn is_warning(&self) -> bool
pub fn is_warning(&self) -> bool
Whether context utilization exceeds the warning threshold (80%)
Sourcepub fn is_critical(&self) -> bool
pub fn is_critical(&self) -> bool
Whether context utilization exceeds the critical threshold (90%)
Sourcepub fn deferred_savings(&self) -> usize
pub fn deferred_savings(&self) -> usize
Tokens saved by deferring tools (vs loading all schemas)
Trait Implementations§
Source§impl Clone for ContextBudget
impl Clone for ContextBudget
Source§fn clone(&self) -> ContextBudget
fn clone(&self) -> ContextBudget
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more