Repository navigation
Conversation
Anthropic-style streams only carry the cache creation TTL split (usage.cache_creation.ephemeral_5m_input_tokens / ephemeral_1h_input_tokens) in the message_start usage chunk; the closing message_delta chunk repeats the totals without it. ChunkProcessor._calculate_usage_per_chunk overwrote prompt_tokens_details with every successive usage chunk, so the split was clobbered by the last chunk while the aggregate cache_creation_input_tokens survived through its own keep-the-meaningful-chunk tracking With the split gone, generic_cost_per_token had nothing to read from usage.prompt_tokens_details.cache_creation_token_details and billed the whole cache write at cache_creation_input_token_cost, the 5m rate. 1h-TTL writes (for example Bedrock passthrough streaming with cache_control ttl 1h) were undercharged by the cache_creation_input_token_cost_above_1hr delta Track cache_creation_token_details across usage chunks the same way the aggregate already is, expose it on UsagePerChunk, and reattach it to the final usage's prompt_tokens_details in calculate_usage Fixes BerriAI#29432.
Greptile SummaryThis PR fixes a cost-calculation bug where Anthropic's
Confidence Score: 5/5The change is narrowly scoped to the chunk-combination path, follows the existing keep-first-non-None tracking pattern already used for other fields, and is verified by a targeted regression test that fails on the base branch and passes with the fix. All three changed files are clean: the core logic correctly separates No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/streaming_chunk_builder_utils.py | Adds independent tracking of cache_creation_token_details in _calculate_usage_per_chunk (keep-first-non-None logic) and re-attaches it to prompt_tokens_details in calculate_usage after the last chunk's prompt_tokens_details overwrites the field. |
| litellm/types/litellm_core_utils/streaming_chunk_builder_utils.py | Adds cache_creation_token_details: Optional[CacheCreationTokenDetails] to the UsagePerChunk TypedDict to carry the new field through the pipeline. |
| tests/test_litellm/litellm_core_utils/test_streaming_chunk_builder_utils.py | New regression test test_calculate_usage_retains_cache_creation_token_details replays the exact two-chunk scenario (message_start with 5m/1h split, message_delta without it) and asserts both split fields survive on the combined usage; no real network calls. |
Reviews (1): Last reviewed commit: "fix(streaming): preserve 5m/1h cache cre..." | Re-trigger Greptile
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
|
Any update about this PR? |
|
Main keeps the 5m and 1h cache write split in combined streaming usage since #32073, so closing this. Reopen if something is missing |
Relevant issues
Fixes #29432
Linear ticket
N/A
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
make test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
CI (LiteLLM team)
Branch creation CI run
Link:
CI run for the last commit
Link:
Merge / cherry-pick CI run
Links:
Screenshots / Proof of Fix
The combine step is deterministic, so the regression is visible by replaying the exact usage chunks from the issue's Bedrock invocation log through
stream_chunk_builder: a message_start usage carryingcache_creation_token_details(ephemeral_5m_input_tokens=288,ephemeral_1h_input_tokens=74) followed by a message_delta usage carrying only totals, priced againstbedrock/global.anthropic.claude-opus-4-8(cache_creation_input_token_cost=6.25e-6,cache_creation_input_token_cost_above_1hr=1.0e-5)The 0.0002775 difference is exactly 74 tokens x (10.00 - 6.25) per million, the undercount computed in the issue. To see it end to end, run the proxy as a Bedrock passthrough and POST
/bedrock/model/<inference-profile>/invoke-with-response-streamwithcache_control: {"type": "ephemeral", "ttl": "1h"}on a cacheable block; SpendLogcost_breakdown.cache_creation_costnow bills the 1h portion atcache_creation_input_token_cost_above_1hrinstead of the 5m rateType
🐛 Bug Fix
Changes
ChunkProcessor._calculate_usage_per_chunkinlitellm/litellm_core_utils/streaming_chunk_builder_utils.pyoverwroteprompt_tokens_detailswith every successive usage chunk. Anthropic-style streams only send the 5m/1h cache creation split in the message_start usage; the closing message_delta repeats the totals without it, so the split was clobbered by the last chunk while the aggregatecache_creation_input_tokenssurvived through its own keep-the-meaningful-chunk tracking. Downstream,calculate_cache_writing_costinlitellm/litellm_core_utils/llm_cost_calc/utils.pythen had nousage.prompt_tokens_details.cache_creation_token_detailsto read and billed the whole cache write at the 5m rateThe fix tracks
cache_creation_token_detailsacross usage chunks the same way the aggregate already is (keep the chunk that has it), adds the field to theUsagePerChunkTypedDict inlitellm/types/litellm_core_utils/streaming_chunk_builder_utils.py, and reattaches it to the final usage'sprompt_tokens_detailsincalculate_usageRegression test
test_calculate_usage_retains_cache_creation_token_detailsintests/test_litellm/litellm_core_utils/test_streaming_chunk_builder_utils.pyfails on the base branch and passes with the fix.tests/test_litellm/litellm_core_utils/is green apart fromtest_bedrock_converse_messages_pt_document_various_formats, which also fails on the base branch;tests/test_litellm/test_bedrock_anthropic_1hr_cache_pricing.py,test_cost_calculator.py,test_stream_chunk_builder_annotations.pyand the anthropic passthrough logging handler tests all pass