From a341c87f6f15ce7e47160a85687cb415dc9d96bc Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 17:49:54 -0700 Subject: [PATCH] fix(bedrock): price a model_id ARN at its region's row --- litellm/cost_calculator.py | 12 +++++++++++- .../llms/bedrock/embed/test_bedrock_embedding.py | 12 +++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py index 86b22069a39..4da2f33c3ee 100644 --- a/litellm/cost_calculator.py +++ b/litellm/cost_calculator.py @@ -22,6 +22,7 @@ from litellm.constants import ( OPENAI_LIVE_TERMINAL_RESPONSE_EVENT_TYPES, REALTIME_METERED_USAGE_KEY, ) +from litellm.litellm_core_utils.aws_partition import is_bedrock_arn from litellm.litellm_core_utils.core_helpers import as_str_mapping from litellm.litellm_core_utils.cost_discount import resolve_cost_discount from litellm.litellm_core_utils.decisions_pricing import decisions_pricing_fields, is_decisions_call_type @@ -884,6 +885,15 @@ def _bedrock_registration_has_prices(entry: Mapping[str, object]) -> bool: ) +def _bedrock_invoked_profile(invoked_model_id: str) -> ModelInfo | None: + profile: Final = _cost_map_model_info(invoked_model_id, "bedrock") + arn_region: Final = invoked_model_id.split(":")[3] if is_bedrock_arn(invoked_model_id) else None + regional_key: Final = f"bedrock/{arn_region}/{profile['key']}" if profile is not None and arn_region else None + if regional_key is None or _raw_cost_map_entry(regional_key) is None: + return profile + return _cost_map_model_info(regional_key, "bedrock") + + def _bedrock_pricing_info( model: str | None, invoked_model_id: str | None, @@ -900,7 +910,7 @@ def _bedrock_pricing_info( ), None, ) - profile: Final = _cost_map_model_info(invoked_model_id, "bedrock") if invoked_model_id else None + profile: Final = _bedrock_invoked_profile(invoked_model_id) if invoked_model_id else None if profile is None or not _declares_rates(profile): return _cost_map_model_info(registration[0], None) if registration is not None else None if registration is None or registration[0] == profile["key"]: diff --git a/tests/unit/llms/bedrock/embed/test_bedrock_embedding.py b/tests/unit/llms/bedrock/embed/test_bedrock_embedding.py index b689a3fc5f1..b749eb5ed80 100644 --- a/tests/unit/llms/bedrock/embed/test_bedrock_embedding.py +++ b/tests/unit/llms/bedrock/embed/test_bedrock_embedding.py @@ -406,24 +406,22 @@ def test_bedrock_cohere_embedding_bills_the_row_of_the_region_it_calls( def test_bedrock_embedding_with_a_govcloud_arn_model_id_bills_at_the_arn_region_row( respx_mock: respx.MockRouter, ) -> None: - arn: Final = "arn:aws-us-gov:bedrock:us-gov-west-1::foundation-model/amazon.nova-2-multimodal-embeddings-v1:0" route = respx_mock.post(url__startswith=f"{GOVCLOUD_WEST_RUNTIME}/model/").mock( - return_value=httpx.Response( - 200, json={"embeddings": [{"embeddingType": "TEXT", "embedding": [0.1, 0.2, 0.3]}]} - ) + return_value=httpx.Response(200, json=titan_embedding_response) ) response: Final = litellm.embedding( - model="bedrock/amazon.nova-2-multimodal-embeddings-v1:0", + model="bedrock/amazon.titan-embed-text-v2:0", input=[test_input], api_key="test-bearer-token", - model_id=arn, + model_id="arn:aws-us-gov:bedrock:us-gov-west-1::foundation-model/amazon.titan-embed-text-v2:0", ) assert route.call_count == 1 + assert response.usage.prompt_tokens > 0 assert response._hidden_params["response_cost"] == pytest.approx( response.usage.prompt_tokens - * litellm.model_cost["bedrock/us-gov-west-1/amazon.nova-2-multimodal-embeddings-v1:0"]["input_cost_per_token"] + * litellm.model_cost["bedrock/us-gov-west-1/amazon.titan-embed-text-v2:0"]["input_cost_per_token"] )