Skip to content

fix(parser): handle nested cache_creation.input_tokens format (v2.1.152+)#119

Merged
delexw merged 1 commit into
mainfrom
fix-issue-116
Jun 3, 2026
Merged

fix(parser): handle nested cache_creation.input_tokens format (v2.1.152+)#119
delexw merged 1 commit into
mainfrom
fix-issue-116

Conversation

@delexw
Copy link
Copy Markdown
Owner

@delexw delexw commented May 31, 2026

Summary

The Anthropic API can return prompt-cache write token counts in two formats:

  • Flat (pre-v2.1.152): usage.cache_creation_input_tokens: N
  • Nested (v2.1.152+): usage.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_tokens was always 0 for sessions written by Claude Code v2.1.152 and later.

Changes

src-tauri/src/parser/entry.rs

  • Add CacheCreationUsage struct for serde deserialization of the nested format
  • Add optional cache_creation: Option<CacheCreationUsage> field to EntryUsage
  • Add EntryUsage::effective_cache_creation_input_tokens() — returns max(flat, nested)
  • Add cache_creation_from_value(usage: &Value) -> i64 helper for raw JSON Value access sites
  • 7 new unit tests covering flat, nested, both, and neither formats

src-tauri/src/parser/classify.rs

  • Use effective_cache_creation_input_tokens() instead of reading the flat field directly

src-tauri/src/parser/session.rs

  • Replace inline .get("cache_creation_input_tokens") in both scan_session_metadata() and IncrementalTokenScanner::ingest_line() with cache_creation_from_value()
  • 1 new integration test: incremental_scanner_nested_cache_creation_format

src-tauri/src/parser/subagent.rs

  • Replace inline .get("cache_creation_input_tokens") in scan_subagent_tokens_into() with cache_creation_from_value()

specs/01-parser-pipeline.md

  • Add v2.1.152 entry to the Version-Compatibility Normalisations table

Backward Compatibility

The max(flat, nested) strategy means:

  • Old sessions (flat only, nested = 0) return the flat value unchanged
  • New sessions (nested only, flat = 0) correctly return the nested value
  • Both present returns the larger (safe edge case)

Fixes #116

…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
@delexw delexw merged commit e7e3a88 into main Jun 3, 2026
1 check passed
@delexw delexw deleted the fix-issue-116 branch June 3, 2026 06:45
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.

[Compat] Claude Code v2.1.152: cache_creation_input_tokens now populated from nested API breakdown — usage field values change in transcript

1 participant