Skip to main content

Module usage

Module usage 

Source
Expand description

Per-call metering read from the standard response of an operation.

A channel’s job on the response side is to shape whatever its upstream answered into the standard form of the operation it served: Claude Messages, OpenAI Chat Completions or Responses, Gemini, an embedding or image reply. From there on every response is a standard one, whether it is converted for the client or passed through, so usage has one source: the readers here, chosen by operation and dialect. No reader knows a vendor. Fields a vendor adds beside the standard ones are the vendor’s to read.

Each reader has two forms. whole reads a complete response body. UsageReader watches a stream as it passes, chunk by chunk or message by message, without holding more of it than the event it is decoding: the stream is framed with this crate’s own SseDecoder and JsonArrayDecoder, events are filtered by name or by a substring before anything is parsed. Until first output, content is also inspected for TTFT. When fallback output counting is disabled, only events that carry usage are deserialized into small envelopes that skip the content around the usage object.

A reading is None when the response reported no usage, which is not the same as zero consumption; a malformed response is one that reported none. No reader panics on its input. A streamed reading is Partial until the event that settles it has been seen, and whenever the stream was cut.

What each operation reads:

operationdialectssource
generate, stream generate, compactClaude, OpenAI Chat, Responses (HTTP and websocket), Geminithe dialect’s usage object
count tokensallonly a usage object the upstream attaches; the count itself was never consumed
embeddingsOpenAI, Geminiusage / usageMetadata, input only
imagesOpenAIusage, the number of images, and the image qualifiers
transcription, translationOpenAIusage in tokens or seconds
rerankOpenAIusage in tokens or search units
realtimeOpenAIeach response.done, summed by response id

Structs§

CacheCreation
One reported cache-write quantity. Unknown retention is not a default TTL.
NormalizedUsage
ResponseUsage
TokenUsage
Reported counts only. None is unknown; Some(0) is an explicit zero.
UsageAttempt
UsageReader
Watches one streamed response for its usage.

Enums§

UsageCompleteness
UsageStreamEnd
How a watched stream ended. EOF alone does not establish complete provider usage; Interrupted means the stream stopped before its end.
UsageTransport
How a streamed response reaches whoever watches it for usage.

Functions§

reads
Whether a complete response of operation in dialect has usage for whole to read. A caller that must hold a body to read it whole holds it only when this says there is something to find.
whole
Usage from a complete, successful response body of operation in dialect. The caller decides whether the response succeeded; an error body reports no usage anyway.
whole_output_characters
Generated characters in a complete standard response. Unsupported bodies yield zero; binary/media sizes are never a substitute for output tokens.