BerriAI/litellm · error · ValueError

Missing Anthropic API Key

Error message

Missing Anthropic API Key

What it means

Raised when the Anthropic files handler cannot build an auth header for the batch-results download call. AnthropicModelInfo.get_auth_header(api_key, api_base) returns None when no usable credential is found (passed api_key or ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN env), and downloading batch results requires direct Anthropic credentials.

Source

Thrown at litellm/llms/anthropic/files/handler.py:91

            HttpxBinaryResponseContent: Binary content wrapped in compatible response format
        """
        file_id: Final = file_content_request.get("file_id")
        if not file_id:
            raise ValueError("file_id is required in file_content_request")

        # Extract batch_id from file_id
        # Handle both formats: "anthropic_batch_results:{batch_id}" or just "{batch_id}"
        if file_id.startswith("anthropic_batch_results:"):
            batch_id = file_id.replace("anthropic_batch_results:", "", 1)
        else:
            batch_id = file_id

        # Get Anthropic API credentials
        api_base = self.anthropic_model_info.get_api_base(api_base)
        auth_header: Final = self.anthropic_model_info.get_auth_header(api_key, api_base)

        if auth_header is None:
            raise ValueError("Missing Anthropic API Key")

        # Construct the Anthropic batch results URL
        encoded_batch_id: Final = encode_url_path_segment(batch_id, field_name="batch_id")
        results_url: Final = f"{api_base.rstrip('/')}/v1/messages/batches/{encoded_batch_id}/results"

        # Prepare headers
        headers: Final = {
            "accept": "application/json",
            "anthropic-version": "2023-06-01",
        }
        headers.update(auth_header)

        # Make the request to Anthropic
        async_client: Final = get_async_httpx_client(llm_provider=LlmProviders.ANTHROPIC)
        anthropic_response: Final = await async_client.get(url=results_url, headers=headers)
        anthropic_response.raise_for_status()

        # Transform Anthropic batch results to OpenAI format

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Set ANTHROPIC_API_KEY (or ANTHROPIC_AUTH_TOKEN) in the environment of the process doing the download.
  2. Or pass api_key explicitly to the file-content call.
  3. In containerized deployments, verify the secret is actually exported into that pod/process env.

Example fix

# before
content = handler.get_file_content(file_content_request={"file_id": fid})  # no env key

# after
import os
os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..."
content = handler.get_file_content(file_content_request={"file_id": fid})
# or pass api_key="sk-ant-..." directly
Defensive patterns

Strategy: validation

Validate before calling

import os

def anthropic_credentials_present(api_key: str | None) -> bool:
    return bool(api_key or os.getenv("ANTHROPIC_API_KEY") or os.getenv("ANTHROPIC_AUTH_TOKEN"))

Try / catch

try:
    content = handler.get_file_content(file_content_request=req, api_key=api_key)
except ValueError as e:
    if "Missing Anthropic API Key" in str(e):
        return http_error(500, "server missing ANTHROPIC_API_KEY configuration")
    raise

Prevention

When it happens

Trigger: Calling get_file_content without api_key while ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN are unset in the environment — e.g. a worker process that fetched batches via the router (credentials held server-side) but downloads results directly.

Common situations: Background jobs / Celery workers lacking the env var; containers where the key is mounted under a different name; assuming the gateway injects credentials into direct handler calls.

Related errors


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