pub struct CostTracker {
pub total_input_tokens: u64,
pub total_output_tokens: u64,
pub total_cost_usd: f64,
pub call_count: u64,
pub budget_usd: Option<f64>,
}Expand description
Tracks cumulative token usage and estimated cost for a session.
Fields§
§total_input_tokens: u64§total_output_tokens: u64§total_cost_usd: f64§call_count: u64§budget_usd: Option<f64>Optional session cost budget in USD. When set, the agent loop should
check is_over_budget and pause when
the budget is exhausted.
Implementations§
Source§impl CostTracker
impl CostTracker
Sourcepub fn set_budget(&mut self, usd: f64)
pub fn set_budget(&mut self, usd: f64)
Set a cost budget in USD for the session.
Once total_cost_usd reaches or
exceeds this value, is_over_budget
returns true and the agent loop should pause.
Sourcepub fn is_over_budget(&self) -> bool
pub fn is_over_budget(&self) -> bool
Check whether the session has exceeded its cost budget.
Returns false when no budget has been set.
Sourcepub fn remaining_budget(&self) -> Option<f64>
pub fn remaining_budget(&self) -> Option<f64>
Return the remaining budget in USD, or None if no budget is set.
Sourcepub fn record_usage(
&mut self,
usage: &TokenUsage,
pricing: Option<&PricingInfo>,
) -> f64
pub fn record_usage( &mut self, usage: &TokenUsage, pricing: Option<&PricingInfo>, ) -> f64
Record token usage from a single LLM call.
Returns the incremental cost for this call in USD.
Sourcepub fn format_cost(&self) -> String
pub fn format_cost(&self) -> String
Format the total cost for display.
Sourcepub fn to_metadata(&self) -> HashMap<String, Value>
pub fn to_metadata(&self) -> HashMap<String, Value>
Export cost data for session metadata persistence.
Sourcepub fn restore_from_metadata(&mut self, metadata: &Value)
pub fn restore_from_metadata(&mut self, metadata: &Value)
Restore cost state from session metadata (for --continue sessions).
Trait Implementations§
Source§impl Clone for CostTracker
impl Clone for CostTracker
Source§fn clone(&self) -> CostTracker
fn clone(&self) -> CostTracker
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CostTracker
impl Debug for CostTracker
Source§impl Default for CostTracker
impl Default for CostTracker
Source§impl<'de> Deserialize<'de> for CostTracker
impl<'de> Deserialize<'de> for CostTracker
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for CostTracker
impl RefUnwindSafe for CostTracker
impl Send for CostTracker
impl Sync for CostTracker
impl Unpin for CostTracker
impl UnsafeUnpin for CostTracker
impl UnwindSafe for CostTracker
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
Mutably borrows from an owned value. Read more