pub struct TokenUsage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub cache_read_tokens: u32,
pub cache_creation_tokens: u32,
pub cost_usd: Option<f64>,
}Expand description
Token usage captured from a single LLM round-trip.
Why: (#50) Anthropic’s OpenRouter responses carry cache_read_input_tokens
and cache_creation_input_tokens alongside the standard prompt/completion
counts. Exposing those as first-class fields lets PerfCollector track
cache effectiveness over time. For non-Anthropic models these stay 0.
cost_usd (response-side cache-usage fix) carries OpenRouter’s
authoritative, already cache-discounted USD cost for this one round-trip
when the provider reported one; None means no authoritative cost was
available and callers must fall back to static per-token pricing.
What: Plain struct; additive (+-style) accumulation is done inline in
PerfCollector::totals.
Test: token_usage_default_is_zeros, token_usage_accumulates,
token_usage_cost_accumulates.
Fields§
§prompt_tokens: u32§completion_tokens: u32§cache_read_tokens: u32§cache_creation_tokens: u32§cost_usd: Option<f64>Authoritative per-call USD cost reported by the provider (OpenRouter),
already netting any cache discount. None when unavailable.
Implementations§
Source§impl TokenUsage
impl TokenUsage
Sourcepub fn new(
prompt: u32,
completion: u32,
cache_read: u32,
cache_creation: u32,
) -> Self
pub fn new( prompt: u32, completion: u32, cache_read: u32, cache_creation: u32, ) -> Self
Construct a TokenUsage from the four individual counts.
Why: Most call sites (tests, static-pricing paths) don’t have an
authoritative provider-reported cost; cost_usd starts None and
callers set it explicitly (e.g. UsageBlock::into_token_usage) when
one is available.
Sourcepub fn add(&mut self, other: &TokenUsage)
pub fn add(&mut self, other: &TokenUsage)
Add another usage record into this one (in place).
Why: Each phase may comprise multiple LLM turns (tool loop); we sum
them into a single PhaseRecord. cost_usd sums the two authoritative
costs when both are present; when only one side carries a cost it is
preserved rather than discarded, and the sum is None only when
neither side has one — a partial per-turn cost is still meaningful
signal that must not silently disappear.
What: Field-wise saturating add for token counts; Option<f64> sum for
cost.
Test: token_usage_accumulates, token_usage_cost_accumulates.
Trait Implementations§
Source§impl Clone for TokenUsage
impl Clone for TokenUsage
Source§fn clone(&self) -> TokenUsage
fn clone(&self) -> TokenUsage
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for TokenUsage
Source§impl Debug for TokenUsage
impl Debug for TokenUsage
Source§impl Default for TokenUsage
impl Default for TokenUsage
Source§fn default() -> TokenUsage
fn default() -> TokenUsage
Source§impl<'de> Deserialize<'de> for TokenUsage
impl<'de> Deserialize<'de> for TokenUsage
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for TokenUsage
impl PartialEq for TokenUsage
Source§impl Serialize for TokenUsage
impl Serialize for TokenUsage
impl StructuralPartialEq for TokenUsage
Auto Trait Implementations§
impl Freeze for TokenUsage
impl RefUnwindSafe for TokenUsage
impl Send for TokenUsage
impl Sync for TokenUsage
impl Unpin for TokenUsage
impl UnsafeUnpin for TokenUsage
impl UnwindSafe for TokenUsage
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
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<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