Repository navigation
Let the Anthropic client authenticate with a bearer token - #70
Merged
Merged
Conversation
The Anthropic client always sent its credential in the `x-api-key` header, so it could only use an API key. The Anthropic API also takes an OAuth access token (what `ANTHROPIC_AUTH_TOKEN` holds for the official SDKs, and what `ant auth print-credentials --access-token` prints after `ant auth login`), and an LLM gateway in front of it usually takes a token of its own. Both go in `Authorization: Bearer <token>`. Add `ai::anthropic::create_client_with_auth_token`, and an `AuthScheme` argument of `AnthropicClient` that selects the header. The `anthropic-version` header is sent in both cases. Nothing changes for an API key. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.
Related: ClickHouse/ClickHouse#136766
The Anthropic client always sends its credential in the
x-api-keyheader, so it can only use an API key.The Anthropic API also takes a bearer token in
Authorization: Bearer <token>:ANTHROPIC_AUTH_TOKENenvironment variable holds for the official SDKs, and whatant auth print-credentials --access-tokenprints afterant auth login;This adds
ai::anthropic::create_client_with_auth_token(auth_token, base_url = "")and anAuthSchemeargument ofAnthropicClient(kApiKey,kBearerToken) that selects the header. Theanthropic-versionheader is sent in both cases. An empty token is aConfigurationError: there is no fallback to an environment variable. Nothing changes for an API key.The branch is based on the commit that ClickHouse pins (c6ae8e2), so that ClickHouse can take this change alone; it merges into
mainwithout conflicts.Tests:
AnthropicClientTest.ApiKeyIsSentInXApiKeyHeader,AuthTokenIsSentAsBearer,BearerTokenHeadersKeepTheApiVersion,CreateClientWithAuthToken.🤖 Generated with Claude Code
Note
Low Risk
Additive auth path with existing API-key behavior preserved; changes are localized to Anthropic client construction and headers.
Overview
The Anthropic client previously only supported API keys via the
x-api-keyheader. This PR addsAuthScheme(kApiKey|kBearerToken) and threads it throughAnthropicClientso credentials can be sent either as before or asAuthorization: Bearer <token>(OAuth access tokens / LLM gateway tokens).A new public entry point
ai::anthropic::create_client_with_auth_token(auth_token, base_url)builds a client in bearer mode; empty tokens throwConfigurationErrorwith no env fallback. Existingcreate_client/ API-key constructors are unchanged in behavior—they default tokApiKey.Unit tests cover header selection, request header building (including retained
anthropic-version), and factory behavior.Reviewed by Cursor Bugbot for commit 5bfa38b. Bugbot is set up for automated code reviews on this repo. Configure here.