pub struct ContextBudget { /* private fields */ }Expand description
Token budget for a single agent session.
Tracks the maximum token window and divides it across context slots.
Call ContextBudget::allocate or ContextBudget::allocate_with_opts to get a
BudgetAllocation that can be fed to crate::assembler::ContextAssembler.
Implementations§
Source§impl ContextBudget
impl ContextBudget
Sourcepub fn new(max_tokens: usize, reserve_ratio: f32) -> Self
pub fn new(max_tokens: usize, reserve_ratio: f32) -> Self
Create a new budget with max_tokens capacity and reserve_ratio fraction reserved
for the model response.
§Examples
use zeph_context::budget::ContextBudget;
let budget = ContextBudget::new(128_000, 0.15);
assert_eq!(budget.max_tokens(), 128_000);Sourcepub fn with_graph_enabled(self, enabled: bool) -> Self
pub fn with_graph_enabled(self, enabled: bool) -> Self
Enable or disable graph fact allocation in the budget split.
When enabled, 4% of available tokens are routed to the graph_facts slot, and the
summaries/semantic_recall slices are each reduced by 1%.
Sourcepub fn max_tokens(&self) -> usize
pub fn max_tokens(&self) -> usize
Maximum token capacity for this session.
Sourcepub fn allocate(
&self,
system_prompt: &str,
skills_prompt: &str,
tc: &dyn TokenCounting,
graph_enabled: bool,
) -> BudgetAllocation
pub fn allocate( &self, system_prompt: &str, skills_prompt: &str, tc: &dyn TokenCounting, graph_enabled: bool, ) -> BudgetAllocation
Allocate the budget across context slots for one turn.
Equivalent to allocate_with_opts(…, 0, false).
§Examples
§Examples
use zeph_context::budget::ContextBudget;
// Any type implementing `zeph_common::memory::TokenCounting` can be used.
let budget = ContextBudget::new(128_000, 0.15);
let tc = Tc;
let alloc = budget.allocate("system prompt", "skills prompt", &tc, false);
assert!(alloc.recent_history > 0);Sourcepub fn allocate_with_opts(
&self,
system_prompt: &str,
skills_prompt: &str,
tc: &dyn TokenCounting,
graph_enabled: bool,
digest_tokens: usize,
memory_first: bool,
) -> BudgetAllocation
pub fn allocate_with_opts( &self, system_prompt: &str, skills_prompt: &str, tc: &dyn TokenCounting, graph_enabled: bool, digest_tokens: usize, memory_first: bool, ) -> BudgetAllocation
Allocate context budget with optional digest pre-reservation and MemoryFirst mode.
This method provides fine-grained control over token allocation by allowing callers
to pre-reserve tokens for session digests and toggle MemoryFirst mode. Use this
when digest blocks or memory-focused allocation is needed; otherwise, call
allocate for simpler usage.
§Parameters
system_prompt— the system prompt string; its token count is deducted from available tokensskills_prompt— the skills block; its token count is also deductedtc— a token counter implementingTokenCounting(e.g., the LLM provider)graph_enabled— whentrue, allocates 4% of available tokens tograph_factsdigest_tokens— pre-counted tokens for the session digest block; deducted from available tokens BEFORE percentage splits so it does not silently crowd out other slotsmemory_first— whentrue, setsrecent_historyto 0 and redistributes those tokens acrosssummaries,semantic_recall, andcross_session
§Returns
A BudgetAllocation with all context slots populated according to the budget strategy.
§Examples
use zeph_context::budget::ContextBudget;
let budget = ContextBudget::new(128_000, 0.15);
let tc = Tc;
// Allocate with a pre-counted digest of 500 tokens in MemoryFirst mode
let alloc = budget.allocate_with_opts(
"system prompt",
"skills prompt",
&tc,
false,
500, // digest_tokens
true, // memory_first
);
assert_eq!(alloc.recent_history, 0);Trait Implementations§
Source§impl Clone for ContextBudget
impl Clone for ContextBudget
Source§fn clone(&self) -> ContextBudget
fn clone(&self) -> ContextBudget
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more