BerriAI/litellm · error · ValueError

PROMETHEUS_URL not set please set 'PROMETHEUS_URL=<>' in…

Error message

PROMETHEUS_URL not set please set 'PROMETHEUS_URL=<>' in .env

What it means

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.

Solutions

  1. 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
  2. Verify from the same process: python -c "from litellm.constants import get_secret... " or simply check the env/secret store the process reads
  3. If Prometheus-backed reads are unintended, switch the proxy feature to DB-backed tracking instead
  4. Add PROMETHEUS_URL to your deployment manifest (compose/k8s env) next to other litellm secrets

Example fix

# before
# PROMETHEUS_URL never exported
results = await get_metric_from_prometheus('litellm_proxy_total_requests_metric')  # ValueError

# after
import os
os.environ['PROMETHEUS_URL'] = 'http://prometheus:9090'  # set BEFORE importing/calling
from litellm.integrations.prometheus_helpers.prometheus_api import get_metric_from_prometheus
results = await get_metric_from_prometheus('litellm_proxy_total_requests_metric')
Defensive patterns

Strategy: validation

Validate before calling

import os

if not os.getenv('PROMETHEUS_URL'):
    raise RuntimeError('PROMETHEUS_URL must be set before importing prometheus spend helpers')

Try / catch

try:
    results = await get_metric_from_prometheus('litellm_proxy_total_requests_metric')
except ValueError as e:
    logger.error('prometheus query skipped: %s', e)
    return []

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/273b45481d0eb213. Report an issue: GitHub.

Appendix: source

Thrown at litellm/integrations/prometheus_helpers/prometheus_api.py:27

from litellm import get_secret
from litellm._logging import verbose_logger
from litellm.llms.custom_httpx.http_handler import (
    get_async_httpx_client,
    httpxSpecialProvider,
)

PROMETHEUS_URL: Final[str | None] = get_secret("PROMETHEUS_URL")
PROMETHEUS_SELECTED_INSTANCE: Final[str | None] = get_secret("PROMETHEUS_SELECTED_INSTANCE")
async_http_handler: Final = get_async_httpx_client(llm_provider=httpxSpecialProvider.LoggingCallback)


async def get_metric_from_prometheus(
    metric_name: str,
):
    # Get the start of the current day in Unix timestamp
    if PROMETHEUS_URL is None:
        raise ValueError("PROMETHEUS_URL not set please set 'PROMETHEUS_URL=<>' in .env")

    query: Final = f"{metric_name}[24h]"
    now: Final = int(time.time())
    response: Final = await async_http_handler.get(
        f"{PROMETHEUS_URL}/api/v1/query", params={"query": query, "time": now}
    )  # End of the day
    _json_response: Final = response.json()
    verbose_logger.debug("json response from prometheus /query api %s", _json_response)
    results: Final = response.json()["data"]["result"]
    return results


async def get_fallback_metric_from_prometheus():
    """
    Gets fallback metrics from prometheus for the last 24 hours
    """
    response_message = ""
    relevant_metrics: Final = [

View on GitHub (pinned to 77b7c6c40c)