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) -> Usage
pub fn new(prompt_tokens: u32, completion_tokens: u32) -> Usage
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) -> Usage
pub fn with_cached_tokens(self, cached_tokens: u32) -> Usage
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: Usage)
fn add_assign(&mut self, rhs: Usage)
+= operation. Read moreimpl Copy for Usage
Source§impl<'de> Deserialize<'de> for Usage
impl<'de> Deserialize<'de> for Usage
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Usage, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Usage, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
impl Eq for Usage
Source§impl Serialize for Usage
impl Serialize for Usage
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl StructuralPartialEq for Usage
Auto Trait Implementations§
impl Freeze for Usage
impl RefUnwindSafe for Usage
impl Send for Usage
impl Sync for Usage
impl Unpin for Usage
impl UnsafeUnpin for Usage
impl UnwindSafe for Usage
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.