From 1ac710f005a8b1efbdd6e4e1c1d824362a164797 Mon Sep 17 00:00:00 2001 From: Dushyant Acharya Date: Sun, 14 Jun 2026 06:00:54 +0530 Subject: [PATCH] style: apply black formatting to fix lint checks --- litellm/llms/anthropic/chat/transformation.py | 4 +- tests/test_anthropic_compaction_usage.py | 47 ++++++++++--------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 9e1599fd0f6..e8c1e659e9f 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -2279,7 +2279,9 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ), ) - raw_input_tokens = prompt_tokens - cache_read_input_tokens - cache_creation_input_tokens + raw_input_tokens = ( + prompt_tokens - cache_read_input_tokens - cache_creation_input_tokens + ) prompt_tokens_details = PromptTokensDetailsWrapper( cached_tokens=cache_read_input_tokens, cache_creation_tokens=cache_creation_input_tokens, diff --git a/tests/test_anthropic_compaction_usage.py b/tests/test_anthropic_compaction_usage.py index a6158d2fc3b..1758a94fffc 100644 --- a/tests/test_anthropic_compaction_usage.py +++ b/tests/test_anthropic_compaction_usage.py @@ -1,37 +1,37 @@ from litellm.llms.anthropic.chat.transformation import AnthropicConfig + def test_anthropic_compaction_usage_calculation(): """ Test that calculate_usage correctly sums tokens from the iterations array as requested in Issue #27060. """ anthropic_config = AnthropicConfig() - + # Mock usage object with compaction iterations usage_object = { "input_tokens": 100, # Top-level (excludes compaction) - "output_tokens": 50, # Top-level (excludes compaction) + "output_tokens": 50, # Top-level (excludes compaction) "iterations": [ { "iteration": 1, "type": "compaction", "input_tokens": 1000, - "output_tokens": 500 + "output_tokens": 500, }, { "iteration": 2, "type": "message", "input_tokens": 100, - "output_tokens": 50 - } - ] + "output_tokens": 50, + }, + ], } - + usage = anthropic_config.calculate_usage( - usage_object=usage_object, - reasoning_content=None + usage_object=usage_object, reasoning_content=None ) - + # Assertions # Total prompt tokens should be 1000 + 100 = 1100 assert usage.prompt_tokens == 1100 @@ -39,48 +39,48 @@ def test_anthropic_compaction_usage_calculation(): assert usage.completion_tokens == 550 # Total tokens should be 1650 assert usage.total_tokens == 1650 - + # Assert details assert usage.prompt_tokens_details.text_tokens == 1100 - + # Assert iterations passthrough assert usage.iterations is not None assert len(usage.iterations) == 2 assert usage.iterations[0]["type"] == "compaction" + def test_anthropic_compaction_usage_with_iteration_cache(): """ Test that calculate_usage correctly sums caching tokens FROM iterations. This covers the specific case mentioned by JasonPan. """ anthropic_config = AnthropicConfig() - + usage_object = { "input_tokens": 100, "output_tokens": 50, "iterations": [ { "type": "compaction", - "input_tokens": 500, + "input_tokens": 500, "output_tokens": 200, "cache_creation_input_tokens": 50, - "cache_read_input_tokens": 17000 + "cache_read_input_tokens": 17000, }, { "type": "message", - "input_tokens": 100, + "input_tokens": 100, "output_tokens": 50, "cache_creation_input_tokens": 10, - "cache_read_input_tokens": 20 - } - ] + "cache_read_input_tokens": 20, + }, + ], } - + usage = anthropic_config.calculate_usage( - usage_object=usage_object, - reasoning_content=None + usage_object=usage_object, reasoning_content=None ) - + # input_tokens sum = 500 + 100 = 600 # cache_creation sum = 50 + 10 = 60 # cache_read sum = 17000 + 20 = 17020 @@ -90,6 +90,7 @@ def test_anthropic_compaction_usage_with_iteration_cache(): assert usage.prompt_tokens_details.cache_creation_tokens == 60 assert usage.prompt_tokens_details.cached_tokens == 17020 + if __name__ == "__main__": test_anthropic_compaction_usage_calculation() test_anthropic_compaction_usage_with_iteration_cache()