pub struct UsageBlock {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
pub cache_read_input_tokens: u32,
pub cache_creation_input_tokens: u32,
pub prompt_tokens_details: Option<PromptTokensDetails>,
pub cost: Option<f64>,
}Expand description
Token usage block from the API response.
Why: Mapping the wire usage object to our internal TokenUsage is easiest
when we first deserialise into this intermediate struct.
What: Carries prompt_tokens, completion_tokens, and total_tokens from
the wire; optional Anthropic-native cache fields (flat
cache_read_input_tokens / cache_creation_input_tokens, present on the
direct/Bedrock path) AND the OpenRouter-shaped nested
prompt_tokens_details object — both are accepted so either provider’s
wire shape populates the canonical TokenUsage cache fields. cost is
OpenRouter’s authoritative, already-discounted USD cost for the call (only
present when OpenRouter includes detailed usage accounting).
Test: usage_block_maps_to_token_usage,
usage_block_maps_openrouter_prompt_tokens_details,
usage_block_prefers_authoritative_cost.
Fields§
§prompt_tokens: u32Tokens consumed by the prompt (including cached tokens).
completion_tokens: u32Tokens produced by the completion.
total_tokens: u32Sum of prompt + completion; informational only.
cache_read_input_tokens: u32Anthropic-native (direct/Bedrock path): prompt tokens served from the cache (not re-computed).
cache_creation_input_tokens: u32Anthropic-native (direct/Bedrock path): prompt tokens written into the cache this turn.
prompt_tokens_details: Option<PromptTokensDetails>OpenRouter-shaped nested cache counters (OpenAI wire style). None
when the provider reports the flat Anthropic-native fields instead (or
omits cache accounting entirely).
cost: Option<f64>OpenRouter’s authoritative total USD cost for this call, already
netting any cache discount. None for providers that don’t report it
(direct/Bedrock) or when OpenRouter’s detailed-usage accounting wasn’t
returned.
Implementations§
Source§impl UsageBlock
impl UsageBlock
Sourcepub fn into_token_usage(self) -> TokenUsage
pub fn into_token_usage(self) -> TokenUsage
Convert this wire usage block into the crate’s TokenUsage.
Why: PerfCollector speaks TokenUsage; this conversion centralises the
mapping so callers don’t need to know the wire field names, and merges
whichever of the two wire cache shapes (Anthropic-native flat fields vs.
OpenRouter’s nested prompt_tokens_details) the provider populated.
What: cache_read_tokens is cache_read_input_tokens when non-zero,
else prompt_tokens_details.cached_tokens; cache_creation_tokens
follows the same fallback using cache_creation_input_tokens /
cache_write_tokens. cost_usd on the resulting TokenUsage carries
self.cost verbatim so callers can prefer OpenRouter’s authoritative,
cache-aware price over a static per-token recompute.
Test: usage_block_maps_to_token_usage,
usage_block_maps_openrouter_prompt_tokens_details,
usage_block_prefers_authoritative_cost.
Trait Implementations§
Source§impl Clone for UsageBlock
impl Clone for UsageBlock
Source§fn clone(&self) -> UsageBlock
fn clone(&self) -> UsageBlock
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for UsageBlock
impl Debug for UsageBlock
Source§impl Default for UsageBlock
impl Default for UsageBlock
Source§fn default() -> UsageBlock
fn default() -> UsageBlock
Source§impl<'de> Deserialize<'de> for UsageBlock
impl<'de> Deserialize<'de> for UsageBlock
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>,
Auto Trait Implementations§
impl Freeze for UsageBlock
impl RefUnwindSafe for UsageBlock
impl Send for UsageBlock
impl Sync for UsageBlock
impl Unpin for UsageBlock
impl UnsafeUnpin for UsageBlock
impl UnwindSafe for UsageBlock
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