pub struct TokenUsage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
pub cache_hit_tokens: u32,
pub cache_miss_tokens: u32,
pub reasoning_tokens: u32,
}Expand description
Token usage data from an LLM response.
Fields§
§prompt_tokens: u32§completion_tokens: u32§total_tokens: u32§cache_hit_tokens: u32Input tokens served from the provider’s prompt cache.
Invariant (normalised across providers):
cache_hit_tokens + cache_miss_tokens == total input tokens.
A “hit” is a token read from cache; everything else processed for the
prompt (fresh input and tokens written to cache) counts as a “miss”.
This lets consumers compute a cache-hit rate as
cache_hit / (cache_hit + cache_miss) uniformly, regardless of which
provider reported the usage. DeepSeek already reports the split this
way (prompt_tokens = hit + miss); Anthropic reports input_tokens,
cache_read_input_tokens and cache_creation_input_tokens separately,
so its parser folds input + creation into cache_miss_tokens.
cache_miss_tokens: u32§reasoning_tokens: u32Reasoning / thinking tokens emitted by models that support extended thinking (DeepSeek R1, OpenAI o1, Anthropic extended thinking). Adds to the cost total because the model spent compute producing them. Default 0 for models that don’t report this separately.
Implementations§
Source§impl TokenUsage
impl TokenUsage
Sourcepub fn accumulate(self, other: TokenUsage) -> TokenUsage
pub fn accumulate(self, other: TokenUsage) -> TokenUsage
Saturating element-wise sum. Used to accumulate across LLM calls.
Trait Implementations§
Source§impl Clone for TokenUsage
impl Clone for TokenUsage
impl Copy for TokenUsage
Source§impl Debug for TokenUsage
impl Debug for TokenUsage
Source§impl Default for TokenUsage
impl Default for 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>,
impl Eq for TokenUsage
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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.