Skip to main content

TokenUsage

Struct TokenUsage 

Source
#[non_exhaustive]
pub struct TokenUsage { pub prompt: u64, pub completion: u64, pub total: u64, pub cached: u64, pub reasoning: u64, }
Expand description

Token consumption reported by a provider, as one increment.

This is the payload of Event::Usage, which is incremental: these five numbers describe the tokens consumed since the previous usage event of the same generation, and a consumer adds them up.

use reef_core::runtime::model;

// The common case: a provider that reports one total per call.
let usage = model::TokenUsage::new(1_200, 340);
assert_eq!(usage.total, 1_540);

// A provider that also breaks its counts down. Neither detail is added to
// the count it belongs to; both are already inside it.
let detailed = model::TokenUsage::new(1_200, 340)
    .with_cached(900)
    .with_reasoning(210);
assert_eq!(detailed.prompt, 1_200);
assert_eq!(detailed.total, 1_540);
assert_eq!(detailed.cached, 900);
assert_eq!(detailed.reasoning, 210);

§What the five numbers mean

The two detail fields are parts of the counts above them, not additions to them, which is the convention providers that report them already use. An adapter that added them separately would make every budget over-count.

  • prompt is every input token, cached included.
  • completion is every output token, reasoning included.
  • total is the provider’s own total for this increment. Self::new sets it to prompt + completion, which is what a provider that reports no separate total means; Self::with_total states a different one, for a provider that does.

§The fields are public, deliberately

Every other value type in this crate hides its fields, and this one does not, because the rule is about who has to construct the value rather than about consistency of style. A struct that a trait method returns — Info, two types away — must be buildable by every provider that will ever exist, so a field added later would break all of them, and its fields are private with a builder in front. A TokenUsage is built inside one optional event, and read by policies that want all five numbers at once. Privatising the fields would add five getters to the five setters, make the #[non_exhaustive] above a no-op that then has to be removed, and buy nothing: the attribute already stops an external struct literal, so a sixth field stays additive either way.

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.
§prompt: u64

Input tokens consumed, including cached.

§completion: u64

Output tokens generated, including reasoning.

§total: u64

Provider-reported total for this increment.

§cached: u64

The part of prompt that was served from cache.

§reasoning: u64

The part of completion that was reasoning.

Implementations§

Source§

impl TokenUsage

Source

pub const fn new(prompt: u64, completion: u64) -> Self

Reports one increment of input and output tokens.

total becomes prompt + completion, saturating; the two detail fields are zero. Each is stated separately by the builder below when a provider reports it.

Source

pub const fn with_total(self, total: u64) -> Self

States a provider-reported total that is not prompt + completion.

Source

pub const fn with_cached(self, cached: u64) -> Self

States how much of prompt came from cache.

Source

pub const fn with_reasoning(self, reasoning: u64) -> Self

States how much of completion was reasoning.

Trait Implementations§

Source§

impl Clone for TokenUsage

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 TokenUsage

Source§

impl Debug for TokenUsage

Source§

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

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

impl Default for TokenUsage

Source§

fn default() -> Self

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

impl Eq for TokenUsage

Source§

impl PartialEq for TokenUsage

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 StructuralPartialEq for TokenUsage

Auto Trait Implementations§

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<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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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.