{"record":{"id":"273b45481d0eb213","repo":"BerriAI/litellm","slug":"prometheus-url-not-set-please-set-prometheus-url","errorCode":null,"errorMessage":"PROMETHEUS_URL not set please set 'PROMETHEUS_URL=<>' in .env","messagePattern":"PROMETHEUS_URL not set please set 'PROMETHEUS_URL=<>' in \\.env","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/integrations/prometheus_helpers/prometheus_api.py","lineNumber":27,"sourceCode":"\nfrom litellm import get_secret\nfrom litellm._logging import verbose_logger\nfrom litellm.llms.custom_httpx.http_handler import (\n    get_async_httpx_client,\n    httpxSpecialProvider,\n)\n\nPROMETHEUS_URL: Final[str | None] = get_secret(\"PROMETHEUS_URL\")\nPROMETHEUS_SELECTED_INSTANCE: Final[str | None] = get_secret(\"PROMETHEUS_SELECTED_INSTANCE\")\nasync_http_handler: Final = get_async_httpx_client(llm_provider=httpxSpecialProvider.LoggingCallback)\n\n\nasync def get_metric_from_prometheus(\n    metric_name: str,\n):\n    # Get the start of the current day in Unix timestamp\n    if PROMETHEUS_URL is None:\n        raise ValueError(\"PROMETHEUS_URL not set please set 'PROMETHEUS_URL=<>' in .env\")\n\n    query: Final = f\"{metric_name}[24h]\"\n    now: Final = int(time.time())\n    response: Final = await async_http_handler.get(\n        f\"{PROMETHEUS_URL}/api/v1/query\", params={\"query\": query, \"time\": now}\n    )  # End of the day\n    _json_response: Final = response.json()\n    verbose_logger.debug(\"json response from prometheus /query api %s\", _json_response)\n    results: Final = response.json()[\"data\"][\"result\"]\n    return results\n\n\nasync def get_fallback_metric_from_prometheus():\n    \"\"\"\n    Gets fallback metrics from prometheus for the last 24 hours\n    \"\"\"\n    response_message = \"\"\n    relevant_metrics: Final = [","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/integrations/prometheus_helpers/prometheus_api.py#L9-L45","documentation":"get_metric_from_prometheus builds a 24h range query ('{metric_name}[24h]') against {PROMETHEUS_URL}/api/v1/query; PROMETHEUS_URL is read once at module import via get_secret (env or .env) into a module-level Final, so if it was absent at import time every call raises this ValueError regardless of later env changes. This helper backs prometheus-driven metrics/spend logic in the proxy.","triggerScenarios":"Invoking get_metric_from_prometheus (directly or through proxy features that read metrics from Prometheus instead of the DB) in a process started without PROMETHEUS_URL; setting the variable after the interpreter already imported litellm.integrations.prometheus_helpers.prometheus_api; secret present in a different container than the one importing the module.","commonSituations":"Proxy configured for prometheus-backed spend/metrics but the deployment env omitted the URL; .env file not co-located with the running process; env set in the web container but the code runs in a worker.","solutions":["Set PROMETHEUS_URL (e.g. http://prometheus:9090) in the environment before starting the process, then restart so the module-level constant picks it up","Verify from the same process: python -c \"from litellm.constants import get_secret... \" or simply check the env/secret store the process reads","If Prometheus-backed reads are unintended, switch the proxy feature to DB-backed tracking instead","Add PROMETHEUS_URL to your deployment manifest (compose/k8s env) next to other litellm secrets"],"exampleFix":"# before\n# PROMETHEUS_URL never exported\nresults = await get_metric_from_prometheus('litellm_proxy_total_requests_metric')  # ValueError\n\n# after\nimport os\nos.environ['PROMETHEUS_URL'] = 'http://prometheus:9090'  # set BEFORE importing/calling\nfrom litellm.integrations.prometheus_helpers.prometheus_api import get_metric_from_prometheus\nresults = await get_metric_from_prometheus('litellm_proxy_total_requests_metric')","handlingStrategy":"validation","validationCode":"import os\n\nif not os.getenv('PROMETHEUS_URL'):\n    raise RuntimeError('PROMETHEUS_URL must be set before importing prometheus spend helpers')","typeGuard":null,"tryCatchPattern":"try:\n    results = await get_metric_from_prometheus('litellm_proxy_total_requests_metric')\nexcept ValueError as e:\n    logger.error('prometheus query skipped: %s', e)\n    return []","preventionTips":["Set PROMETHEUS_URL in the process environment before startup — it is read once at import","Restart the process after changing the variable; late exports do not apply","Gate prometheus-backed features on a config flag so they are only enabled where the env exists"],"tags":["prometheus","missing-env-var","spend-tracking","configuration"],"backgroundTag":"missing-env-var","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}