pub struct Usage {
pub input_tokens: u64,
pub output_tokens: u64,
pub cached_input_tokens: u64,
}Expand description
Three-field token usage as returned by OpenAI’s chat-completion and responses APIs.
Chat Completions exposes the cached count under
prompt_tokens_details.cached_tokens; the Responses API uses
cached_input_tokens directly. The From impls in this crate flatten
both shapes onto these fields.
Fields§
§input_tokens: u64Fresh input/prompt tokens (not served from the prompt cache).
output_tokens: u64Output/completion tokens.
cached_input_tokens: u64Input tokens that were served from the prompt cache (cache hit).
Implementations§
Source§impl Usage
impl Usage
Sourcepub fn total_tokens(&self) -> u64
pub fn total_tokens(&self) -> u64
Total tokens billed (input + output + cached_input).
Sourcepub fn from_chat_completions(
prompt_tokens: u64,
completion_tokens: u64,
cached_tokens: u64,
) -> Self
pub fn from_chat_completions( prompt_tokens: u64, completion_tokens: u64, cached_tokens: u64, ) -> Self
Build a Usage from a Chat Completions response.
OpenAI reports prompt_tokens as including cached tokens, so we
subtract cached_tokens from prompt_tokens to get the fresh
input count this struct expects.
Sourcepub fn from_responses_api(
input_tokens: u64,
output_tokens: u64,
cached_input_tokens: u64,
) -> Self
pub fn from_responses_api( input_tokens: u64, output_tokens: u64, cached_input_tokens: u64, ) -> Self
Build a Usage from a Responses-API response.
On the Responses API, input_tokens already excludes cached tokens
(cached_input_tokens is reported separately).