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(router): read an application profile's base model from metadata on a cache hit (#47144)

yuneng-jiang1e3641a52a6c
diff --git a/litellm/router_utils/pre_call_checks/prompt_caching_deployment_check.py b/litellm/router_utils/pre_call_checks/prompt_caching_deployment_check.py
index b0da3ed264a..b2f0c658161 100644
--- a/litellm/router_utils/pre_call_checks/prompt_caching_deployment_check.py
+++ b/litellm/router_utils/pre_call_checks/prompt_caching_deployment_check.py
@@ -21,7 +21,11 @@ from litellm.litellm_core_utils.token_counter import offload_token_count
from litellm.llms.bedrock.common_utils import bedrock_cache_point_ttl_supported, bedrock_capability_model
from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import CallTypes, StandardLoggingPayload
-from litellm.utils import get_prompt_cache_min_tokens, is_prompt_caching_valid_prompt
+from litellm.utils import (
+ get_base_model_from_metadata,
+ get_prompt_cache_min_tokens,
+ is_prompt_caching_valid_prompt,
+)
from ..prompt_caching_cache import PromptCachingCache
@@ -61,7 +65,9 @@ def _honors_one_hour_cache_ttl(
) -> bool:
if standard_logging_object["custom_llm_provider"] == "bedrock":
deployment_model: Final = standard_logging_object["model"]
- capability_model: Final = bedrock_capability_model(deployment_model, litellm_params.get("base_model"))
+ capability_model: Final = bedrock_capability_model(
+ deployment_model, get_base_model_from_metadata({"litellm_params": litellm_params})
+ )
return bedrock_cache_point_ttl_supported(capability_model, deployment_model)
model_map_value: Final = standard_logging_object["model_map_information"]["model_map_value"]
return model_map_value is not None and (model_map_value.get("cache_creation_input_token_cost_above_1hr") or 0) > 0
diff --git a/tests/unit/router_utils/pre_call_checks/test_prompt_caching_deployment_check.py b/tests/unit/router_utils/pre_call_checks/test_prompt_caching_deployment_check.py
index e5565a3df16..5f018ab5dda 100644
--- a/tests/unit/router_utils/pre_call_checks/test_prompt_caching_deployment_check.py
+++ b/tests/unit/router_utils/pre_call_checks/test_prompt_caching_deployment_check.py
@@ -143,6 +143,48 @@ def test_bedrock_application_profile_uses_its_base_model_for_one_hour_ttl() -> N
) is True
+def test_bedrock_application_profile_cache_hit_reads_base_model_from_metadata_model_info() -> None:
+ standard_logging_object: Final = cast(
+ StandardLoggingPayload,
+ {
+ "custom_llm_provider": "bedrock",
+ "model": "bedrock/converse/arn:aws:bedrock:us-east-1:000000000000:application-inference-profile/pinprofile",
+ },
+ )
+ assert (
+ _honors_one_hour_cache_ttl(
+ standard_logging_object,
+ {"metadata": {"model_info": {"base_model": "bedrock/us.anthropic.claude-sonnet-5-5"}}},
+ )
+ is True
+ )
+
+
+def test_bedrock_application_profile_top_level_base_model_wins_over_metadata_model_info() -> None:
+ standard_logging_object: Final = cast(
+ StandardLoggingPayload,
+ {
+ "custom_llm_provider": "bedrock",
+ "model": "bedrock/converse/arn:aws:bedrock:us-east-1:000000000000:application-inference-profile/pinprofile",
+ },
+ )
+ top_level: Final = "bedrock/anthropic.claude-3-haiku-20240307-v1:0"
+ in_metadata: Final = "bedrock/us.anthropic.claude-sonnet-5-5"
+ top_level_alone: Final = _honors_one_hour_cache_ttl(standard_logging_object, {"base_model": top_level})
+ metadata_alone: Final = _honors_one_hour_cache_ttl(
+ standard_logging_object, {"metadata": {"model_info": {"base_model": in_metadata}}}
+ )
+ assert top_level_alone != metadata_alone
+
+ assert (
+ _honors_one_hour_cache_ttl(
+ standard_logging_object,
+ {"base_model": top_level, "metadata": {"model_info": {"base_model": in_metadata}}},
+ )
+ is top_level_alone
+ )
+
+
@pytest.mark.asyncio
async def test_async_filter_deployments_does_not_narrow_prompt_below_model_minimum():
"""