Skip to main content

Module usage_of

Module usage_of 

Source
Expand description

The one reading of a provider’s usage, for every place srv counts tokens (docs/specs/SPEC_TOKEN_ANALYSIS_PER_KANBAN_TASK_2026_11_22.md section 4.5): the Audit, the context meter and the agents translator read through it, and the per-task ledger counts from it.

usage_of takes one output line and returns the call it reports, in six buckets that never overlap: fresh input, cache read, cache write (5 minutes and 1 hour), output and reasoning. A line that reports no usage, or in a shape this does not know, returns None: not reported is never zero. Nothing of the line is kept: no text, no tool input, no path.

Shapes read (the fixtures in usage_of_tests.rs are lines of each):

  • Claude stream-json: an assistant frame, or a stream_event holding message_start; parent_tool_use_id (or isSidechain) marks a subagent’s call. The same call is repeated per content block with the same message id, and only output_tokens grows: counting once per message id is the counter’s job.
  • Codex session file: an event_msg token_count, from its last_token_usage. Cached tokens are inside input_tokens there, so they are taken out of it here.
  • A model response (a route model’s, through the gateway): a Messages response body, or a message_start event of its stream.

A turn’s result line is not a call (its usage is the turn’s sum): turn_total reads that.

Structs§

Buckets
What a call used, in buckets that never overlap. The inclusive totals (OpenTelemetry’s input_tokens) are sums of these.
CallUsage
One call, as its line reports it.

Enums§

CallKind
Whose call it is.
Source
Which shape the line had; a counter of lines it could not read is kept per source, so a provider’s change shows.

Functions§

buckets_of
The buckets of an Anthropic-style usage object, and whether it reports reasoning apart. None when it holds none of the four counts: a field that is missing among them is 0, an object with none of them is not a reading.
turn_total
A turn’s sum, from the usage of its result line.
usage_of
The call that line reports, if it reports one.
usage_of_value
usage_of for a line already parsed.