pub struct Usage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
pub cached_tokens: Option<u32>,
}Expand description
Token usage for one conversation.
Field names match the OpenAI wire format; total_tokens follows the
vendor’s convention (not necessarily the sum of the other two). Default
= all zeros, with nothing reported.
Presence is carried by the enclosing type: ChatResponse::usage /
StreamEvent::Done::usage are Option<Usage> — None means the
endpoint did not report usage for this turn, Some means the reported
values. The distinction matters for observability: “not reported” is not
the same as “reportedly zero” (the Agent layer also tracks it in
RunSummary::usage_omitted).
cached_tokens carries the same distinction inside
a reported usage: the prompt-cache breakdown is optional per turn even
when the totals are present.
Fields§
§prompt_tokens: u32Input tokens for this turn.
completion_tokens: u32Output tokens for this turn.
total_tokens: u32Total for this turn (vendor convention).
cached_tokens: Option<u32>Prompt tokens served from the provider’s prompt cache.
A subset of prompt_tokens, not an addition to
it (vendors bill cached input at a lower rate).
None = the endpoint did not report a cache breakdown for this turn;
Some(0) = it reported that nothing hit the cache. Endpoints differ
here — prompt-caching ones report the breakdown (OpenAI / DeepSeek as
prompt_tokens_details.cached_tokens), others omit it — so a reported
zero must not be inferred from an omission. When turns are summed,
only the reported parts contribute and
RunSummary::cache_omitted marks whether
the sum is a lower bound.
Implementations§
Source§impl Usage
impl Usage
Sourcepub fn new(prompt_tokens: u32, completion_tokens: u32) -> Self
pub fn new(prompt_tokens: u32, completion_tokens: u32) -> Self
Constructs from input / output counts; the total is summed
automatically and no cache breakdown is reported
(with_cached_tokens sets one).
Sourcepub fn with_cached_tokens(self, cached_tokens: u32) -> Self
pub fn with_cached_tokens(self, cached_tokens: u32) -> Self
Sets the prompt tokens served from the provider’s cache, marking the
breakdown as reported (Some in cached_tokens).
Trait Implementations§
Source§impl AddAssign for Usage
Usage accumulates per turn (the Agent layer sums tokens across turns).
impl AddAssign for Usage
Usage accumulates per turn (the Agent layer sums tokens across turns).
cached_tokens sums the parts that were reported: a turn that omitted the
breakdown contributes nothing rather than zero.
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read more