feat(anthropic): support ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL env vars#24140
Conversation
…env vars Co-Authored-By: Claude <[email protected]> Signed-off-by: Devin Petersohn <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR adds support for two environment variables that mirror the official Anthropic Python SDK: Key changes:
Minor inconsistencies:
Confidence Score: 4/5
|
| 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
Last reviewed commit: "Merge branch 'main' ..."
Co-Authored-By: Claude <[email protected]>
…f-contained validate_environment Co-Authored-By: Claude <[email protected]>
…sthrough Co-Authored-By: Claude <[email protected]>
| 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" | ||
| ) |
There was a problem hiding this comment.
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.
| 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.
| 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") |
There was a problem hiding this comment.
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.
| 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.
Relevant issues
N/A
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/test_litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewDelays 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)
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