Skip to content

Let the Anthropic client authenticate with a bearer token - #70

Merged
alexey-milovidov merged 1 commit into
mainfrom
anthropic-auth-token
Dec 3, 2026
Merged

alexey-milovidov merged 1 commit into
mainfrom
anthropic-auth-token

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Related: ClickHouse/ClickHouse#136766

The Anthropic client always sends its credential in the x-api-key header, so it can only use an API key.

The Anthropic API also takes a bearer token in Authorization: Bearer <token>:

  • an OAuth access token — what the ANTHROPIC_AUTH_TOKEN environment variable holds for the official SDKs, and what ant auth print-credentials --access-token prints after ant auth login;
  • the token of an LLM gateway placed in front of the API.

This adds ai::anthropic::create_client_with_auth_token(auth_token, base_url = "") and an AuthScheme argument of AnthropicClient (kApiKey, kBearerToken) that selects the header. The anthropic-version header is sent in both cases. An empty token is a ConfigurationError: 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 main without 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-key header. This PR adds AuthScheme (kApiKey | kBearerToken) and threads it through AnthropicClient so credentials can be sent either as before or as Authorization: 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 throw ConfigurationError with no env fallback. Existing create_client / API-key constructors are unchanged in behavior—they default to kApiKey.

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.

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>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Trivial.

@alexey-milovidov alexey-milovidov self-assigned this Dec 3, 2026
@alexey-milovidov
alexey-milovidov merged commit dcaf1ca into main Dec 3, 2026
3 checks passed
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.

1 participant