{"record":{"id":"9ead571312188c44","repo":"BerriAI/litellm","slug":"anthropic-api-key-is-required-set-anthropic-api-k","errorCode":null,"errorMessage":"Anthropic API key is required. Set ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment variable or pass api_key parameter.","messagePattern":"Anthropic API key is required\\. Set ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment variable or pass api_key parameter\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/anthropic/files/transformation.py","lineNumber":101,"sourceCode":"            message=error_message,\n            headers=(cast(httpx.Headers, headers) if isinstance(headers, dict) else headers),\n        )\n\n    def validate_environment(\n        self,\n        headers: dict,\n        model: str,\n        messages: list,\n        optional_params: dict,\n        litellm_params: dict,\n        api_key: str | None = None,\n        api_base: str | None = None,\n    ) -> dict:\n        if api_base is None and isinstance(litellm_params, dict):\n            api_base = litellm_params.get(\"api_base\")\n        auth_header: Final = AnthropicModelInfo.get_auth_header(api_key, api_base)\n        if auth_header is None:\n            raise ValueError(\n                \"Anthropic API key is required. Set ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment variable or pass api_key parameter.\"\n            )\n        headers.update(\n            {\n                **auth_header,\n                \"anthropic-version\": \"2023-06-01\",\n                \"anthropic-beta\": ANTHROPIC_FILES_BETA_HEADER,\n            }\n        )\n        return headers\n\n    def get_supported_openai_params(self, model: str) -> list[OpenAICreateFileRequestOptionalParams]:\n        return [\"purpose\"]\n\n    def map_openai_params(\n        self,\n        non_default_params: dict,\n        optional_params: dict,","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/anthropic/files/transformation.py#L83-L119","documentation":"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.","triggerScenarios":"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.","commonSituations":"Scripts run before exporting the key; multi-provider setups where only other providers' keys are configured; CI environments missing the Anthropic secret.","solutions":["export ANTHROPIC_API_KEY=sk-ant-... (or ANTHROPIC_AUTH_TOKEN) before running your code.","Or pass api_key explicitly to the file creation call / litellm_params.","For CI, add the key as a masked secret in the pipeline environment."],"exampleFix":"# before\nresp = litellm.create_file(file=open(\"data.jsonl\", \"rb\"), purpose=\"messages\")  # no key anywhere\n\n# after\nresp = litellm.create_file(\n    file=open(\"data.jsonl\", \"rb\"),\n    purpose=\"messages\",\n    api_key=\"sk-ant-...\",  # or export ANTHROPIC_API_KEY first\n)","handlingStrategy":"validation","validationCode":"import os\n\ndef require_anthropic_key(api_key: str | None) -> str:\n    key = api_key or os.getenv(\"ANTHROPIC_API_KEY\") or os.getenv(\"ANTHROPIC_AUTH_TOKEN\")\n    if not key:\n        raise RuntimeError(\"ANTHROPIC_API_KEY/ANTHROPIC_AUTH_TOKEN not configured; files upload disabled\")\n    return key","typeGuard":null,"tryCatchPattern":"try:\n    resp = litellm.create_file(...)\nexcept ValueError as e:\n    if \"Anthropic API key is required\" in str(e):\n        return http_error(503, \"file upload unavailable: missing Anthropic credentials\")\n    raise","preventionTips":["Add startup checks that fail fast when file features are enabled without Anthropic creds.","Keep credentials in env/secret managers, not hardcoded fallbacks.","In CI, mask and export the key in the job environment."],"tags":["anthropic","auth","api-key","files","environment"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}