pub struct UsageTracker { /* private fields */ }Expand description
Accumulates token usage and execution statistics over a single task run.
The default task handlers create one UsageTracker per task, thread it
through run_tool_loop, and - when usage metadata is
enabled - merge metadata into the task’s
metadata field once the task reaches a terminal state.
Counters are split into two groups:
- Token usage (
prompt_tokens/completion_tokens/total_tokens), summed from eachCompletionUsagethe gateway returns. Everyadd_token_usagecall also bumps an internalllm_callscounter sometadatacan decide whether ausageblock is meaningful. - Execution stats (
iterations/messages/tool_calls/failed_tools), counted as the agent loop runs.
All mutators take &self (interior mutability via a Mutex) so the
tracker can be shared behind a shared reference across .await points.
Implementations§
Source§impl UsageTracker
impl UsageTracker
pub fn new() -> Self
Sourcepub fn add_token_usage(&self, usage: &CompletionUsage)
pub fn add_token_usage(&self, usage: &CompletionUsage)
Add the token counts from one chat-completion response and record that an LLM call was made.
Sourcepub fn increment_iteration(&self)
pub fn increment_iteration(&self)
Count one pass through the agent’s model ↔ tool loop.
Sourcepub fn add_messages(&self, count: usize)
pub fn add_messages(&self, count: usize)
Count count conversation messages produced during the loop (the
default handlers use this for tool-result messages fed back to the
model).
Sourcepub fn increment_tool_calls(&self)
pub fn increment_tool_calls(&self)
Count one dispatched tool call.
Sourcepub fn increment_failed_tools(&self)
pub fn increment_failed_tools(&self)
Count one tool call that failed (missing handler or handler error).
Trait Implementations§
Source§impl Debug for UsageTracker
impl Debug for UsageTracker
Source§impl Default for UsageTracker
impl Default for UsageTracker
Source§fn default() -> UsageTracker
fn default() -> UsageTracker
Auto Trait Implementations§
impl !Freeze for UsageTracker
impl RefUnwindSafe for UsageTracker
impl Send for UsageTracker
impl Sync for UsageTracker
impl Unpin for UsageTracker
impl UnsafeUnpin for UsageTracker
impl UnwindSafe for UsageTracker
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
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