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) -> ContextBudget
pub fn new(max_tokens: usize, reserve_ratio: f32) -> ContextBudget
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) -> ContextBudget
pub fn with_graph_enabled(self, enabled: bool) -> ContextBudget
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 moreAuto Trait Implementations§
impl Freeze for ContextBudget
impl RefUnwindSafe for ContextBudget
impl Send for ContextBudget
impl Sync for ContextBudget
impl Unpin for ContextBudget
impl UnsafeUnpin for ContextBudget
impl UnwindSafe for ContextBudget
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request