ModelUsage#
Implement the model contract and normalize usage and stream output. See the guide.
- class zett_agent.model.ModelUsage(input_tokens=0, output_tokens=0, cache_read_tokens=0, cache_write_tokens=0, cache_reported=True, reasoning_tokens=0, ttft_any_ns=None, ttft_content_ns=None, first_chunk=None, first_content_chunk=None)[source]#
Normalized token and cache measurements reported by a provider adapter.
input_tokensis the complete model input, including cache reads and cache writes.output_tokensincludes reasoning tokens. This invariant makes totals and cache-hit ratios comparable across provider schemas.cache_reportedseparates a verified cache miss from a protocol that never reports cache counters. Compatible gateways often relay one protocol through another and drop those counters on the way, so a zero withoutcache_reportedmeans unknown rather than measured.The streaming measurements are not provider-reported. They are filled in by the runtime after the stream ends, because only the runtime sees when each delta arrived: a provider adapter sees usage counters in the final payload, not the arrival time of the first token.
ttft_any_nsmeasures the first delta of any kind, which for a reasoning model is usually a reasoning chunk, whilettft_content_nsmeasures the first answer-content delta. Measuring both keeps “the model started talking” apart from “the user saw a word”.The clock starts when the runtime starts the generation, so an adapter that retries internally is inside the measurement on purpose. A step that never streams, or that fails before its first delta, leaves the fields None rather than zero; a failed step stores no message, so a reader cannot confuse “no measurement” with “measured as instant”.
ttft_content_nsrepeats the informationcontent_started_at - started_atalready holds for streamed content; it is stored so both time-to-first-token values share one monotonic clock and one unit, and a reader never mixes a wall-clock subtraction with it.- Parameters:
input_tokens (int)
output_tokens (int)
cache_read_tokens (int)
cache_write_tokens (int)
cache_reported (bool)
reasoning_tokens (int)
ttft_any_ns (int | None)
ttft_content_ns (int | None)
first_chunk (str | None)
first_content_chunk (str | None)
- input_tokens: int#
Total input tokens, including cached input and cache writes.
- output_tokens: int#
Total generated tokens, including reasoning tokens when reported.
- cache_read_tokens: int#
Input tokens reused from the provider cache, not extra input tokens.
- cache_write_tokens: int#
Input tokens written to the provider cache, not extra input tokens.
- cache_reported: bool#
Whether the provider reported how much input came from the cache; False means unknown.
- reasoning_tokens: int#
Reasoning subset of output_tokens; zero when not reported.
- ttft_any_ns: int | None#
Nanoseconds from the request starting to the first streamed delta of any kind; None when the runtime observed no delta, as with a non-streaming provider or a request that failed before the first chunk.
- ttft_content_ns: int | None#
Nanoseconds from the request starting to the first answer-content delta; None when the stream carried no answer content, including tool-only calls.
- first_chunk: str | None#
First non-empty delta of any kind, clipped to
FIRST_CHUNK_LIMITcharacters; None when no delta arrived.
- first_content_chunk: str | None#
First non-empty answer-content delta, clipped the same way; None when the stream carried no answer content.
- property total_tokens: int#
Return input plus output without double-counting cache or reasoning.
- property cache_hit_rate: float | None#
Return cached input divided by total input, or None when unknown.
None means there is no ratio to report: the request had no input tokens, or the provider did not report cache counters for it. A protocol that reports cache usage still yields zero for a genuine miss.