pub struct Usage {
pub prompt_tokens: Option<u32>,
pub completion_tokens: Option<u32>,
pub total_tokens: Option<u32>,
pub reasoning_tokens: Option<u32>,
pub cache_read_tokens: Option<u32>,
pub cache_write_tokens: Option<u32>,
pub cost_usd: Option<f64>,
pub stop_reason: Option<String>,
}Expand description
Token usage information from a model response.
Providers should emit this at the end of each response stream. Token counts and costs are optional since not all providers report them.
Fields§
§prompt_tokens: Option<u32>Number of tokens in the prompt/input.
completion_tokens: Option<u32>Number of tokens in the completion/output.
total_tokens: Option<u32>Total tokens (prompt + completion).
reasoning_tokens: Option<u32>Tokens used for reasoning/thinking (for reasoning models).
cache_read_tokens: Option<u32>Tokens read from cache (for providers with prompt caching).
cache_write_tokens: Option<u32>Tokens written to cache.
cost_usd: Option<f64>Estimated cost in USD for this request.
stop_reason: Option<String>Provider-specific reason the generation stopped (e.g. stop, length, tool_use).
Implementations§
Source§impl Usage
impl Usage
Sourcepub const fn new(prompt_tokens: u32, completion_tokens: u32) -> Self
pub const fn new(prompt_tokens: u32, completion_tokens: u32) -> Self
Creates a new usage with basic token counts.
Sourcepub const fn with_reasoning_tokens(self, tokens: u32) -> Self
pub const fn with_reasoning_tokens(self, tokens: u32) -> Self
Adds reasoning token count.
Sourcepub const fn with_cache_tokens(self, read: u32, write: u32) -> Self
pub const fn with_cache_tokens(self, read: u32, write: u32) -> Self
Adds cache token counts.
Sourcepub fn with_stop_reason(self, reason: impl Into<String>) -> Self
pub fn with_stop_reason(self, reason: impl Into<String>) -> Self
Adds provider stop reason metadata.
Sourcepub fn accumulate(&mut self, other: &Self)
pub fn accumulate(&mut self, other: &Self)
Accumulates usage from another instance.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Usage
impl<'de> Deserialize<'de> for Usage
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>,
Deserialize this value from the given Serde deserializer. Read more
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
Mutably borrows from an owned value. Read more