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
impl CostTracker
Sourcepub fn new(session_dir: PathBuf, model: &str, provider: &str) -> Self
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().
Sourcepub fn record_usage(&mut self, usage: TokenUsage, latency_ms: u64)
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.
Sourcepub fn accumulated_usage(&self) -> TokenUsage
pub fn accumulated_usage(&self) -> TokenUsage
Return the accumulated token usage so far.
Sourcepub fn accumulated_latency_ms(&self) -> u64
pub fn accumulated_latency_ms(&self) -> u64
Return the accumulated LLM latency in milliseconds.
Sourcepub fn cost_usd(&self) -> Option<f64>
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.
Sourcepub fn write_cost_json(&self) -> Result<PathBuf>
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.
Sourcepub fn update_meta_with_cost(&self) -> Result<()>
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.
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
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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