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>
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>
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.