feat: expose cache_creation_tokens in LLM metrics - #6655
Open
chasleslr wants to merge 2 commits into
Open
Conversation
chasleslr
marked this pull request as ready for review
July 31, 2026 17:57
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
chasleslr
force-pushed
the
feat/cache-creation-tokens
branch
from
August 3, 2026 12:56
3e84098 to
25e7480
Compare
chasleslr
marked this pull request as draft
August 3, 2026 12:57
chasleslr
marked this pull request as ready for review
August 3, 2026 13:03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Surfaces prompt-cache creation (write) token counts through the metrics pipeline so downstream consumers can report on cache-creation usage.
cache_creation_tokenstoLLMMetricsand wires it from the existingCompletionUsage.cache_creation_tokensat the metrics construction site.input_cache_creation_tokensto the aggregateLLMModelUsageand accumulates it inModelUsageCollector.collect().cache_creation_tokensin thelog_metricsLLM log line.Why
Providers already populate
CompletionUsage.cache_creation_tokens(Anthropic fromcache_creation_input_tokens; AWS Bedrock as of #6663), but the value was silently dropped whenLLMMetricswas constructed —LLMMetricshad no such field. So cache reads were observable (prompt_cached_tokens) while cache writes were not, on either the per-requestmetrics_collectedsignal or the aggregatesession_usage_updated/session.usagesignal. This surfaces already-collected-but-discarded data.Backward compatibility
All new fields default to
0. No existing construction site or consumer changes behavior. The deprecatedUsageSummary/UsageCollectoris intentionally left untouched.Testing
tests/test_metrics_usage.py(unit): defaults-to-zero, value pass-through onLLMMetrics, and additive aggregation intoLLMModelUsageviaModelUsageCollector.make checkpasses (ruff format + lint + mypy strict).Notes
main. Provider-side emission ofcache_creation_tokensonCompletionUsageis already handled upstream (Anthropic, and AWS Bedrock via fix(aws): count cached tokens in Bedrock's prompt_tokens #6663); this PR is the remaining piece that carries the value intoLLMMetrics/LLMModelUsage.Known limitations (follow-ups, intentionally out of scope)
telemetry/otel_metrics.pyemits an OTel counter forprompt_cached_tokensbut not yet forcache_creation_tokens.voice/remote_session.py(_session_usage_to_proto) cannot forwardinput_cache_creation_tokensuntil the externalagent_pbprotobuf gains a corresponding field.🤖 Generated with Claude Code