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
Wire-shape token-usage block from a chat-completions response.
Why: the wire usage object comes in two shapes (Anthropic-native flat
fields on the direct/Bedrock path, OpenRouter’s nested prompt_tokens_details
on the OpenRouter path). Deserialising into this intermediate struct — which
accepts BOTH — then mapping to Usage keeps the provider-shape knowledge
in one place.
What: carries prompt/completion/total counts, the optional Anthropic-native
flat cache fields, the optional OpenRouter nested object, and the optional
authoritative cost.
Test: usage_block_maps_flat_fields, usage_block_maps_openrouter_details,
usage_block_prefers_authoritative_cost, default_usage_block_is_zero.
Fields§
§prompt_tokens: u32Prompt tokens (including cached tokens).
completion_tokens: u32Completion tokens produced.
total_tokens: u32Prompt + completion; informational only.
cache_read_input_tokens: u32Anthropic-native (direct/Bedrock path): prompt tokens served from cache.
cache_creation_input_tokens: u32Anthropic-native (direct/Bedrock path): prompt tokens written to cache.
prompt_tokens_details: Option<PromptTokensDetails>OpenRouter-shaped nested cache counters. None when the provider
reports the flat fields instead (or omits cache accounting entirely).
cost: Option<f64>Provider-authoritative total USD cost, already netting any cache
discount. None for providers that do not report it.
Implementations§
Source§impl UsageBlock
impl UsageBlock
Sourcepub fn into_usage(self) -> Usage
pub fn into_usage(self) -> Usage
Convert this wire block into the normalized Usage.
Why: callers speak Usage; this centralises the wire-field mapping and
merges whichever of the two cache shapes the provider populated so no
call site needs to know the wire field names.
What: cache_read_tokens is the flat cache_read_input_tokens when
non-zero, else prompt_tokens_details.cached_tokens; cache_creation_tokens
follows the same fallback. cost_usd carries self.cost verbatim.
Test: usage_block_maps_flat_fields, usage_block_maps_openrouter_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<UsageBlock, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<UsageBlock, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Serialize for UsageBlock
impl Serialize for UsageBlock
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,
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