From 10330bd7aa72b3a071329e034d9e350806ed65f9 Mon Sep 17 00:00:00 2001 From: Kannan Priyadharshan Date: Tue, 23 Jun 2026 21:13:46 +0800 Subject: [PATCH] test(anthropic): cover cache usage edge cases --- ...al_pass_through_adapters_transformation.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py index 1d17cb93699..3949af50410 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py @@ -2384,6 +2384,45 @@ def test_translate_streaming_openai_response_to_anthropic_cache_tokens_from_hidd assert message_delta["usage"]["cache_creation_input_tokens"] == 20 +def test_translate_streaming_openai_response_to_anthropic_cache_tokens_with_applied_edits(): + from litellm.types.utils import PromptTokensDetailsWrapper + + usage = Usage( + prompt_tokens=120, + completion_tokens=50, + total_tokens=170, + prompt_tokens_details=PromptTokensDetailsWrapper( + cached_tokens=30, + cache_creation_tokens=20, + ), + ) + response = ModelResponseStream( + choices=[ + StreamingChoices( + index=0, + delta=Delta(), + finish_reason="stop", + ) + ], + usage=usage, + ) + + adapter = LiteLLMAnthropicMessagesAdapter() + message_delta = adapter.translate_streaming_openai_response_to_anthropic( + response=response, + current_content_block_index=0, + applied_edits=[{"type": "compact_20260112"}], + ) + + assert message_delta["usage"]["input_tokens"] == 70 + assert message_delta["usage"]["output_tokens"] == 50 + assert message_delta["usage"]["cache_read_input_tokens"] == 30 + assert message_delta["usage"]["cache_creation_input_tokens"] == 20 + assert message_delta["context_management"]["applied_edits"][0]["type"] == ( + "compact_20260112" + ) + + # ===================================================================== # Web Search Tool Transformation Tests # =====================================================================