Utility to count token usage from event rows, distinguishing known, legacy, and unknown.
# Source: https://github.com/razzant/ouroboros | License: MIT | Copyright 2026 Anton Razzhigaev
# Provided via justclaw.it.com; keep this notice with the code.
"""Read-only token summaries for event consumers, never a money projection.
No runtime imports: offline artifact readers may load this module by path.
"""
from __future__ import annotations
from typing import Iterable, Mapping, Any
TOKEN_COUNTERS = ("prompt_tokens", "completion_tokens", "cached_tokens", "cache_write_tokens")
def token_counter(row: Mapping[str, Any], name: str) -> tuple[str, int | None]:
"""One row's counter: ``("known", n)``, ``("legacy_zero", None)`` or ``("unknown", None)``.
Missing and invalid values are unknown. A zero in an unstamped/v1 row is
ambiguous: old writers could synthesize that zer
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key