Skip to main content

Usage

Struct Usage 

Source
#[non_exhaustive]
pub struct Usage { pub input_tokens: Option<u64>, pub output_tokens: Option<u64>, pub total_tokens: Option<u64>, pub cached_input_tokens: Option<u64>, pub cache_creation_input_tokens: Option<u64>, pub tool_use_prompt_tokens: Option<u64>, pub reasoning_tokens: Option<u64>, pub cost: Option<Cost>, /* private fields */ }
Expand description

The token usage a provider reported for one completion.

Every provider mapping keeps one contract, so the counters read the same way on every provider:

  • cached_input_tokens + cache_creation_input_tokens <= input_tokens: input counts every prompt token, cache reads and writes included.
  • reasoning_tokens <= output_tokens: output counts every generated token, reasoning included.
  • total_tokens == input_tokens + output_tokens, absent unless both are reported.

A counter the provider did not send is None; a reported zero is Some(0). Serialized as the same keys, absent when None.

use rig_core::completion::Usage;

let usage = Usage::new().input_tokens(12).output_tokens(3).total_tokens(15);
assert_eq!(usage.input_tokens, Some(12));
assert!(usage.cost.is_none());

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§input_tokens: Option<u64>

Every input token of the request: uncached, read from a cache, written to a cache, and any prompt a provider’s hosted tools added.

§output_tokens: Option<u64>

Every output token, reasoning included.

§total_tokens: Option<u64>

input_tokens + output_tokens; absent unless both are reported.

§cached_input_tokens: Option<u64>

The part of input_tokens read from a provider-managed cache.

§cache_creation_input_tokens: Option<u64>

The part of input_tokens written to a provider-managed cache.

§tool_use_prompt_tokens: Option<u64>

The part of input_tokens a provider’s hosted tools added to the prompt.

§reasoning_tokens: Option<u64>

The part of output_tokens spent on internal reasoning (“thinking”, “thoughts”).

§cost: Option<Cost>

What the turn cost in USD, when known. Never derived from the token counters here, and none of them is derived from it. A cost the provider reports is its figure. One priced from the model’s catalog spec (Pricing::cost, from the facts the wire was connected with) is the standard-tier list price of the counted tokens: it leaves out the service tier, long-context price tiers and hosted-tool fees (web search, code execution), so it can be lower than the bill.

Implementations§

Source§

impl Usage

Source

pub const fn new() -> Self

No counter and no cost reported.

Source

pub fn is_reported(&self) -> bool

Whether the provider reported any counter or a cost.

Source

pub fn tokens_complete(&self) -> bool

Whether this sum has a figure from every call for each of input, output and total. false means a summed counter can be lower than what the calls used. A counter no call reported stays None, which does not make the sum incomplete, and the detail counters such as Self::reasoning_tokens are not considered.

Source

pub fn context_tokens(&self) -> Option<u64>

The tokens the call read and wrote, what a conversation’s next request sends again: Self::total_tokens, else the input and output tokens. None without input tokens.

Source

pub fn input_tokens(self, tokens: impl Into<Option<u64>>) -> Self

Set, or with None clear, Self::input_tokens.

Source

pub fn output_tokens(self, tokens: impl Into<Option<u64>>) -> Self

Set, or with None clear, Self::output_tokens.

Source

pub fn total_tokens(self, tokens: impl Into<Option<u64>>) -> Self

Set, or with None clear, Self::total_tokens.

Source

pub fn cached_input_tokens(self, tokens: impl Into<Option<u64>>) -> Self

Set, or with None clear, Self::cached_input_tokens.

Source

pub fn cache_creation_input_tokens(self, tokens: impl Into<Option<u64>>) -> Self

Set, or with None clear, Self::cache_creation_input_tokens.

Source

pub fn tool_use_prompt_tokens(self, tokens: impl Into<Option<u64>>) -> Self

Set, or with None clear, Self::tool_use_prompt_tokens.

Source

pub fn reasoning_tokens(self, tokens: impl Into<Option<u64>>) -> Self

Set, or with None clear, Self::reasoning_tokens.

Source

pub fn cost(self, cost: impl Into<Option<Cost>>) -> Self

Set, or with None clear, Self::cost.

Trait Implementations§

Source§

impl Add for Usage

Source§

type Output = Usage

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign for Usage

Token counters add where an unreported side adds nothing. Cost sums only when both sides have one: a turn whose cost is unknown makes the sum unknown, rather than too low. A side that reports nothing at all (Usage::is_reported is false) adds nothing, so a fold from Usage::default keeps its first turn’s cost.

The sum is no longer complete when either side is not, when only one side has input, output or total, or when the right side reports nothing, which counts as a call that reported nothing. An empty left side counts as the start of a fold, so fold calls from Usage::default: empty_call + call leaves no mark.

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl Clone for Usage

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Usage

Source§

impl Debug for Usage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Usage

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Usage

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<&EmbeddingUsage> for Usage

The provider’s counts are passed through unchanged, so a reply whose prompt_tokens exceeds its total_tokens keeps both as sent.

Source§

fn from(usage: &EmbeddingUsage) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Usage

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Usage

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Usage

Auto Trait Implementations§

§

impl Forget for Usage

§

impl Freeze for Usage

§

impl Move 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> IntoToolOutput for T
where T: Serialize + 'static,

Source§

fn into_tool_output(self) -> Result<ToolOutput, ToolExecutionError>

Convert this value without routing structured data through a string.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more