import time import httpx from fastapi import APIRouter from config import LITELLM_HEALTH_API_KEY, LITELLM_URL router = APIRouter() @router.get("/health") async def litellm_health(): timeout = httpx.Timeout(2.0) checks = ("/health", "/v1/models") api_key = (LITELLM_HEALTH_API_KEY or "").strip() headers = {} if api_key: headers = { "Authorization": f"Bearer {api_key}", "X-API-Key": api_key, } last_error = None async with httpx.AsyncClient(timeout=timeout) as client: for path in checks: url = f"{LITELLM_URL.rstrip('/')}{path}" started = time.perf_counter() try: response = await client.get(url, headers=headers) latency_ms = round((time.perf_counter() - started) * 1000, 1) if response.is_success: return { "ok": True, "status": "up", "latency_ms": latency_ms, "endpoint": path, "http_status": response.status_code, } if response.status_code == 401 and not api_key: return { "ok": True, "status": "auth_required", "latency_ms": latency_ms, "endpoint": path, "http_status": response.status_code, } if response.status_code == 401 and api_key: last_error = f"{path} returned 401 with configured health auth" else: last_error = f"{path} returned {response.status_code}" except Exception as exc: last_error = str(exc) return { "ok": False, "status": "down", "latency_ms": None, "endpoint": None, "error": last_error or "health check failed", }