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
[Feat] LiteLLM Allow setting Uvicorn Keep Alive Timeout (#11594)
Ishaan Jaff55cd5f096cb1
diff --git a/docs/my-website/docs/proxy/deploy.md b/docs/my-website/docs/proxy/deploy.mdindex 852b674a734..4503b0469a2 100644--- a/docs/my-website/docs/proxy/deploy.md+++ b/docs/my-website/docs/proxy/deploy.md@@ -683,7 +683,29 @@ docker run \--run_hypercorn```-### 4. config.yaml file on s3, GCS Bucket Object/url+### 4. Keepalive Timeout++Defaults to 5 seconds. Between requests, connections must receive new data within this period or be disconnected.+++Usage Example:+In this example, we set the keepalive timeout to 75 seconds.++```shell showLineNumbers title="docker run"+docker run ghcr.io/berriai/litellm:main-stable \+ --keepalive_timeout 75+```++Or set via environment variable:+In this example, we set the keepalive timeout to 75 seconds.++```shell showLineNumbers title="Environment Variable"+export KEEPALIVE_TIMEOUT=75+docker run ghcr.io/berriai/litellm:main-stable+```+++### 5. config.yaml file on s3, GCS Bucket Object/urlUse this if you cannot mount a config file on your deployment service (example - AWS Fargate, Railway etc)diff --git a/litellm/proxy/proxy_cli.py b/litellm/proxy/proxy_cli.pyindex 0a9ff2f064b..70fb2e0e518 100644--- a/litellm/proxy/proxy_cli.py+++ b/litellm/proxy/proxy_cli.py@@ -118,6 +118,7 @@ class ProxyInitializationHelpers:host: str,port: int,log_config: Optional[str] = None,+ keepalive_timeout: Optional[int] = None,) -> dict:"""Get the arguments for `uvicorn` worker@@ -135,6 +136,8 @@ class ProxyInitializationHelpers:elif litellm.json_logs:print("Using json logs. Setting log_config to None.") # noqauvicorn_args["log_config"] = None+ if keepalive_timeout is not None:+ uvicorn_args["timeout_keep_alive"] = keepalive_timeoutreturn uvicorn_args@staticmethod@@ -465,6 +468,13 @@ class ProxyInitializationHelpers:default=False,help="Skip starting the server after setup (useful for migrations only)",)+ "--keepalive_timeout",+ default=None,+ type=int,+ help="Set the uvicorn keepalive timeout in seconds (uvicorn timeout_keep_alive parameter)",+ envvar="KEEPALIVE_TIMEOUT",+)def run_server( # noqa: PLR0915host,port,@@ -501,6 +511,7 @@ def run_server( # noqa: PLR0915log_config,use_prisma_migrate,skip_server_startup,+ keepalive_timeout,):args = locals()if local:@@ -774,6 +785,7 @@ def run_server( # noqa: PLR0915host=host,port=port,log_config=log_config,+ keepalive_timeout=keepalive_timeout,)if run_gunicorn is False and run_hypercorn is False:if ssl_certfile_path is not None and ssl_keyfile_path is not None:diff --git a/tests/test_litellm/proxy/test_proxy_cli.py b/tests/test_litellm/proxy/test_proxy_cli.pyindex 70a39df485b..5fd8ea69b2d 100644--- a/tests/test_litellm/proxy/test_proxy_cli.py+++ b/tests/test_litellm/proxy/test_proxy_cli.py@@ -100,6 +100,19 @@ class TestProxyInitializationHelpers:)assert args["log_config"] is None+ # Test with keepalive_timeout+ args = ProxyInitializationHelpers._get_default_unvicorn_init_args(+ "localhost", 8000, None, 60+ )+ assert args["timeout_keep_alive"] == 60++ # Test with both log_config and keepalive_timeout+ args = ProxyInitializationHelpers._get_default_unvicorn_init_args(+ "localhost", 8000, "log_config.json", 120+ )+ assert args["log_config"] == "log_config.json"+ assert args["timeout_keep_alive"] == 120+@patch("asyncio.run")@patch("builtins.print")def test_init_hypercorn_server(self, mock_print, mock_asyncio_run):@@ -245,3 +258,53 @@ class TestProxyInitializationHelpers:assert result.exit_code == 0mock_uvicorn_run.assert_called_once()++ @patch("uvicorn.run")+ @patch("builtins.print")+ def test_keepalive_timeout_flag(self, mock_print, mock_uvicorn_run):+ """Test that the keepalive_timeout flag is properly passed to uvicorn"""+ from click.testing import CliRunner++ from litellm.proxy.proxy_cli import run_server++ runner = CliRunner()++ mock_app = MagicMock()+ mock_proxy_config = MagicMock()+ mock_key_mgmt = MagicMock()+ mock_save_worker_config = MagicMock()++ with patch.dict(+ "sys.modules",+ {+ "proxy_server": MagicMock(+ app=mock_app,+ ProxyConfig=mock_proxy_config,+ KeyManagementSettings=mock_key_mgmt,+ save_worker_config=mock_save_worker_config,+ )+ },+ ), patch(+ "litellm.proxy.proxy_cli.ProxyInitializationHelpers._get_default_unvicorn_init_args"+ ) as mock_get_args:+ mock_get_args.return_value = {+ "app": "litellm.proxy.proxy_server:app",+ "host": "localhost",+ "port": 8000,+ "timeout_keep_alive": 30,+ }++ result = runner.invoke(run_server, ["--local", "--keepalive_timeout", "30"])++ assert result.exit_code == 0+ mock_get_args.assert_called_once_with(+ host="0.0.0.0",+ port=4000,+ log_config=None,+ keepalive_timeout=30,+ )+ mock_uvicorn_run.assert_called_once()++ # Check that the uvicorn.run was called with the timeout_keep_alive parameter+ call_args = mock_uvicorn_run.call_args+ assert call_args[1]["timeout_keep_alive"] == 30