Skip to content

feat: count cache writes, reasoning tokens, usage per model and durations - #139

Merged
gopaljigaur merged 7 commits into
mainfrom
feat/usage-accounting
Oct 15, 2026
Merged

gopaljigaur merged 7 commits into
mainfrom
feat/usage-accounting

Conversation

@gopaljigaur

@gopaljigaur gopaljigaur commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

What changed

Usage gains three fields, all summed by + and so by runs, nested runs and sessions:

  • cache_write_tokens: prompt tokens written to the provider's cache, from LiteLLM's prompt_tokens_details.cache_write_tokens (its canonical field since before our 1.101.0 floor, mirrored from Anthropic's and Bedrock's cache_creation_input_tokens).
  • reasoning_tokens: from completion_tokens_details.reasoning_tokens, which LiteLLM fills for OpenAI, Anthropic, Bedrock, Gemini and xAI.
  • api_duration: seconds from each completion's request to its whole response, retries included, timed with time.perf_counter() around the call. A paced stream (stream(), astream(), stream_events(), astream_events()) reads only a few chunks ahead of the caller, so the caller's reading time counts too; the field and the docs say so. LiteLLM's own _response_ms is missing from assembled streams, so it is not used.

cached_tokens keeps its name. As LiteLLM reports them, cached and cache-written tokens are counted in prompt_tokens, and reasoning tokens in completion_tokens.

RunResult gains:

  • usage_by_model: Dict[str, Optional[Usage]], keyed by the configured model that made each completion. A refused completion counts under the model that refused, the answer under the fallback, a subagent's completions under its model in every run above it, and a compaction summary under the summarizing model. An entry is None once one of that model's completions reported no usage, like the run's total.
  • duration: the run's wall-clock seconds, tool calls included.

Telemetry: chat spans set gen_ai.usage.cache_read.input_tokens, gen_ai.usage.cache_write.input_tokens and gen_ai.usage.reasoning.output_tokens when the response reports them. Span durations already cover latency.

These are the names on main of open-telemetry/semantic-conventions-genai, where the GenAI conventions now live. That repository has no release or tag yet: its changelog.d/440.breaking.md renames gen_ai.usage.cache_creation.input_tokens (what the opentelemetry-semantic-conventions 0.62b1 package still ships) to gen_ai.usage.cache_write.input_tokens, unreleased. With no release to follow, main's names are the current ones, so the spans use those instead of the name about to go. gen_ai.usage.reasoning.output_tokens is released, in semantic-conventions v1.41.0.

Decisions

  • Names follow the most common ones: reasoning_tokens (OpenAI Agents SDK, Vercel AI SDK, LiteLLM) and cache_write_tokens (Pydantic AI, Vercel AI SDK v6, LiteLLM's canonical field).
  • api_duration lives on Usage so it splits by model, rolls up from subagents and sums into sessions with the code that already does that for tokens. Parallel subagents add up, so it can exceed duration. Wall-clock time doesn't sum across nested runs, so it is only on RunResult.
  • Audio tokens are left out: no caller yet, and cost already prices them.
  • _Run.spend and the compaction spend callback take the model. _complete_with_fallbacks now returns the step's usage with its response, since timing happens per attempt in _send. run.py changes are a few lines.
  • A small _added helper replaces the four copies of the "None once unknown" sum.

Tests

tests/test_usage.py: Anthropic cache writes and reads through LiteLLM's real response and SSE parsers, OpenAI cached and reasoning tokens through its real response parser, a refusal fallback's per-model split, a subagent on another model, a session accumulating cache tokens and API time across runs, a refusal without usage leaving its model's entry and the total None, and durations (per step, summed, a streamed completion, wall-clock covering a tool's wait). Also a compaction model's own entry and the new span attributes.

uv run pytest -n 4, uv run pre-commit run --all-files and uv run mypy cyclops pass.

Closes #63

@gopaljigaur
gopaljigaur force-pushed the feat/usage-accounting branch from e04dfa9 to 12f076e Compare October 15, 2026 07:42

@gopaljigaur gopaljigaur left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked against LiteLLM 1.101.0 and 1.105.1: prompt_tokens_details.cache_write_tokens is the canonical field in both, mirrored from Anthropic's and Bedrock's cache_creation_input_tokens, and both add cache reads and writes into prompt_tokens, so cached_tokens and cache_write_tokens are disjoint subsets of prompt_tokens on Anthropic and OpenAI alike, with no double count. Names match Pydantic AI (cache_write_tokens), LiteLLM and the OpenAI Agents SDK (reasoning_tokens). The new tests fail on main (14 failures) and pass here; origin/main (with #136) is an ancestor of the head; pytest -n 4, pre-commit, mypy, the site build and CI pass; the confidentiality and dash checks are clean. Three findings inline.

Comment thread cyclops/observability/otel.py Resolved
Comment thread cyclops/core/types.py Resolved
Comment thread cyclops/core/run_state.py Resolved
… durations

Usage gains cache_write_tokens and reasoning_tokens, read from the
prompt_tokens_details and completion_tokens_details LiteLLM normalizes,
and api_duration, the seconds from each completion's request to its
whole response. RunResult gains usage_by_model, keyed by the configured
model that made each completion, so fallback models, subagents on other
models and a compaction model each keep their own usage, and duration,
the run's wall-clock seconds.
@gopaljigaur
gopaljigaur force-pushed the feat/usage-accounting branch from 12f076e to d279bae Compare October 15, 2026 07:57

@gopaljigaur gopaljigaur left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings.

@gopaljigaur
gopaljigaur merged commit bdfb574 into main Oct 15, 2026
13 checks passed
@gopaljigaur
gopaljigaur deleted the feat/usage-accounting branch October 15, 2026 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fuller usage accounting

1 participant