budget
Context budget computation — tracks token allocation across the LLM context window to prevent overflow and optimize utilization.
Structs
Section titled “Structs”ContextBudget
Section titled “ContextBudget”Token budget allocation for an LLM context window.
Breaks the total context window into reserved zones and computes the remaining space available for conversation history.
┌────────────────────────────────────────────────────┐│ Context Window (total) │├────────────┬────────────┬────────┬─────────────────┤│ System │ Tools │ Output │ Available ││ (fixed) │ (schemas) │ (gen) │ for History │└────────────┴────────────┴────────┴─────────────────┘Fields
| Field | Type | Description |
|---|---|---|
total | u32 | Total context window in tokens (from ModelCapabilities). |
reserved_output | u32 | Tokens reserved for model output generation. |
reserved_system | u32 | Tokens consumed by the system prompt. |
reserved_tools | u32 | Tokens consumed by tool schema definitions. |
available_for_history | u32 | Remaining tokens available for conversation history + memories. |
Methods
fn new(context_window: u32, max_output_tokens: u32, system_message: Option<&str>, tools_json: &[serde_json::Value]) -> SelfCreate a new budget from model capabilities and current context.
Parameters
Section titled “Parameters”context_window: Total tokens the model can acceptmax_output_tokens: Tokens to reserve for generationsystem_message: The system prompt text (will be estimated)tools_json: Tool schema definitions (will be estimated)
history_usage
Section titled “history_usage”fn history_usage(&self, history_messages: &[serde_json::Value]) -> u32Compute how many tokens are actually used by a set of history messages.
would_exceed
Section titled “would_exceed”fn would_exceed(&self, history_messages: &[serde_json::Value]) -> boolCheck whether adding the given messages would exceed the history budget.
utilization
Section titled “utilization”fn utilization(&self, history_messages: &[serde_json::Value]) -> f32Utilization ratio (0.0 to 1.0) of the full context window.
remaining
Section titled “remaining”fn remaining(&self, history_messages: &[serde_json::Value]) -> u32Remaining tokens available after current history usage.