fix(parser): handle nested cache_creation.input_tokens format (v2.1.152+)#119
Merged
Conversation
…52+)
The Anthropic API can report cache-write tokens in two formats:
- Flat (older): usage.cache_creation_input_tokens: N
- Nested (v2.1.152+): usage.cache_creation: { input_tokens: N }
Previously the parser only read the flat field, causing
cache_creation_input_tokens to show 0 for sessions recorded
after the Claude Code v2.1.152 fix.
Add CacheCreationUsage sub-struct and optional cache_creation field to
EntryUsage so serde handles nested deserialization. Add
cache_creation_from_value() helper for raw JSON Value access sites
(session.rs scan_session_metadata, IncrementalTokenScanner,
subagent.rs scan_subagent_tokens_into). Both the struct method and
helper take max(flat, nested) to remain backward-compatible with
pre-fix sessions that recorded 0 in the flat field.
Fixes #116
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
The Anthropic API can return prompt-cache write token counts in two formats:
usage.cache_creation_input_tokens: Nusage.cache_creation: { input_tokens: N }Before this fix, all three token-scanning paths in the parser only read the flat field, so
cache_creation_input_tokenswas always 0 for sessions written by Claude Code v2.1.152 and later.Changes
src-tauri/src/parser/entry.rsCacheCreationUsagestruct for serde deserialization of the nested formatcache_creation: Option<CacheCreationUsage>field toEntryUsageEntryUsage::effective_cache_creation_input_tokens()— returnsmax(flat, nested)cache_creation_from_value(usage: &Value) -> i64helper for raw JSON Value access sitessrc-tauri/src/parser/classify.rseffective_cache_creation_input_tokens()instead of reading the flat field directlysrc-tauri/src/parser/session.rs.get("cache_creation_input_tokens")in bothscan_session_metadata()andIncrementalTokenScanner::ingest_line()withcache_creation_from_value()incremental_scanner_nested_cache_creation_formatsrc-tauri/src/parser/subagent.rs.get("cache_creation_input_tokens")inscan_subagent_tokens_into()withcache_creation_from_value()specs/01-parser-pipeline.mdBackward Compatibility
The
max(flat, nested)strategy means:Fixes #116