pub struct CostTracker { /* private fields */ }Expand description
Thread-safe cumulative cost tracker.
Construct once per run (or share one per session), attach it to a
crate::token_counter::TokenTrackingLLM with
with_cost_tracker, optionally share the same Arc with an
AgentExecutor::with_cost_tracker for hard budget enforcement.
Implementations§
Source§impl CostTracker
impl CostTracker
Sourcepub fn new(table: impl Into<Arc<PricingTable>>) -> Self
pub fn new(table: impl Into<Arc<PricingTable>>) -> Self
Tracker over the given pricing table.
Sourcepub fn with_builtin_prices() -> Self
pub fn with_builtin_prices() -> Self
Tracker over the built-in price snapshot.
Sourcepub fn with_scope(self, scope: impl Into<String>) -> Self
pub fn with_scope(self, scope: impl Into<String>) -> Self
Labels this tracker with a run/session id (carried in reports and
emitted cost events).
Sourcepub fn with_metrics_sink(self, sink: Arc<dyn MetricsSink>) -> Self
pub fn with_metrics_sink(self, sink: Arc<dyn MetricsSink>) -> Self
Attaches an observability sink: every record emits one
ObsEvent::Cost. Sink failures are warned, never propagated.
Sourcepub fn pricing(&self) -> &PricingTable
pub fn pricing(&self) -> &PricingTable
The pricing table backing this tracker.
Sourcepub async fn record(
&self,
provider: Option<&str>,
model: &str,
prompt_tokens: usize,
completion_tokens: usize,
) -> f64
pub async fn record( &self, provider: Option<&str>, model: &str, prompt_tokens: usize, completion_tokens: usize, ) -> f64
Records one LLM call and returns its USD cost.
Models absent from the table price at 0.0 but still count toward token / call aggregates. The observability event (when attached) is exported after aggregation; export failure only logs a warning.
Sourcepub async fn record_usage(
&self,
provider: Option<&str>,
model: &str,
usage: &TokenUsage,
) -> f64
pub async fn record_usage( &self, provider: Option<&str>, model: &str, usage: &TokenUsage, ) -> f64
Records one call from a provider-annotated TokenUsage.
Sourcepub async fn total_cost_usd(&self) -> f64
pub async fn total_cost_usd(&self) -> f64
Cumulative USD spend (the value the budget gate reads).
Sourcepub async fn report(&self) -> CostReport
pub async fn report(&self) -> CostReport
Snapshot report of everything recorded so far.
Sourcepub async fn records(&self) -> Vec<CostRecord>
pub async fn records(&self) -> Vec<CostRecord>
Every individual call recorded so far (oldest first).