Skip to content

feat(anthropic): support ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL env vars#24140

Merged
5 commits merged intoBerriAI:mainfrom
devin-petersohn:feat/anthropic-auth-token-and-base-url
Mar 20, 2026
Merged

feat(anthropic): support ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL env vars#24140
5 commits merged intoBerriAI:mainfrom
devin-petersohn:feat/anthropic-auth-token-and-base-url

Conversation

@devin-petersohn
Copy link
Copy Markdown
Contributor

@devin-petersohn devin-petersohn commented Mar 19, 2026

Relevant issues

N/A

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Type

🆕 New Feature
🐛 Bug Fix
🧹 Refactoring
📖 Documentation
🚄 Infrastructure
✅ Test

Changes

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 19, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Mar 20, 2026 1:43am

Request Review

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 19, 2026

CLA assistant check
All committers have signed the CLA.

@codspeed-hq
Copy link
Copy Markdown
Contributor

codspeed-hq bot commented Mar 19, 2026

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing devin-petersohn:feat/anthropic-auth-token-and-base-url (3a0652c) with main (2900e0e)

Open in CodSpeed

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 19, 2026

Greptile Summary

This PR adds support for two environment variables that mirror the official Anthropic Python SDK: ANTHROPIC_AUTH_TOKEN (sent as Authorization: Bearer) and ANTHROPIC_BASE_URL (fallback for the API base URL). The change is applied consistently across the main completion path, batches, files, skills, the experimental messages pass-through, and the proxy passthrough endpoint.

Key changes:

  • New AnthropicModelInfo.get_auth_token() and get_auth_header() static helpers centralise credential resolution; get_auth_header correctly routes OAuth tokens (sk-ant-oat*) to Authorization: Bearer and regular API keys to x-api-key
  • validate_environment in common_utils.py now falls back to ANTHROPIC_AUTH_TOKEN when no API key is found, and raises a helpful error message mentioning both variables
  • get_api_base and batches/main.py both add ANTHROPIC_BASE_URL as a lower-priority fallback after ANTHROPIC_API_BASE
  • ANTHROPIC_AUTH_TOKEN is added to the Sentry denylist to prevent secret leakage
  • The proxy passthrough endpoint no longer hard-codes x-api-key: "None" when the API key is absent
  • Comprehensive mock-only unit tests cover all new code paths

Minor inconsistencies:

  • Error messages in litellm/llms/anthropic/batches/transformation.py (line 48) and litellm/llms/anthropic/files/handler.py (line 89) still only mention ANTHROPIC_API_KEY, leaving users unaware of the new ANTHROPIC_AUTH_TOKEN alternative
  • litellm/utils.py validate_environment checks for both env vars but missing_keys.append("ANTHROPIC_API_KEY") (lines 6166 and ~6399) still reports only ANTHROPIC_API_KEY in the missing-key diagnostic

Confidence Score: 4/5

  • Safe to merge — no regressions in the core auth path; remaining issues are minor error-message inconsistencies.
  • The central get_auth_header helper is correct and well-tested. OAuth token routing, credential precedence, and the passthrough x-api-key: None bug are all properly addressed. The only open items are two stale error strings that don't mention ANTHROPIC_AUTH_TOKEN (in batches/transformation.py and files/handler.py) and the missing_keys diagnostic in utils.py — none of which affect runtime correctness.
  • litellm/llms/anthropic/batches/transformation.py and litellm/llms/anthropic/files/handler.py have error messages that still only reference ANTHROPIC_API_KEY; litellm/utils.py appends only ANTHROPIC_API_KEY to missing_keys.

Important Files Changed

Filename Overview
litellm/llms/anthropic/common_utils.py Core of the PR — adds get_auth_token, get_auth_header, and updates validate_environment / get_anthropic_headers to support ANTHROPIC_AUTH_TOKEN. Logic is sound; get_auth_header correctly handles OAuth-key routing. Previous review concerns (null header, OAuth mis-routing, missing credential resolution) are all addressed.
litellm/llms/anthropic/batches/transformation.py Correctly delegates auth resolution to get_auth_header; minor issue: error message text still references only "API Key", not ANTHROPIC_AUTH_TOKEN.
litellm/llms/anthropic/files/handler.py Auth lookup moved to get_auth_header; error string "Missing Anthropic API Key" is still too narrow — it doesn't mention ANTHROPIC_AUTH_TOKEN.
litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py Adds ANTHROPIC_BASE_URL env var fallback and fixes the previous x-api-key: "None" header bug by using get_auth_header.
litellm/utils.py Both validate_environment call-sites correctly check for either env var, but missing_keys.append still only names ANTHROPIC_API_KEY; minor UX inconsistency.
tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py Comprehensive mock-only tests covering every new code path (get_auth_token, get_auth_header, validate_environment fallbacks, passthrough, base-URL chain). No real network calls.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Caller provides api_key] --> B{get_auth_header}
    B --> C{get_api_key\nresolves ANTHROPIC_API_KEY}
    C -->|key found| D{is_anthropic_oauth_key?}
    D -->|yes| E["Authorization: Bearer {key}"]
    D -->|no| F["x-api-key: {key}"]
    C -->|not found| G{get_auth_token\nresolves ANTHROPIC_AUTH_TOKEN}
    G -->|token found| H["Authorization: Bearer {token}"]
    G -->|not found| I[return None → raise error]

    subgraph Base URL Resolution
        J[api_base param] -->|fallback chain| K["ANTHROPIC_API_BASE env"]
        K -->|fallback| L["ANTHROPIC_BASE_URL env"]
        L -->|fallback| M["https://api.anthropic.com"]
    end
Loading

Last reviewed commit: "Merge branch 'main' ..."

Comment thread litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py Outdated
Comment thread litellm/llms/anthropic/common_utils.py
Co-Authored-By: Claude <[email protected]>
Comment thread litellm/llms/anthropic/common_utils.py
Comment thread litellm/llms/anthropic/common_utils.py
@ghost ghost changed the base branch from main to litellm_oss_staging_03_19_2026 March 20, 2026 01:41
@ghost ghost changed the base branch from litellm_oss_staging_03_19_2026 to main March 20, 2026 01:41
@ghost ghost merged commit 48e9d0b into BerriAI:main Mar 20, 2026
37 of 39 checks passed
Comment on lines 47 to 49
raise ValueError(
"Missing Anthropic API Key - A call is being made to anthropic but no key is set either in the environment variables or via params"
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Error message doesn't mention ANTHROPIC_AUTH_TOKEN

The error message was not updated when the auth check was changed from api_key is None to auth_header is None. Users who only set ANTHROPIC_AUTH_TOKEN and hit a misconfiguration would still see a message telling them to set an API key, with no hint that an auth token is also accepted.

Suggested change
raise ValueError(
"Missing Anthropic API Key - A call is being made to anthropic but no key is set either in the environment variables or via params"
)
raise ValueError(
"Missing Anthropic API Key - A call is being made to anthropic but no key is set either in the environment variables or via params. Please set `ANTHROPIC_API_KEY` or `ANTHROPIC_AUTH_TOKEN` in your environment vars"
)

The same issue exists in litellm/llms/anthropic/files/handler.py line 89: raise ValueError("Missing Anthropic API Key") should be updated to mention ANTHROPIC_AUTH_TOKEN as well.

Comment thread litellm/utils.py
if "ANTHROPIC_API_KEY" in os.environ or "ANTHROPIC_AUTH_TOKEN" in os.environ:
keys_in_environment = True
else:
missing_keys.append("ANTHROPIC_API_KEY")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 missing_keys only lists ANTHROPIC_API_KEY

The condition was updated to accept either ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN, but the fallback error message was not updated. When neither variable is set, downstream consumers of missing_keys (e.g. error messages surfaced to the user) will only mention ANTHROPIC_API_KEY, leaving users unaware they can also use ANTHROPIC_AUTH_TOKEN.

Suggested change
missing_keys.append("ANTHROPIC_API_KEY")
missing_keys.append("ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN")

The same issue applies at line ~6399 in the second elif model in litellm.anthropic_models: block.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants