style: apply black formatting to fix lint checks

This commit is contained in:
Dushyant Acharya 2026-06-14 06:00:54 +05:30
parent 548ae6a089
commit 1ac710f005
No known key found for this signature in database
2 changed files with 27 additions and 24 deletions

View file

@ -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,

View file

@ -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()