:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(llm-gateway): strip global inference prefix for bedrock count_tokens routing (#73293)
Chris Volzer233ffdccabcd
diff --git a/services/llm-gateway/src/llm_gateway/bedrock.py b/services/llm-gateway/src/llm_gateway/bedrock.pyindex 891138f3e246..8c090d1d3d77 100644--- a/services/llm-gateway/src/llm_gateway/bedrock.py+++ b/services/llm-gateway/src/llm_gateway/bedrock.py@@ -310,7 +310,7 @@ async def count_tokens_with_bedrock(def _sync() -> int:bedrock_runtime_client = get_bedrock_runtime_client(aws_region_name, timeout_seconds)- # CountTokens API does not support regional model prefixes ("us.anthropic.", "eu.anthropic.")+ # CountTokens API does not support inference-profile prefixes ("us.anthropic.", "global.anthropic.")count_tokens_model = _strip_regional_inference_prefix(model)# Build the minimal invoke body Bedrock CountTokens accepts. Unlike invoke_model,@@ -331,9 +331,13 @@ async def count_tokens_with_bedrock(def _strip_regional_inference_prefix(model: str) -> str:- """Drop the regional inference-profile prefix ("us.anthropic.", "eu.anthropic.") so the- bare foundation-model id ("anthropic.<model>") is used for token counting."""- return model.replace("us.anthropic.", "anthropic.").replace("eu.anthropic.", "anthropic.")+ """Drop the inference-profile prefix ("us.anthropic.", "eu.anthropic.", "global.anthropic.")+ so the bare foundation-model id ("anthropic.<model>") is used for token counting."""+ return (+ model.replace("us.anthropic.", "anthropic.")+ .replace("eu.anthropic.", "anthropic.")+ .replace("global.anthropic.", "anthropic.")+ )# Models bedrock-runtime CountTokens rejects outright with "The provided model doesn't supportdiff --git a/services/llm-gateway/tests/test_bedrock.py b/services/llm-gateway/tests/test_bedrock.pyindex 1a70cbc2b118..496d783f7bf3 100644--- a/services/llm-gateway/tests/test_bedrock.py+++ b/services/llm-gateway/tests/test_bedrock.py@@ -569,10 +569,12 @@ class TestBedrockCountTokensViaProvider:)@pytest.mark.parametrize(- "model,expected_mantle_model",+ "model,region,expected_mantle_model",[- pytest.param("claude-opus-4-8", "us.anthropic.claude-opus-4-8", id="opus_4_8"),- pytest.param("claude-fable-5", "us.anthropic.claude-fable-5", id="fable_5"),+ pytest.param("claude-opus-4-8", "us-east-1", "us.anthropic.claude-opus-4-8", id="opus_4_8"),+ pytest.param("claude-fable-5", "us-east-1", "us.anthropic.claude-fable-5", id="fable_5"),+ # EU maps fable to the global profile; it must skip runtime CountTokens like the us. form.+ pytest.param("claude-fable-5", "eu-central-1", "global.anthropic.claude-fable-5", id="fable_5_eu_global"),],)@patch("llm_gateway.api.anthropic.get_settings")@@ -586,10 +588,11 @@ class TestBedrockCountTokensViaProvider:authenticated_client: TestClient,valid_request_headers: dict[str, str],model: str,+ region: str,expected_mantle_model: str,) -> None:mock_settings = MagicMock()- mock_settings.bedrock_region_name = "us-east-1"+ mock_settings.bedrock_region_name = regionmock_settings.request_timeout = 300.0mock_get_settings.return_value = mock_settings@@ -921,6 +924,9 @@ class TestSupportsBedrockRuntimeCountTokens:pytest.param("us.anthropic.claude-opus-4-8", False, id="unsupported_opus"),pytest.param("eu.anthropic.claude-opus-4-8", False, id="unsupported_opus_eu_prefix"),pytest.param("us.anthropic.claude-fable-5", False, id="unsupported_fable"),+ # EU routes fable through the global profile (no eu. geo profile exists for it);+ # runtime CountTokens rejects it the same as the us. form.+ pytest.param("global.anthropic.claude-fable-5", False, id="unsupported_fable_global_prefix"),],)def test_classifies_bedrock_model_ids(self, model: str, expected: bool) -> None: