Skip to main content

CostTracker

Struct CostTracker 

Source
pub struct CostTracker { /* private fields */ }
Expand description

Tracks token usage and cost across an agent run, persisting to the session directory.

CostTracker is designed to be used alongside SessionWriter. It writes a cost.json file into the same session directory and optionally updates the .meta.json file with cost summary fields.

Implementations§

Source§

impl CostTracker

Source

pub fn new(session_dir: PathBuf, model: &str, provider: &str) -> Self

Create a new CostTracker for the given session directory.

session_dir should be the same directory used by SessionWriter. Pricing is looked up from the bundled providers.toml via pricing_for().

Source

pub fn record_usage(&mut self, usage: TokenUsage, latency_ms: u64)

Record token usage and latency from a single LLM call.

Call this from the integration layer after each runtime.run(), passing outcome.total_usage and outcome.llm_latency_ms from the RuntimeOutcome.

Source

pub fn accumulated_usage(&self) -> TokenUsage

Return the accumulated token usage so far.

Source

pub fn accumulated_latency_ms(&self) -> u64

Return the accumulated LLM latency in milliseconds.

Source

pub fn cost_usd(&self) -> Option<f64>

Compute the cost in USD for the accumulated usage.

Returns None if pricing is unknown for the model.

Source

pub fn cost_data(&self) -> CostData

Build the current CostData snapshot.

Source

pub fn write_cost_json(&self) -> Result<PathBuf>

Write the cost data to cost.json in the session directory.

Returns the path to the written file.

Source

pub fn update_meta_with_cost(&self) -> Result<()>

Update the .meta.json file in the session directory with cost summary fields.

Reads the existing meta file, adds cost fields, and writes it back. If the meta file doesn’t exist or can’t be read, this is a no-op.

Source

pub fn finish(&mut self) -> Result<()>

Finalise the tracker: write cost.json and update .meta.json.

After calling this, the tracker is marked as finished and subsequent calls are no-ops.

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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, 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<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