pub struct CostTracker { /* private fields */ }eval only.Expand description
Tracks cost and latency metrics from agent event streams.
Uses per-model pricing tables to compute estimated USD costs from
token counts extracted from Event streams.
§Example
use adk_eval::cost_tracker::CostTracker;
use adk_eval::pricing::ModelPricing;
// Use default pricing
let tracker = CostTracker::new();
// Or provide custom pricing
let custom_pricing = vec![
ModelPricing::new("my-model", 0.001, 0.002),
];
let tracker = CostTracker::with_pricing(custom_pricing);Implementations§
Source§impl CostTracker
impl CostTracker
Sourcepub fn new() -> CostTracker
pub fn new() -> CostTracker
Creates a new CostTracker with default pricing for common models.
Default pricing includes Google Gemini, OpenAI GPT, and Anthropic Claude model families.
Sourcepub fn with_pricing(pricing: Vec<ModelPricing>) -> CostTracker
pub fn with_pricing(pricing: Vec<ModelPricing>) -> CostTracker
Creates a new CostTracker with the specified pricing table.
§Arguments
pricing- A list ofModelPricingentries to use for cost computation.
Sourcepub fn extract_metrics(
&self,
events: &[Event],
duration: Duration,
) -> CostMetrics
pub fn extract_metrics( &self, events: &[Event], duration: Duration, ) -> CostMetrics
Extract cost metrics from an event stream.
Iterates over events looking for UsageMetadata
on LLM responses. Token counts are summed across all events that contain
usage metadata. If no usage metadata is found, token counts default to zero.
The duration parameter is converted to milliseconds for the latency_ms field.
Note: The cost_usd field will be None because the model name is not
available on the Event struct. Use compute_cost
separately when the model name is known.
§Arguments
events- Slice of events from an agent execution.duration- Wall-clock duration of the execution.
§Returns
A CostMetrics struct with aggregated token counts and latency.
Sourcepub fn compute_cost(
&self,
model: &str,
prompt_tokens: u64,
completion_tokens: u64,
) -> Option<f64>
pub fn compute_cost( &self, model: &str, prompt_tokens: u64, completion_tokens: u64, ) -> Option<f64>
Compute cost from token counts and model name.
Uses the formula:
(prompt_tokens / 1000.0) * input_cost_per_1k + (completion_tokens / 1000.0) * output_cost_per_1kReturns None if the model is not found in the pricing table.
§Arguments
model- Model identifier to look up pricing for.prompt_tokens- Number of input tokens.completion_tokens- Number of output tokens.