Skip to main content

UsageReader

Struct UsageReader 

Source
pub struct UsageReader { /* private fields */ }
Expand description

Watches one streamed response for its usage.

Feed it the stream as it passes — raw HTTP chunks with push, or websocket text messages with push_message — and take the reading with finish. It borrows what it is given and never alters the stream. Every reading is cumulative: snapshot and finish replace earlier readings rather than adding to them.

Implementations§

Source§

impl UsageReader

Source

pub fn new( operation: Operation, dialect: Dialect, transport: UsageTransport, ) -> Option<Self>

A reader for a stream of operation in dialect arriving over transport, or None when that stream reports no usage to watch for: an operation without a streamed form, or a transport its dialect does not stream over. An HTTP stream without a declared framing is read the way the dialect streams by default: a JSON array for Gemini, SSE for the rest.

Source

pub fn keep_usage_event(&mut self)

Also keep the latest event that carried usage, for usage_event. A vendor may report fields of its own beside the standard usage object, such as a price it charged; those are not this module’s to read, and whoever reads them needs the event they arrived in. Off by default, because the kept event is a copy: a Responses response.completed repeats the whole answer.

Source

pub fn count_output_characters(&mut self)

Count generated characters for local fallback metering, without retaining the generated text. Call before feeding any chunks. Off by default.

Source

pub fn output_characters(&self) -> u64

Generated characters observed so far; excludes framing and metadata.

Source

pub fn usage_event(&self) -> Option<Value>

The latest event that carried usage, when keep_usage_event asked for it: the Claude message_delta (or the message_start before any delta), the Chat chunk with a usage object, the Responses terminal event, the Gemini chunk with usageMetadata. None when no event carried usage.

Source

pub fn push(&mut self, chunk: &[u8])

Observe raw HTTP bytes. Chunks need not align with events.

Source

pub fn push_message(&mut self, text: &str)

Observe one websocket text message, which is one complete event.

Source

pub fn output_started(&self) -> bool

Whether a nonempty text, reasoning or tool argument delta has arrived. Response headers, keepalives and usage-only events do not count.

Source

pub fn snapshot(&self) -> Option<NormalizedUsage>

The reading so far. It is Partial until the event that settles it has been seen.

Source

pub fn finish(self, end: UsageStreamEnd) -> Option<NormalizedUsage>

The final reading. A stream that was cut is never complete, whatever it reported before it stopped.

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> 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> ResourceSend for T
where T: Send + ?Sized,

Source§

impl<T> ResourceSync for T
where T: Sync + ?Sized,

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.