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
impl UsageReader
Sourcepub fn new(
operation: Operation,
dialect: Dialect,
transport: UsageTransport,
) -> Option<Self>
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.
Sourcepub fn keep_usage_event(&mut self)
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.
Sourcepub fn count_output_characters(&mut self)
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.
Sourcepub fn output_characters(&self) -> u64
pub fn output_characters(&self) -> u64
Generated characters observed so far; excludes framing and metadata.
Sourcepub fn usage_event(&self) -> Option<Value>
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.
Sourcepub fn push(&mut self, chunk: &[u8])
pub fn push(&mut self, chunk: &[u8])
Observe raw HTTP bytes. Chunks need not align with events.
Sourcepub fn push_message(&mut self, text: &str)
pub fn push_message(&mut self, text: &str)
Observe one websocket text message, which is one complete event.
Sourcepub fn output_started(&self) -> bool
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.
Sourcepub fn snapshot(&self) -> Option<NormalizedUsage>
pub fn snapshot(&self) -> Option<NormalizedUsage>
The reading so far. It is Partial until the event that settles it
has been seen.
Sourcepub fn finish(self, end: UsageStreamEnd) -> Option<NormalizedUsage>
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.