fix: Anthropic OAuth sends empty X-Api-Key, misclassifies billing error - #6854
Closed
babinc wants to merge 3 commits into
Closed
fix: Anthropic OAuth sends empty X-Api-Key, misclassifies billing error#6854babinc wants to merge 3 commits into
babinc wants to merge 3 commits into
Conversation
… billing error (#6475) Two fixes for the "You're out of extra usage" error when using Claude Code OAuth tokens with the Anthropic provider: 1. **Empty X-Api-Key header override (anthropic_adapter.py)**: When ANTHROPIC_API_KEY="" is set in .env (common after setup), the Anthropic Python SDK reads it from the environment and sends an empty X-Api-Key header alongside the valid Bearer Authorization header. The empty X-Api-Key may cause Anthropic's server to route the request through API-key auth instead of OAuth subscription auth, resulting in a billing error. Fix: explicitly set client.api_key = None after construction when using auth_token (Bearer) auth. 2. **Billing pattern not recognized (error_classifier.py)**: The error message "out of extra usage" was not matched by any billing pattern, causing it to be classified as format_error. This triggered the immediate-abort path instead of proper billing error handling (credential rotation, fallback attempts, correct error messaging). Fix: add "out of extra usage" to _BILLING_PATTERNS. Also fixes beta headers: - Remove non-existent "fine-grained-tool-streaming-2025-05-14" beta - Add "context-1m-2025-08-07" beta (required for 1M context models) - Fix User-Agent header case to properly override SDK default Closes #6475 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Remove assertion for removed fine-grained-tool-streaming beta - Add assertion for new context-1m-2025-08-07 beta - Update api_key assertions: now explicitly None (not absent) for auth_token paths to prevent SDK env var fallback Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
CybercodeXx
pushed a commit
to CybercodeXx/hermes-agent
that referenced
this pull request
Apr 17, 2026
… beta, tool names - api_key=None in Bearer/OAuth paths: prevents SDK from reading ANTHROPIC_API_KEY env var and sending a conflicting X-Api-Key header alongside Authorization: Bearer, which caused auth rejections (NousResearch#6475) - Defensive client.api_key = None post-construction for the same reason - Add context-1m-2025-08-07 to _OAUTH_ONLY_BETAS (1M context for Claude Max) - Fix User-Agent header capitalization in OAuth path - Add "out of extra usage" to billing error patterns - Rename session_search → search_sessions and skill_manage → manage_skills in prompt guidance strings to match actual tool names Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
CybercodeXx
pushed a commit
to CybercodeXx/hermes-agent
that referenced
this pull request
Apr 17, 2026
The beta causes two failures:
1. HTTP 429 "Extra usage is required for long context requests" with
claude-sonnet-4-6 — the beta activates extended-context billing even
for normal-size requests under Claude Max OAuth
2. HTTP 400 "This authentication style is incompatible with the long
context beta header" with claude-haiku-4-5 — model does not support it
Keeping the rest of PR NousResearch#6854: api_key=None fix, User-Agent header,
out-of-extra-usage error pattern, and tool name corrections.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This was referenced Jul 30, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #6475 — "You're out of extra usage" error when using Claude Code OAuth tokens with the Anthropic provider.
1. Empty
X-Api-Keyheader sent alongside OAuth Bearer auth (anthropic_adapter.py)When
ANTHROPIC_API_KEY=(empty) is set in.env(common after setup), the Anthropic Python SDK reads it from the environment and sends bothX-Api-Key:(empty) andAuthorization: Bearer <token>headers. The SDK'sauth_headersproperty merges_api_key_auth(which returns{"X-Api-Key": ""}for empty string, since it only skips onNone) with_bearer_auth. The emptyX-Api-Keymay cause Anthropic's server to route the request incorrectly.Fix: Explicitly pass
api_key=Noneto the SDK constructor and setclient.api_key = Noneafter construction for allauth_tokenpaths, preventing the SDK from reading the empty env var.2. "out of extra usage" not recognized as billing error (
error_classifier.py)The Anthropic error message
"You're out of extra usage"was not matched by any pattern in_BILLING_PATTERNS. It fell through toformat_error, which:is_client_errorpath (no retries, no backoff)Fix: Add
"out of extra usage"to_BILLING_PATTERNS.3. Beta header cleanup (
anthropic_adapter.py)fine-grained-tool-streaming-2025-05-14— does not exist in Claude Code v2.1.81 (zero matches in cli.js)context-1m-2025-08-07to_OAUTH_ONLY_BETAS— enables 1M context window for Opus 4.6 / Sonnet 4.6 (per Anthropic docs embedded in Claude Code)User-Agentheader case:"user-agent"→"User-Agent"— the SDK sets"User-Agent": "Anthropic/Python X.X.X"indefault_headers, and lowercase"user-agent"in_custom_headersdoesn't replace it in the Python dict.httpx.Headersthen concatenates both values (Anthropic/Python 0.93.0, claude-cli/2.1.81 (external, cli)) instead of overriding. Using the same case ensures proper replacement.Note on the underlying error
These fixes improve error handling and header correctness. However, the HTTP 400 "out of extra usage" error from Anthropic may still occur for OAuth subscription tokens used from the Python SDK. During investigation we confirmed:
x-stainless-lang: python/x-stainless-runtime: CPythontelemetry headers that differ from Claude Code's Node.js SDKThe exact server-side cause is unknown — it may be related to client fingerprinting, separate usage pools, or billing routing. Users who hit this consistently can use a direct Anthropic API key (
sk-ant-api*) as a workaround.Test plan
test_error_classifier.py— 88 tests passtest_anthropic_adapter.py— 116 tests pass (2 updated for new betas/api_key behavior)test_fallback_model.py,test_provider_fallback.py,test_anthropic_error_handling.py— 47 tests passclient.api_keyisNonewhen using OAuth authX-Api-Keyheader is not sent when using Bearer authUser-Agentisclaude-cli/X.X.X (external, cli)withoutAnthropic/Pythonprefix🤖 Generated with Claude Code