Skip to main content

CostTracker

Struct CostTracker 

Source
pub struct CostTracker { /* private fields */ }
Available on crate feature 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

Source

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.

Source

pub fn with_pricing(pricing: Vec<ModelPricing>) -> CostTracker

Creates a new CostTracker with the specified pricing table.

§Arguments
  • pricing - A list of ModelPricing entries to use for cost computation.
Source

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.

Source

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_1k

Returns 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.

Trait Implementations§

Source§

impl Default for CostTracker

Source§

fn default() -> CostTracker

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more