BerriAI/litellm · error · ValueError

ANTHROPIC_API_BASE/ANTHROPIC_BASE_URL or ANTHROPIC_API_KEY/A

Error message

ANTHROPIC_API_BASE/ANTHROPIC_BASE_URL or ANTHROPIC_API_KEY/ANTHROPIC_AUTH_TOKEN is not set. Please set the environment variable, to query Anthropic's `/models` endpoint.

What it means

Raised by AnthropicModelInfo.get_models() (the /v1/models listing path) when neither an auth header (from ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN) nor an api base could be resolved from arguments or environment. LiteLLM refuses to guess where to send the request, so listing models fails fast with this ValueError.

Source

Thrown at litellm/llms/anthropic/common_utils.py:786

        resolved_key: Final = AnthropicModelInfo.get_api_key(api_key)
        if resolved_key is not None:
            if is_anthropic_oauth_key(resolved_key):
                return {"authorization": f"Bearer {resolved_key}"}
            return AnthropicModelInfo._make_api_key_auth_header(resolved_key, api_base, use_bearer_for_custom_base)
        auth_token: Final = AnthropicModelInfo.get_auth_token()
        if auth_token is not None:
            return {"authorization": f"Bearer {auth_token}"}
        return None

    @staticmethod
    def get_base_model(model: str | None = None) -> str | None:
        return model.replace("anthropic/", "") if model else None

    def get_models(self, api_key: str | None = None, api_base: str | None = None) -> list[str]:
        api_base = AnthropicModelInfo.get_api_base(api_base)
        auth_header: Final = AnthropicModelInfo.get_auth_header(api_key, api_base)
        if api_base is None or auth_header is None:
            raise ValueError(
                "ANTHROPIC_API_BASE/ANTHROPIC_BASE_URL or ANTHROPIC_API_KEY/ANTHROPIC_AUTH_TOKEN is not set. Please set the environment variable, to query Anthropic's `/models` endpoint."
            )
        headers: Final = {"anthropic-version": "2023-06-01"}
        headers.update(auth_header)
        response: Final = litellm.module_level_client.get(
            url=f"{api_base}/v1/models",
            headers=headers,
        )

        try:
            response.raise_for_status()
        except httpx.HTTPStatusError:
            raise Exception(
                f"Failed to fetch models from Anthropic. Status code: {response.status_code}, Response: {response.text}"
            )

        models: Final = response.json()["data"]

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Set ANTHROPIC_API_KEY (or ANTHROPIC_AUTH_TOKEN) in the environment where the call runs.
  2. If using a gateway, set ANTHROPIC_API_BASE/ANTHROPIC_BASE_URL (and the token var) to its URL.
  3. Alternatively pass api_key=... (and api_base=...) directly to get_models.
  4. Verify with: python -c "import os; print(os.environ.get('ANTHROPIC_API_KEY'))" in the same shell/container.
  5. For docker/CI, ensure secrets are injected into the runtime environment, not only the build environment.

Example fix

# before
models = litellm.model_list  # ValueError: no key/base configured

# after
export ANTHROPIC_API_KEY=sk-ant-...
# or in code:
models = litellm.get_model_list():  # not needed once env is set
# programmatic alternative:
# from litellm.llms.anthropic.common_utils import AnthropicModelInfo
# models = AnthropicModelInfo().get_models(api_key="sk-ant-...")
Defensive patterns

Strategy: validation

Validate before calling

import os

def anthropic_models_config_ok() -> bool:
    return (
        os.getenv("ANTHROPIC_API_KEY") is not None
        or os.getenv("ANTHROPIC_AUTH_TOKEN") is not None
    ) and (
        os.getenv("ANTHROPIC_API_BASE") is not None
        or os.getenv("ANTHROPIC_BASE_URL") is not None
        or True  # default api.anthropic.com is fine when a key exists
    )

if not (os.getenv("ANTHROPIC_API_KEY") or os.getenv("ANTHROPIC_AUTH_TOKEN")):
    raise RuntimeError("Set ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN before listing models")

Try / catch

try:
    models = provider_info.get_models()
except ValueError as e:
    if "ANTHROPIC_API_BASE" in str(e):
        load_dotenv();  # or read from secret manager
        models = provider_info.get_models()
    else:
        raise

Prevention

When it happens

Trigger: Calling litellm.model_list with an anthropic provider (or any code path invoking get_models) in an environment where ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN are both unset — and no api_key was passed — or where the api base cannot be derived from ANTHROPIC_API_BASE/ANTHROPIC_BASE_URL.

Common situations: CI jobs, containers, or serverless functions deployed without the Anthropic env vars; .env file not loaded before the call; variable name typos (ANTHROPIC_APIKEY); assuming models can be listed anonymously.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/434924df322b479d. Report an issue: GitHub.