BerriAI/litellm · error · ValueError

Anthropic API key is required. Set ANTHROPIC_API_KEY or ANTH

Error message

Anthropic API key is required. Set ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment variable or pass api_key parameter.

What it means

Header-building validation for the Anthropic Files API transformation. Before sending a create-file request, litellm resolves credentials via AnthropicModelInfo.get_auth_header(api_key, api_base); if none is found it raises this ValueError telling you to set ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN or pass api_key. The Files API is Anthropic-direct, so a credential is mandatory.

Source

Thrown at litellm/llms/anthropic/files/transformation.py:101

            message=error_message,
            headers=(cast(httpx.Headers, headers) if isinstance(headers, dict) else headers),
        )

    def validate_environment(
        self,
        headers: dict,
        model: str,
        messages: list,
        optional_params: dict,
        litellm_params: dict,
        api_key: str | None = None,
        api_base: str | None = None,
    ) -> dict:
        if api_base is None and isinstance(litellm_params, dict):
            api_base = litellm_params.get("api_base")
        auth_header: Final = AnthropicModelInfo.get_auth_header(api_key, api_base)
        if auth_header is None:
            raise ValueError(
                "Anthropic API key is required. Set ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment variable or pass api_key parameter."
            )
        headers.update(
            {
                **auth_header,
                "anthropic-version": "2023-06-01",
                "anthropic-beta": ANTHROPIC_FILES_BETA_HEADER,
            }
        )
        return headers

    def get_supported_openai_params(self, model: str) -> list[OpenAICreateFileRequestOptionalParams]:
        return ["purpose"]

    def map_openai_params(
        self,
        non_default_params: dict,
        optional_params: dict,

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. export ANTHROPIC_API_KEY=sk-ant-... (or ANTHROPIC_AUTH_TOKEN) before running your code.
  2. Or pass api_key explicitly to the file creation call / litellm_params.
  3. For CI, add the key as a masked secret in the pipeline environment.

Example fix

# before
resp = litellm.create_file(file=open("data.jsonl", "rb"), purpose="messages")  # no key anywhere

# after
resp = litellm.create_file(
    file=open("data.jsonl", "rb"),
    purpose="messages",
    api_key="sk-ant-...",  # or export ANTHROPIC_API_KEY first
)
Defensive patterns

Strategy: validation

Validate before calling

import os

def require_anthropic_key(api_key: str | None) -> str:
    key = api_key or os.getenv("ANTHROPIC_API_KEY") or os.getenv("ANTHROPIC_AUTH_TOKEN")
    if not key:
        raise RuntimeError("ANTHROPIC_API_KEY/ANTHROPIC_AUTH_TOKEN not configured; files upload disabled")
    return key

Try / catch

try:
    resp = litellm.create_file(...)
except ValueError as e:
    if "Anthropic API key is required" in str(e):
        return http_error(503, "file upload unavailable: missing Anthropic credentials")
    raise

Prevention

When it happens

Trigger: Calling litellm's create_file against anthropic without an api_key argument and without either env var set in the process. api_base can come from litellm_params, but a base without a key still fails here.

Common situations: Scripts run before exporting the key; multi-provider setups where only other providers' keys are configured; CI environments missing the Anthropic secret.

Related errors


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