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]

feat(bedrock): add a config toggle to disable agent-runtime pass-through (#37386)

yuneng-jiangc33b3a32a60c
diff --git a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
index 635767f4db7..c5ab7f1fc63 100644
--- a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
+++ b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
@@ -50,7 +50,7 @@ from litellm.proxy.vector_store_endpoints.utils import (
get_litellm_managed_vector_store,
is_allowed_to_call_vector_store_endpoint,
)
-from litellm.secret_managers.main import get_secret_str
+from litellm.secret_managers.main import get_secret_str, str_to_bool
from litellm.types.passthrough_endpoints.pass_through_endpoints import (
LITELLM_PASS_THROUGH_CUSTOM_BODY_STATE_KEY,
LITELLM_PASS_THROUGH_RAW_BODY_STATE_KEY,
@@ -1016,15 +1016,21 @@ async def bedrock_proxy_route(
raise ImportError("Missing boto3 to call bedrock. Run 'pip install boto3'.")
aws_region_name: Final = litellm.utils.get_secret(secret_name="AWS_REGION_NAME")
- if _is_bedrock_agent_runtime_route(endpoint=endpoint): # handle bedrock agents
- base_target_url: Final = f"https://bedrock-agent-runtime.{aws_region_name}.amazonaws.com"
- else:
+ if not _is_bedrock_agent_runtime_route(endpoint=endpoint):
return await bedrock_llm_proxy_route(
endpoint=endpoint,
request=request,
fastapi_response=fastapi_response,
user_api_key_dict=user_api_key_dict,
)
+
+ if _is_bedrock_agent_runtime_passthrough_disabled():
+ raise HTTPException(
+ status_code=403,
+ detail="bedrock-agent-runtime pass-through is disabled on this proxy.",
+ )
+
+ base_target_url: Final = f"https://bedrock-agent-runtime.{aws_region_name}.amazonaws.com"
encoded_endpoint = httpx.URL(endpoint).path
# Ensure endpoint starts with '/' for proper URL construction
@@ -1292,6 +1298,15 @@ def _is_bedrock_agent_runtime_route(endpoint: str) -> bool:
return False
+def _is_bedrock_agent_runtime_passthrough_disabled() -> bool:
+ from litellm.proxy.proxy_server import general_settings
+
+ setting: Final = general_settings.get("disable_bedrock_agent_runtime_passthrough")
+ if isinstance(setting, str):
+ return str_to_bool(setting) is True
+ return setting is True
+
+
@router.api_route(
"/assemblyai/{endpoint:path}",
methods=["GET", "POST", "PUT", "DELETE", "PATCH"],
diff --git a/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py b/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py
index 050070e2fcf..b56a8da7c66 100644
--- a/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py
+++ b/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py
@@ -1,7 +1,10 @@
+import contextlib
import json
import os
import sys
import traceback
+from collections.abc import Mapping
+from types import MappingProxyType, SimpleNamespace
from typing import Final
from unittest import mock
from unittest.mock import AsyncMock, MagicMock, Mock, patch
@@ -22,6 +25,7 @@ from litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints import (
_join_url_paths,
azure_proxy_route,
bedrock_llm_proxy_route,
+ bedrock_proxy_route,
create_pass_through_route,
cursor_proxy_route,
get_azure_ai_search_index_from_endpoint,
@@ -1730,6 +1734,116 @@ class TestBedrockLLMProxyRoute:
assert "Blocked by guardrail" in str(exc_info.value.detail)
+class TestBedrockAgentRuntimePassthroughToggle:
+ AGENT_RUNTIME_ENDPOINT: Final = "knowledgebases/KB1234567/retrieve"
+ MODEL_ENDPOINT: Final = "model/us.anthropic.claude-sonnet-4-5-20250929-v1:0/converse"
+ DISABLED: Final = MappingProxyType({"disable_bedrock_agent_runtime_passthrough": True})
+
+ @staticmethod
+ def _mock_request() -> Mock:
+ request: Final = Mock()
+ request.method = "POST"
+ request.state = SimpleNamespace()
+ request.json = AsyncMock(return_value={"retrievalQuery": {"text": "hi"}}) # mutable-ok: must be json.dumps-able
+ return request
+
+ @contextlib.contextmanager
+ def _patched_dispatch(self, general_settings: Mapping[str, object]):
+ from botocore.credentials import Credentials
+
+ bedrock_llm: Final = Mock()
+ bedrock_llm.get_credentials = Mock(return_value=Credentials("ak", "sk"))
+ forwarder: Final = AsyncMock(return_value="forwarded")
+
+ with (
+ patch("litellm.proxy.proxy_server.general_settings", general_settings),
+ patch("litellm.utils.get_secret", return_value="us-east-1"),
+ patch("litellm.llms.bedrock.chat.BedrockConverseLLM", return_value=bedrock_llm),
+ patch(
+ "litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints.create_request_copy",
+ Mock(),
+ ),
+ patch(
+ "litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints.create_pass_through_route",
+ return_value=forwarder,
+ ) as create_route,
+ ):
+ yield create_route, forwarder
+
+ @pytest.mark.asyncio
+ async def test_agent_runtime_dispatch_allowed_by_default(self):
+ with self._patched_dispatch(MappingProxyType({})) as (create_route, forwarder):
+ result: Final = await bedrock_proxy_route(
+ endpoint=self.AGENT_RUNTIME_ENDPOINT,
+ request=self._mock_request(),
+ fastapi_response=Mock(),
+ user_api_key_dict=UserAPIKeyAuth(),
+ )
+
+ assert result == "forwarded"
+ forwarder.assert_awaited_once()
+ assert "bedrock-agent-runtime.us-east-1.amazonaws.com" in create_route.call_args.kwargs["target"]
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize("value", (True, "true", "True"))
+ async def test_agent_runtime_dispatch_rejected_when_disabled(self, value: bool | str):
+ settings: Final = MappingProxyType({"disable_bedrock_agent_runtime_passthrough": value})
+
+ with self._patched_dispatch(settings) as (create_route, forwarder):
+ with pytest.raises(HTTPException) as exc_info:
+ await bedrock_proxy_route(
+ endpoint=self.AGENT_RUNTIME_ENDPOINT,
+ request=self._mock_request(),
+ fastapi_response=Mock(),
+ user_api_key_dict=UserAPIKeyAuth(),
+ )
+
+ assert exc_info.value.status_code == 403
+ assert "bedrock-agent-runtime pass-through is disabled" in str(exc_info.value.detail)
+ create_route.assert_not_called()
+ forwarder.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_model_invoke_still_routed_when_agent_runtime_disabled(self):
+ with (
+ patch("litellm.proxy.proxy_server.general_settings", self.DISABLED),
+ patch("litellm.utils.get_secret", return_value="us-east-1"),
+ patch(
+ "litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints.create_request_copy",
+ Mock(),
+ ),
+ patch(
+ "litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints.bedrock_llm_proxy_route",
+ new=AsyncMock(return_value="llm-route"),
+ ) as llm_route,
+ ):
+ result: Final = await bedrock_proxy_route(
+ endpoint=self.MODEL_ENDPOINT,
+ request=self._mock_request(),
+ fastapi_response=Mock(),
+ user_api_key_dict=UserAPIKeyAuth(),
+ )
+
+ assert result == "llm-route"
+ llm_route.assert_awaited_once()
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize("value", (False, "false", None, "", "yes"))
+ async def test_agent_runtime_dispatch_allowed_for_non_true_values(self, value: object):
+ settings: Final = MappingProxyType({"disable_bedrock_agent_runtime_passthrough": value})
+
+ with self._patched_dispatch(settings) as (create_route, forwarder):
+ result: Final = await bedrock_proxy_route(
+ endpoint=self.AGENT_RUNTIME_ENDPOINT,
+ request=self._mock_request(),
+ fastapi_response=Mock(),
+ user_api_key_dict=UserAPIKeyAuth(),
+ )
+
+ assert result == "forwarded"
+ create_route.assert_called_once()
+
+
class TestLLMPassthroughFactoryProxyRoute:
@pytest.mark.asyncio
async def test_llm_passthrough_factory_proxy_route_success(self):