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.
Otherwise only the 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:
| operation | dialects | source |
|---|---|---|
| generate, stream generate, compact | Claude, OpenAI Chat, Responses (HTTP and websocket), Gemini | the dialect’s usage object |
| count tokens | all | only a usage object the upstream attaches; the count itself was never consumed |
| embeddings | OpenAI, Gemini | usage / usageMetadata, input only |
| images | OpenAI | usage, the number of images, and the image qualifiers |
| transcription, translation | OpenAI | usage in tokens or seconds |
| rerank | OpenAI | usage in tokens or search units |
| realtime | OpenAI | each response.done, summed by response id |
Structs§
- Normalized
Usage - Response
Usage - Token
Usage - Reported counts only. None is unknown; Some(0) is an explicit zero.
- Usage
Attempt - Usage
Reader - Watches one streamed response for its usage.
Enums§
- Usage
Completeness - Usage
Stream End - How a watched stream ended. EOF alone does not establish complete
provider usage;
Interruptedmeans the stream stopped before its end. - Usage
Transport - How a streamed response reaches whoever watches it for usage.
Functions§
- reads
- Whether a complete response of
operationindialecthas usage forwholeto 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
operationindialect. The caller decides whether the response succeeded; an error body reports no usage anyway.