pub struct Usage {
pub input_tokens: Option<usize>,
pub input_tokens_details: Option<FxHashMap<String, usize>>,
pub output_tokens: Option<usize>,
pub output_tokens_details: Option<FxHashMap<String, usize>>,
pub prompt_tokens: Option<usize>,
pub completion_tokens: Option<usize>,
pub total_tokens: Option<usize>,
pub completion_tokens_details: Option<FxHashMap<String, usize>>,
}
Expand description
Token usage statistics for OpenAI API requests.
This structure contains detailed information about token consumption during API requests, including both input (prompt) and output (completion) tokens. Different fields may be populated depending on the specific API endpoint and model used.
§Fields
input_tokens
- Number of tokens in the input/promptinput_tokens_details
- Detailed breakdown of input token usage by categoryoutput_tokens
- Number of tokens in the output/completionoutput_tokens_details
- Detailed breakdown of output token usage by categoryprompt_tokens
- Legacy field for input tokens (may be deprecated)completion_tokens
- Legacy field for output tokens (may be deprecated)total_tokens
- Total number of tokens used (input + output)completion_tokens_details
- Detailed breakdown of completion token usage
§Note
Not all fields will be populated for every request. The availability of detailed token breakdowns depends on the model and API endpoint being used.
§Example
use openai_tools::common::Usage;
// Create usage statistics manually (typically done by API response parsing)
let usage = Usage::new(
Some(25), // input tokens
None, // no detailed input breakdown
Some(50), // output tokens
None, // no detailed output breakdown
Some(25), // prompt tokens (legacy)
Some(50), // completion tokens (legacy)
Some(75), // total tokens
None, // no detailed completion breakdown
);
if let Some(total) = usage.total_tokens {
println!("Request used {} tokens total", total);
}
Fields§
§input_tokens: Option<usize>
§input_tokens_details: Option<FxHashMap<String, usize>>
§output_tokens: Option<usize>
§output_tokens_details: Option<FxHashMap<String, usize>>
§prompt_tokens: Option<usize>
§completion_tokens: Option<usize>
§total_tokens: Option<usize>
§completion_tokens_details: Option<FxHashMap<String, usize>>
Implementations§
Source§impl Usage
impl Usage
Sourcepub fn new(
input_tokens: Option<usize>,
input_tokens_details: Option<FxHashMap<String, usize>>,
output_tokens: Option<usize>,
output_tokens_details: Option<FxHashMap<String, usize>>,
prompt_tokens: Option<usize>,
completion_tokens: Option<usize>,
total_tokens: Option<usize>,
completion_tokens_details: Option<FxHashMap<String, usize>>,
) -> Self
pub fn new( input_tokens: Option<usize>, input_tokens_details: Option<FxHashMap<String, usize>>, output_tokens: Option<usize>, output_tokens_details: Option<FxHashMap<String, usize>>, prompt_tokens: Option<usize>, completion_tokens: Option<usize>, total_tokens: Option<usize>, completion_tokens_details: Option<FxHashMap<String, usize>>, ) -> Self
Constructs a new Usage
.
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
Auto Trait Implementations§
impl Freeze for Usage
impl RefUnwindSafe for Usage
impl Send for Usage
impl Sync for Usage
impl Unpin 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
Converts
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>
Converts
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian()
.