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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(bedrock): guard cache-detail split against partial/unrecognized ttl entries
Daniel Vainshtein42a2b5f057f4
diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.pyindex 9e7c86e615f..dbea1783dc2 100644--- a/litellm/llms/bedrock/chat/converse_transformation.py+++ b/litellm/llms/bedrock/chat/converse_transformation.py@@ -1773,17 +1773,17 @@ class AmazonConverseConfig(BaseConfig):@staticmethoddef _parse_cache_details(usage: ConverseTokenUsageBlock) -> "CacheCreationTokenDetails | None":- """- Split Converse's aggregate cacheWriteInputTokens into the 5m/1h TTL- breakdown from `cacheDetails`, so cost calc can bill each tier- correctly instead of defaulting the whole write to the 5m rate.- https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_CacheDetail.html- """- cache_details = usage.get("cacheDetails")+ """https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_CacheDetail.html"""+ cache_details: Final = usage.get("cacheDetails")if not cache_details:return None- tokens_5m = sum(d["inputTokens"] for d in cache_details if d.get("ttl") == "5m")- tokens_1h = sum(d["inputTokens"] for d in cache_details if d.get("ttl") == "1h")+ tokens_5m: Final = sum(d["inputTokens"] for d in cache_details if d.get("ttl") == "5m")+ tokens_1h: Final = sum(d["inputTokens"] for d in cache_details if d.get("ttl") == "1h")+ # An unrecognized ttl or a partial breakdown would silently understate+ # the cache-write cost, so only use the split when it fully accounts+ # for the aggregate; otherwise fall back to the aggregate-only (5m) cost.+ if tokens_5m + tokens_1h != usage.get("cacheWriteInputTokens", 0):+ return Nonereturn CacheCreationTokenDetails(ephemeral_5m_input_tokens=tokens_5m,ephemeral_1h_input_tokens=tokens_1h,diff --git a/litellm/types/llms/bedrock.py b/litellm/types/llms/bedrock.pyindex 4c3ed8d6993..847e4066cf3 100644--- a/litellm/types/llms/bedrock.py+++ b/litellm/types/llms/bedrock.py@@ -2,7 +2,7 @@ import jsonfrom enum import Enumfrom typing import TYPE_CHECKING, Any, Final, Literal-from typing_extensions import Required, TypedDict, override+from typing_extensions import ReadOnly, Required, TypedDict, overridefrom .openai import ChatCompletionToolCallChunk@@ -217,21 +217,21 @@ class ConverseResponseOutputBlock(TypedDict):class CacheDetailBlock(TypedDict):- """Per-TTL cache-write breakdown. https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_CacheDetail.html"""+ """Per-TTL cache-write breakdown, read-only AWS response data. https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_CacheDetail.html"""- inputTokens: int- ttl: Literal["5m", "1h"]+ inputTokens: ReadOnly[int]+ ttl: ReadOnly[Literal["5m", "1h"]]class ConverseTokenUsageBlock(TypedDict, total=False):- inputTokens: Required[int]- outputTokens: Required[int]- totalTokens: Required[int]- cacheReadInputTokenCount: int- cacheReadInputTokens: int- cacheWriteInputTokenCount: int- cacheWriteInputTokens: int- cacheDetails: list[CacheDetailBlock]+ inputTokens: Required[ReadOnly[int]]+ outputTokens: Required[ReadOnly[int]]+ totalTokens: Required[ReadOnly[int]]+ cacheReadInputTokenCount: ReadOnly[int]+ cacheReadInputTokens: ReadOnly[int]+ cacheWriteInputTokenCount: ReadOnly[int]+ cacheWriteInputTokens: ReadOnly[int]+ cacheDetails: ReadOnly[list[CacheDetailBlock]] # mutable-ok: AWS response array, never mutated after parsingclass ServiceTierBlock(TypedDict):diff --git a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.pyindex 393499fb041..a7d6695ca35 100644--- a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py+++ b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py@@ -75,6 +75,27 @@ def test_transform_usage_with_cache_details():assert details.ephemeral_5m_input_tokens == 288+def test_transform_usage_with_mismatched_cache_details_falls_back():+ """An unrecognized ttl or partial breakdown must not silently understate+ cache-write cost, so the split is only used when it fully accounts for+ cacheWriteInputTokens."""+ usage = ConverseTokenUsageBlock(+ **{+ "inputTokens": 76,+ "outputTokens": 259,+ "totalTokens": 335,+ "cacheWriteInputTokens": 362,+ "cacheDetails": [{"inputTokens": 74, "ttl": "1h"}], # missing the 5m entry+ }+ )+ config = AmazonConverseConfig()+ openai_usage = config._transform_usage(usage)+ assert (+ getattr(openai_usage.prompt_tokens_details, "cache_creation_token_details", None)+ is None+ )++def test_transform_usage_without_cache_details_stays_none():"""No cacheDetails in the response (older models/regions) should leavecache_creation_token_details unset, same as before this field existed."""