Repository navigation
fix(langchain): avoid double counting Anthropic prompt cache creation tokens - #2088
Open
viliamgonda wants to merge 1 commit into
Open
viliamgonda wants to merge 1 commit into
viliamgonda wants to merge 1 commit into
Conversation
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.
What does this PR do?
Fixes langfuse/langfuse#19452
In
CallbackHandler._parse_usage_model, Anthropic prompt caching reports cache-write breakdown undercache_creation(ephemeral_5m_input_tokens,ephemeral_1h_input_tokens) alongside the aggregatecache_creation_input_tokens.Previously,
_parse_usage_modelflattened the nested dict intocache_creation_ephemeral_*and also set/keptcache_creation_input_tokens. Because both the aggregate count and TTL tiers describe the same tokens, downstream bucket summation counted prompt cache creation twice.This change:
input_cache_creation_5m,input_cache_creation_1h), matching server model pricing.cache_creation_input_tokens,input_cache_creation), retaining only unattributed remainder tokens.Type of change
Verification
List the main commands you ran:
Checklist
code_review.md..env.templateif needed.

Confidence Score: 5/5
The PR appears safe to merge.Summary
Fixes double counting of Anthropic cache creation tokens in
_parse_usage_model.No actionable issues were found. Tests were reviewed but not run.
Reviews (1) · Last reviewed commit: "fix(langchain): avoid double counting An..." · Reviewed by Greptile