Skip to content

Commit 08ba38a

Browse files
test: update unit test
1 parent 8273630 commit 08ba38a

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

tests/test_litellm/test_cost_calculator.py

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,10 @@ def test_gemini_25_implicit_caching_cost():
482482
print(f"✓ Gemini 2.5 implicit caching cost calculation is correct: ${result:.8f}")
483483

484484

485-
486485
def test_log_context_cost_calculation():
487486
"""
488487
Test that log context cost calculation works correctly with tiered pricing.
489-
488+
490489
This test verifies that when using extended context (above 200k tokens),
491490
the log context costs are calculated using the appropriate tiered rates.
492491
"""
@@ -520,7 +519,7 @@ def test_log_context_cost_calculation():
520519
],
521520
usage=Usage(
522521
total_tokens=350000, # Above 200k threshold
523-
prompt_tokens=300000, # Above 200k threshold
522+
prompt_tokens=301000, # Above 200k threshold
524523
completion_tokens=50000,
525524
prompt_tokens_details=PromptTokensDetailsWrapper(
526525
text_tokens=300000,
@@ -529,6 +528,7 @@ def test_log_context_cost_calculation():
529528
image_tokens=None,
530529
character_count=None,
531530
video_length_seconds=None,
531+
cache_creation_tokens=1000,
532532
),
533533
completion_tokens_details=None,
534534
_cache_creation_input_tokens=1000, # Some tokens added to cache
@@ -544,63 +544,84 @@ def test_log_context_cost_calculation():
544544

545545
# Debug: Print the actual result
546546
print(f"DEBUG: Actual cost result: ${result:.6f}")
547-
547+
548548
# Get model info to understand the pricing
549549
from litellm import get_model_info
550-
model_info = get_model_info(model="claude-4-sonnet-20250514", custom_llm_provider="anthropic")
551-
550+
551+
model_info = get_model_info(
552+
model="claude-4-sonnet-20250514", custom_llm_provider="anthropic"
553+
)
554+
552555
# Calculate expected cost based on actual model pricing
553556
input_cost_per_token = model_info.get("input_cost_per_token", 0)
554557
output_cost_per_token = model_info.get("output_cost_per_token", 0)
555558
cache_creation_cost_per_token = model_info.get("cache_creation_input_token_cost", 0)
556-
559+
557560
# Check if tiered pricing is applied
558-
input_cost_above_200k = model_info.get("input_cost_per_token_above_200k_tokens", input_cost_per_token)
559-
output_cost_above_200k = model_info.get("output_cost_per_token_above_200k_tokens", output_cost_per_token)
560-
cache_creation_above_200k = model_info.get("cache_creation_input_token_cost_above_200k_tokens", cache_creation_cost_per_token)
561-
561+
input_cost_above_200k = model_info.get(
562+
"input_cost_per_token_above_200k_tokens", input_cost_per_token
563+
)
564+
output_cost_above_200k = model_info.get(
565+
"output_cost_per_token_above_200k_tokens", output_cost_per_token
566+
)
567+
cache_creation_above_200k = model_info.get(
568+
"cache_creation_input_token_cost_above_200k_tokens",
569+
cache_creation_cost_per_token,
570+
)
571+
562572
print(f"DEBUG: Base input cost per token: ${input_cost_per_token:.2e}")
563573
print(f"DEBUG: Base output cost per token: ${output_cost_per_token:.2e}")
564-
print(f"DEBUG: Base cache creation cost per token: ${cache_creation_cost_per_token:.2e}")
565-
574+
print(
575+
f"DEBUG: Base cache creation cost per token: ${cache_creation_cost_per_token:.2e}"
576+
)
577+
566578
# Handle tiered pricing - if not available, use base pricing
567579
if input_cost_above_200k is not None:
568-
print(f"DEBUG: Tiered input cost per token (>200k): ${input_cost_above_200k:.2e}")
580+
print(
581+
f"DEBUG: Tiered input cost per token (>200k): ${input_cost_above_200k:.2e}"
582+
)
569583
else:
570584
print(f"DEBUG: No tiered input pricing available, using base pricing")
571585
input_cost_above_200k = input_cost_per_token
572-
586+
573587
if output_cost_above_200k is not None:
574-
print(f"DEBUG: Tiered output cost per token (>200k): ${output_cost_above_200k:.2e}")
588+
print(
589+
f"DEBUG: Tiered output cost per token (>200k): ${output_cost_above_200k:.2e}"
590+
)
575591
else:
576592
print(f"DEBUG: No tiered output pricing available, using base pricing")
577593
output_cost_above_200k = output_cost_per_token
578-
594+
579595
if cache_creation_above_200k is not None:
580-
print(f"DEBUG: Tiered cache creation cost per token (>200k): ${cache_creation_above_200k:.2e}")
596+
print(
597+
f"DEBUG: Tiered cache creation cost per token (>200k): ${cache_creation_above_200k:.2e}"
598+
)
581599
else:
582600
print(f"DEBUG: No tiered cache creation pricing available, using base pricing")
583601
cache_creation_above_200k = cache_creation_cost_per_token
584-
602+
585603
# Since we're above 200k tokens, we should use tiered pricing if available
586604
expected_input_cost = 300000 * input_cost_above_200k
587605
expected_output_cost = 50000 * output_cost_above_200k
588606
expected_cache_cost = 1000 * cache_creation_above_200k
589607
expected_total = expected_input_cost + expected_output_cost + expected_cache_cost
590-
608+
591609
print(f"DEBUG: Expected total: ${expected_total:.6f}")
592-
610+
593611
# Allow for small floating point differences
594612
assert (
595613
abs(result - expected_total) < 1e-6
596614
), f"Expected cost ${expected_total:.6f}, but got ${result:.6f}"
597615

598-
print(f"✓ Log context cost calculation with tiered pricing is correct: ${result:.6f}")
616+
print(
617+
f"✓ Log context cost calculation with tiered pricing is correct: ${result:.6f}"
618+
)
599619
print(f" - Input tokens (300k): ${expected_input_cost:.6f}")
600620
print(f" - Output tokens (50k): ${expected_output_cost:.6f}")
601621
print(f" - Cache creation (1k): ${expected_cache_cost:.6f}")
602622
print(f" - Total: ${result:.6f}")
603623

624+
604625
def test_gemini_25_explicit_caching_cost_direct_usage():
605626
"""
606627
Test that Gemini 2.5 models correctly calculate costs with explicit caching.

0 commit comments

Comments
 (0)