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
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Dec 5, 2026
When the `?` command of `clickhouse-client` was used with no AI provider
configured anywhere - no `ai` section in the client configuration, no API key in
the environment, no default credentials for `aiGenerate` on the server - the
client printed a list of things to configure and that was all.

Now the client offers to set a provider up on the spot, once in a session, and
the same setup is available as `? login`:

- An API key of Anthropic or OpenAI, typed with hidden input, with the address
  of the page where a key is created.
- The sign-in of the `ant` command-line tool of Anthropic, when it is installed
  (Apache Ant, which has the same name, is told apart). If nobody is signed in,
  the client offers to run `ant auth login`. The client keeps the command that
  prints a short-lived access token, never a token, and runs it again when the
  provider rejects the token.
- A model server of the user's own - Ollama or LM Studio - when one answers on
  its default address, with a choice among its models.

Nothing that is found is used without the user choosing it. The choice can be
saved to `ai.xml` in the configuration directory of the user
(`~/.config/clickhouse/`), which is created readable by its owner only, and
`? logout` forgets it. A provider in the client configuration takes precedence
over the saved one, which takes precedence over a key in the environment.

Smaller gaps are closed along the way:

- The `ANTHROPIC_AUTH_TOKEN` environment variable is used as a bearer token,
  with a lower precedence than `ANTHROPIC_API_KEY`, as in the SDKs of
  Anthropic.
- The `ANTHROPIC_BASE_URL` and `OPENAI_BASE_URL` environment variables say where
  the requests go when the credential comes from the environment, and the
  confirmation that the client asks for before it uses such a credential names
  the variable and the address.
- `ai.base_url` works with the `anthropic` provider, and a trailing `/v1` of a
  base URL is not sent twice.
- New settings: `ai.auth_token`, and `ai.api_key_command` and
  `ai.auth_token_command`, which run a command that prints the credential (a
  password manager, a vault, `ant auth print-credentials --access-token`).

The credentials of a Claude subscription, such as the ones Claude Code keeps,
are not imported: Anthropic does not permit third-party tools to use them.

The bearer token needs `create_client_with_auth_token` of the SDK:
ClickHouse/ai-sdk-cpp#70

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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