Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix bedrock cost calculation for cached tokens (#12488)

jdietzsch912ff301ee21e1
diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py
index d0a5cac14e4..98ca7b97ebc 100644
--- a/litellm/cost_calculator.py
+++ b/litellm/cost_calculator.py
@@ -29,6 +29,9 @@ from litellm.llms.anthropic.cost_calculation import (
from litellm.llms.azure.cost_calculation import (
cost_per_token as azure_openai_cost_per_token,
)
+from litellm.llms.bedrock.cost_calculation import (
+ cost_per_token as bedrock_cost_per_token,
+)
from litellm.llms.bedrock.image.cost_calculator import (
cost_calculator as bedrock_image_cost_calculator,
)
@@ -326,6 +329,8 @@ def cost_per_token( # noqa: PLR0915
)
elif custom_llm_provider == "anthropic":
return anthropic_cost_per_token(model=model, usage=usage_block)
+ elif custom_llm_provider == "bedrock":
+ return bedrock_cost_per_token(model=model, usage=usage_block)
elif custom_llm_provider == "openai":
return openai_cost_per_token(model=model, usage=usage_block)
elif custom_llm_provider == "databricks":
diff --git a/litellm/llms/bedrock/cost_calculation.py b/litellm/llms/bedrock/cost_calculation.py
new file mode 100644
index 00000000000..b20350d7325
--- /dev/null
+++ b/litellm/llms/bedrock/cost_calculation.py
@@ -0,0 +1,22 @@
+"""
+Helper util for handling bedrock-specific cost calculation
+- e.g.: prompt caching
+"""
+
+from typing import TYPE_CHECKING, Tuple
+
+from litellm.litellm_core_utils.llm_cost_calc.utils import generic_cost_per_token
+
+if TYPE_CHECKING:
+ from litellm.types.utils import Usage
+
+
+def cost_per_token(model: str, usage: "Usage") -> Tuple[float, float]:
+ """
+ Calculates the cost per token for a given model, prompt tokens, and completion tokens.
+
+ Follows the same logic as Anthropic's cost per token calculation.
+ """
+ return generic_cost_per_token(
+ model=model, usage=usage, custom_llm_provider="bedrock"
+ )
\ No newline at end of file
diff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.py
index 9645bb04f20..aa109f04c2d 100644
--- a/tests/test_litellm/test_cost_calculator.py
+++ b/tests/test_litellm/test_cost_calculator.py
@@ -325,3 +325,89 @@ def test_cost_calculator_with_cache_creation():
)
print(result)
+
+
+def test_bedrock_cost_calculator_comparison_with_without_cache():
+ """Test that Bedrock caching reduces costs compared to non-cached requests"""
+ from litellm import completion_cost
+ from litellm.types.utils import (
+ Choices,
+ Message,
+ PromptTokensDetailsWrapper,
+ Usage,
+ )
+
+ # Response WITHOUT caching
+ response_no_cache = ModelResponse(
+ id="msg_no_cache",
+ created=1750733889,
+ model="anthropic.claude-sonnet-4-20250514-v1:0",
+ object="chat.completion",
+ choices=[
+ Choices(
+ finish_reason="stop",
+ index=0,
+ message=Message(
+ content="Response without cache",
+ role="assistant",
+ ),
+ )
+ ],
+ usage=Usage(
+ total_tokens=28508,
+ prompt_tokens=28495,
+ completion_tokens=13,
+ ),
+ )
+
+ # Response WITH caching (same total tokens, but most are cached)
+ response_with_cache = ModelResponse(
+ id="msg_with_cache",
+ created=1750733889,
+ model="anthropic.claude-sonnet-4-20250514-v1:0",
+ object="chat.completion",
+ choices=[
+ Choices(
+ finish_reason="stop",
+ index=0,
+ message=Message(
+ content="Response with cache",
+ role="assistant",
+ ),
+ )
+ ],
+ usage=Usage(
+ **{
+ "total_tokens": 28508,
+ "prompt_tokens": 28495,
+ "completion_tokens": 13,
+ "prompt_tokens_details": {"audio_tokens": None, "cached_tokens": 0},
+ "cache_read_input_tokens": 28491, # Most tokens are read from cache (cheaper)
+ "completion_tokens_details": {
+ "audio_tokens": None,
+ "reasoning_tokens": 0,
+ "accepted_prediction_tokens": None,
+ "rejected_prediction_tokens": None,
+ },
+ "cache_creation_input_tokens": 15, # Only 15 new tokens added to cache
+ }
+ ),
+ )
+
+ # Calculate costs
+ cost_no_cache = completion_cost(
+ completion_response=response_no_cache,
+ model="bedrock/anthropic.claude-sonnet-4-20250514-v1:0",
+ custom_llm_provider="bedrock",
+ )
+
+ cost_with_cache = completion_cost(
+ completion_response=response_with_cache,
+ model="bedrock/anthropic.claude-sonnet-4-20250514-v1:0",
+ custom_llm_provider="bedrock",
+ )
+
+ # Verify that cached request is cheaper
+ assert cost_with_cache < cost_no_cache
+ print(f"Cost without cache: {cost_no_cache}")
+ print(f"Cost with cache: {cost_with_cache}")