pub struct CostTracker { /* private fields */ }Expand description
Cost tracker for API usage monitoring and budget enforcement.
Implementations§
Source§impl CostTracker
impl CostTracker
Sourcepub fn new(
config: CostConfig,
workspace_dir: &Path,
) -> Result<CostTracker, Error>
pub fn new( config: CostConfig, workspace_dir: &Path, ) -> Result<CostTracker, Error>
Create a new cost tracker.
Sourcepub fn session_id(&self) -> &str
pub fn session_id(&self) -> &str
Get the session ID.
Sourcepub fn check_budget(
&self,
estimated_cost_usd: f64,
) -> Result<BudgetCheck, Error>
pub fn check_budget( &self, estimated_cost_usd: f64, ) -> Result<BudgetCheck, Error>
Check if a request is within budget.
Sourcepub fn record_usage(&self, usage: TokenUsage) -> Result<(), Error>
pub fn record_usage(&self, usage: TokenUsage) -> Result<(), Error>
Record a usage event without per-agent attribution.
Sourcepub fn record_usage_with_agent(
&self,
usage: TokenUsage,
agent_alias: Option<&str>,
) -> Result<(), Error>
pub fn record_usage_with_agent( &self, usage: TokenUsage, agent_alias: Option<&str>, ) -> Result<(), Error>
Record a usage event attributed to a specific agent alias. When
[cost].track_per_agent is false the alias is dropped before
persistence.
Sourcepub fn get_summary(&self) -> Result<CostSummary, Error>
pub fn get_summary(&self) -> Result<CostSummary, Error>
Get the current cost summary. When [cost].track_per_agent is
enabled, the response includes a by_agent rollup over today’s
records.
Sourcepub fn get_summary_in_bounds(
&self,
from: Option<DateTime<Utc>>,
to: Option<DateTime<Utc>>,
) -> Result<CostSummary, Error>
pub fn get_summary_in_bounds( &self, from: Option<DateTime<Utc>>, to: Option<DateTime<Utc>>, ) -> Result<CostSummary, Error>
Filter persisted records by [from, to) (either side None is
unbounded) and roll up by_model / by_agent / window totals.
Bounds come from the caller (the dashboard computes them in the
operator’s local timezone); the tracker doesn’t decide what
“today” means.
Sourcepub fn get_summary_for_agent(
&self,
agent_alias: &str,
) -> Result<CostSummary, Error>
pub fn get_summary_for_agent( &self, agent_alias: &str, ) -> Result<CostSummary, Error>
Get the current cost summary scoped to a single agent alias. The
session/day/month figures and by_model are filtered to records
attributed to that alias; by_agent is left empty since the
caller already chose the dimension.
Source§impl CostTracker
impl CostTracker
Sourcepub fn get_or_init_global(
config: CostConfig,
workspace_dir: &Path,
) -> Option<Arc<CostTracker>>
pub fn get_or_init_global( config: CostConfig, workspace_dir: &Path, ) -> Option<Arc<CostTracker>>
Return the process-global CostTracker, creating it on first call.
Subsequent calls (from gateway or channels, whichever starts second)
receive the same Arc. Returns None when cost tracking is disabled
or initialisation fails.
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<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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