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
[Liveness/Liveliness probe] add separate health app for liveness probes in files (#12669)
Jugal D. Bhatt4b09d0d5171e
diff --git a/docs/my-website/docs/proxy/config_settings.md b/docs/my-website/docs/proxy/config_settings.mdindex e62dc29be94..6bf425581dd 100644--- a/docs/my-website/docs/proxy/config_settings.md+++ b/docs/my-website/docs/proxy/config_settings.md@@ -640,6 +640,8 @@ router_settings:| REQUEST_TIMEOUT | Timeout in seconds for requests. Default is 6000| ROUTER_MAX_FALLBACKS | Maximum number of fallbacks for router. Default is 5| SECRET_MANAGER_REFRESH_INTERVAL | Refresh interval in seconds for secret manager. Default is 86400 (24 hours)+| SEPARATE_HEALTH_APP | If set to '1', runs health endpoints on a separate ASGI app and port. Default: '0'.+| SEPARATE_HEALTH_PORT | Port for the separate health endpoints app. Only used if SEPARATE_HEALTH_APP=1. Default: 4001.| SERVER_ROOT_PATH | Root path for the server application| SET_VERBOSE | Flag to enable verbose logging| SINGLE_DEPLOYMENT_TRAFFIC_FAILURE_THRESHOLD | Minimum number of requests to consider "reasonable traffic" for single-deployment cooldown logic. Default is 1000diff --git a/docs/my-website/docs/proxy/prod.md b/docs/my-website/docs/proxy/prod.mdindex e16c4ed25a6..ba99f412199 100644--- a/docs/my-website/docs/proxy/prod.md+++ b/docs/my-website/docs/proxy/prod.md@@ -232,6 +232,24 @@ To fix this, just set `LITELLM_MIGRATION_DIR="/path/to/writeable/directory"` inLiteLLM will use this directory to write migration files.+## 10. Use a Separate Health Check App+Using a separate health check app ensures that your liveness and readiness probes remain responsive even when the main application is under heavy load.++**Why is this important?**++- If your health endpoints share the same process as your main app, high traffic or resource exhaustion can cause health checks to hang or fail.+- When Kubernetes liveness probes hang or time out, it may incorrectly assume your pod is unhealthy and restart it—even if the main app is just busy, not dead.+- By running health endpoints on a separate lightweight FastAPI app (with its own port), you guarantee that health checks remain fast and reliable, preventing unnecessary pod restarts during traffic spikes or heavy workloads.+- Since the proxy and health app are running in the same pod, if health check app fails, it signifies that the pod is unhealthy and needs to restart/have action taken upon.++**How to enable:**++Set the following environment variable(s):+```bash+SEPARATE_HEALTH_APP="1" # Default "0"+SEPARATE_HEALTH_PORT="8001" # Default "4001", Works only if `SEPARATE_HEALTH_APP` is "1"+```+## Extras### Expected Performance in Productiondiff --git a/litellm/proxy/proxy_cli.py b/litellm/proxy/proxy_cli.pyindex 53c2a665ae6..0be75a8b99b 100644--- a/litellm/proxy/proxy_cli.py+++ b/litellm/proxy/proxy_cli.py@@ -302,6 +302,23 @@ class ProxyInitializationHelpers:return "uvloop"+def run_separate_health_app():+ separate_health = os.getenv("SEPARATE_HEALTH_APP", "0")+ if separate_health == "1":+ from fastapi import FastAPI+ from litellm.proxy.health_endpoints._health_endpoints import router as health_router+ health_app = FastAPI(title="LiteLLM Health Endpoints")+ health_app.include_router(health_router)+ health_port = int(os.getenv("SEPARATE_HEALTH_PORT", 4001))+ print(f"\033[1;32mLiteLLM Health Endpoints: Running on http://0.0.0.0:{health_port}\033[0m")+ import threading+ def run_health_app():+ import uvicorn+ uvicorn.run(health_app, host="0.0.0.0", port=health_port, log_level="info")+ t = threading.Thread(target=run_health_app, daemon=True)+ t.start()++@click.command()@click.option("--host", default="0.0.0.0", help="Host for the server to listen on.", envvar="HOST"@@ -790,7 +807,11 @@ def run_server( # noqa: PLR0915# DO NOT DELETE - enables global variables to work across filesfrom litellm.proxy.proxy_server import app # noqa-++ # --- SEPARATE HEALTH APP LOGIC ---+ run_separate_health_app()+ # --- END SEPARATE HEALTH APP LOGIC ---+# Skip server startup if requested (after all setup is done)if skip_server_startup:print( # noqadiff --git a/tests/test_litellm/proxy/test_proxy_cli.py b/tests/test_litellm/proxy/test_proxy_cli.pyindex 22ba5c53ab5..b78c08d5a9c 100644--- a/tests/test_litellm/proxy/test_proxy_cli.py+++ b/tests/test_litellm/proxy/test_proxy_cli.py@@ -9,6 +9,9 @@ sys.path.insert() # Adds the parent directory to the system-pathfrom litellm.proxy.proxy_cli import ProxyInitializationHelpers+import builtins+from litellm.proxy.proxy_cli import run_separate_health_app+import typesclass TestProxyInitializationHelpers:@@ -308,3 +311,49 @@ class TestProxyInitializationHelpers:# Check that the uvicorn.run was called with the timeout_keep_alive parametercall_args = mock_uvicorn_run.call_argsassert call_args[1]["timeout_keep_alive"] == 30+++def test_run_separate_health_app_runs_only_when_flag(monkeypatch):+ # Patch print to track output+ printed = []+ monkeypatch.setattr(builtins, "print", lambda *args, **kwargs: printed.append(args))+ # Patch threading.Thread to track if it's started+ started = {}+ class DummyThread:+ def __init__(self, target, daemon):+ started['called'] = True+ def start(self):+ started['started'] = True+ monkeypatch.setattr("threading.Thread", DummyThread)+ # Patch uvicorn.run to prevent actual server start+ monkeypatch.setattr("uvicorn.run", lambda *a, **k: None)+ # Patch FastAPI import by patching sys.modules['fastapi']+ class DummyFastAPI:+ def __init__(self, *a, **k):+ pass+ def include_router(self, router):+ pass+ dummy_fastapi = types.ModuleType("fastapi")+ dummy_fastapi.FastAPI = DummyFastAPI+ monkeypatch.setitem(sys.modules, "fastapi", dummy_fastapi)+ # Patch the health_endpoints module in sys.modules+ dummy_health_endpoints = types.ModuleType("litellm.proxy.health_endpoints._health_endpoints")+ dummy_health_endpoints.router = object()+ monkeypatch.setitem(sys.modules, "litellm.proxy.health_endpoints._health_endpoints", dummy_health_endpoints)++ # Should NOT run if flag is not set+ monkeypatch.setenv("SEPARATE_HEALTH_APP", "0")+ started.clear()+ printed.clear()+ run_separate_health_app()+ assert not started, "Should not start thread if flag is not '1'"+ assert not printed, "Should not print if flag is not '1'"++ # Should run if flag is set+ monkeypatch.setenv("SEPARATE_HEALTH_APP", "1")+ started.clear()+ printed.clear()+ run_separate_health_app()+ assert started.get('called'), "Should create thread if flag is '1'"+ assert started.get('started') is None or started.get('started'), "Should start thread if flag is '1'"+ assert any("LiteLLM Health Endpoints" in str(args) for args in printed), "Should print health endpoint message if flag is '1'"