Repository navigation
feat(events): report prompt-cache hits as cached_tokens - #332
Open
raymondginger2018-sudo wants to merge 1 commit into
Open
raymondginger2018-sudo wants to merge 1 commit into
raymondginger2018-sudo wants to merge 1 commit into
Conversation
`UsageInfo` folded everything into `prompt_tokens`, so a host could not show whether a long prefix was actually cached. Providers already report the hit count under different names (DeepSeek `prompt_cache_hit_tokens`, OpenAI `prompt_tokens_details.cached_tokens`, Anthropic `cache_read_input_tokens`); read the first one that carries a count and expose it as `cached_tokens`, a subset of `prompt_tokens`. Tests: DeepSeek and OpenAI shapes, plus the uncached default.
This branch has not been deployed
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.
Summary
UsageInfofolded everything the provider reported intoprompt_tokens, so a host could not tell whether a long prefix was actually cached. The providers already report the hit count — under different names per vendor — and that count is a subset ofprompt_tokens.UsageInfo.cached_tokensnow carries it, so a UI or a cost report can show cache effectiveness without re-parsing provider payloads.Changes
core/events/llm_events.pyUsageInfo.cached_tokens: int = 0— defaults keep every existing construction site working._cached_tokens(usage)reads the first field that carries a count:cached_tokens,prompt_cache_hit_tokens(DeepSeek),cache_read_input_tokens(Anthropic),prompt_tokens_details.cached_tokens(OpenAI). Missing or non-numeric values read as0.llm_response_to_eventsfills the field from the response usage; serialization carries it.tests/test_llm_events.py— DeepSeek, OpenAI, and the uncached default.Compatibility
prompt_tokenskeeps its existing meaning (cache hits remain included, as providers report them).Tests
tests/test_llm_events.py— 8 passed on this revision;pre-commit(ruff 0.15.21 + ruff-format) clean.