跳到正文
DEV Community· Alex·· 2 天前AI 評分64

Claude Code 接入兼容 Anthropic API 的閘道:金鑰存放、設定優先次序與上下文壓縮

Claude Code on an Anthropic-compatible gateway: where the key goes, which setting wins, and when it compacts

AI 導讀

文章説明如何把 Claude Code 接入支援 Anthropic Messages API 的第三方端點,並設定 API 位址、金鑰及模型。文中整理設定檔優先次序、apiKeyHelper 的用法及五個模型槽位;非 Claude 模型預設按 200,000 tokens 上下文處理,並可另設壓縮時機。

正文

Disclosure: I run UberLLM; the examples use its API, but the setup works the same with any compatible provider where noted.

Claude Code talks to the Anthropic Messages API. Any endpoint that speaks that API can serve it: a model maker's own Anthropic-compatible endpoint, a company gateway, or a marketplace. I wrote earlier about mapping Claude Code to DeepSeek, GLM or Kimi. This post is the plumbing underneath: where the address and key go, which setting wins when two disagree, how compaction behaves on a non-Claude model, and how to check and undo it.

1. A quick test in one terminal

Two environment variables are enough:

export ANTHROPIC_BASE_URL=https://api.uberllm.dev
export ANTHROPIC_AUTH_TOKEN=ull_your_key
claude

Windows PowerShell:

$env:ANTHROPIC_BASE_URL = "https://api.uberllm.dev"
$env:ANTHROPIC_AUTH_TOKEN = "ull_your_key"
claude

Note the address: the Anthropic-style base URL has no /v1 at the end (the OpenAI-style one, for other tools, is https://api.uberllm.dev/v1). Use your own provider's address and key here; nothing else changes.

This lasts only for the current terminal. For daily use, move it into a settings file.

2. Keep the key out of settings files with apiKeyHelper

Instead of pasting the key into JSON, save it to a file only you can read:

mkdir -p ~/.uberllm && chmod 700 ~/.uberllm
printf '%s\n' 'ull_your_key' > ~/.uberllm/key && chmod 600 ~/.uberllm/key

Then point Claude Code at it:

{
  "apiKeyHelper": "cat ~/.uberllm/key",
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.uberllm.dev"
  }
}

Where that block goes decides its scope:

Scope File
Every project ~/.claude/settings.json
One project <project>/.claude/settings.local.json

A one-project setup leaves your other projects on your normal Anthropic plan.

How the helper behaves:

  • Claude Code runs apiKeyHelper and sends what it prints as both Authorization: Bearer and x-api-key, which is why it works with gateways that read either header.
  • It keeps the answer for five minutes. CLAUDE_CODE_API_KEY_HELPER_TTL_MS changes that.
  • If requests fail with Your apiKeyHelper script is failing, the key file is missing or unreadable.

The key never lands in a settings file, so it does not end up in a backup, a dotfiles repository or a screen share. The pattern is the same for any provider's key.

One more trap: Claude Code reads a project's own settings only after you trust that folder. On a computer where Claude Code was never set up, put the block in ~/.claude/settings.json first, or a brand-new install can still ask you to log in.

3. Which setting wins

Settings files are read in this order, first one wins:

  1. Managed settings (set by your company, if any)
  2. claude --settings <file>
  3. <project>/.claude/settings.local.json
  4. <project>/.claude/settings.json
  5. ~/.claude/settings.json

A variable in a settings file's env block beats the same variable in your shell in most sessions. Two exceptions cause most "why is it still using my Anthropic login?" reports:

  • The key. Claude Code uses ANTHROPIC_AUTH_TOKEN first, then ANTHROPIC_API_KEY, then apiKeyHelper. A token exported in your shell replaces the saved key.
  • The model. /model in the session wins, then claude --model, then ANTHROPIC_MODEL, then the model setting. A shell ANTHROPIC_MODEL beats the model setting.

The fix is boring: once the settings file is in place, remove every ANTHROPIC_* variable from your shell and your shell profile (~/.zshrc, ~/.bashrc).

4. Models: keep Claude, or map every slot

By default nothing is pinned. Claude Code asks for its own model names (opus, sonnet, haiku), and a gateway that serves Claude models answers them.

To run on other models, Claude Code has five slots, set in the env block:

Variable What it sets
ANTHROPIC_MODEL The model the session starts on
ANTHROPIC_DEFAULT_OPUS_MODEL What opus means
ANTHROPIC_DEFAULT_SONNET_MODEL What sonnet means
ANTHROPIC_DEFAULT_HAIKU_MODEL What haiku means; also used for background work such as titles and summaries
CLAUDE_CODE_SUBAGENT_MODEL The model subagents run on

Map every one. A slot left unset keeps asking for a Claude model; on a multi-model gateway that request is served and billed at Claude prices. The model must also support tool calls, since that is how Claude Code edits files and runs commands. On UberLLM a request with tools sent to a model that cannot call them is refused with 400 tools_not_supported; GET https://api.uberllm.dev/v1/models marks supports_tools on each chat model.

5. Context size and compaction

Claude Code knows the context size of Claude models only. For any other model it assumes 200,000 tokens and compacts the conversation before that.

  • If the model has a 1-million-token context, add [1m] to the end of its id, for example "ANTHROPIC_MODEL": "deepseek/deepseek-v4-pro[1m]".
  • For any other size, set CLAUDE_CODE_MAX_CONTEXT_TOKENS to the model's real context, for example "400000".
  • To choose when it compacts: autoCompactWindow (or CLAUDE_CODE_AUTO_COMPACT_WINDOW) takes a number of tokens from 100,000 to 1,000,000. CLAUDE_AUTOCOMPACT_PCT_OVERRIDE (1 to 100) compacts earlier, at that share of the window. autoCompactEnabled: false turns it off.

Do not round up. A context set larger than the model has makes requests fail once the conversation outgrows the model.

6. What changes when you leave Anthropic's endpoint

These apply to any third-party endpoint, not just mine:

  • Your claude.ai login is not used. Features that need it, such as claude.ai connectors, /schedule and Remote Control, are not available in that session.
  • The cost Claude Code shows is not your bill. It is worked out on your computer from Anthropic's list prices, and shows nothing useful for other models. Read your provider's usage page instead. (On UberLLM, a status-line script can show your balance under the prompt; it reads GET /v1/balance at most once a minute.)
  • Your code goes to whoever serves the model. Everything the session reads is sent to the model. On a marketplace like UberLLM that means third-party providers, so do not use it in a repository that holds secrets, credentials or customer data.

7. Check it

Start Claude Code and run /status: the API address should be your provider's. Then call the Messages API directly with the same key:

curl https://api.uberllm.dev/v1/messages \
  -H "Authorization: Bearer $(cat ~/.uberllm/key)" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model":"claude-haiku-4-5","max_tokens":16,"messages":[{"role":"user","content":"ping"}]}'

If that answers and Claude Code does not, the problem is in the settings order (section 3), not the endpoint.

8. Undo it

Remove apiKeyHelper and the ANTHROPIC_* entries from the settings file, and unset any you exported. Claude Code goes back to your Anthropic login on the next start.

If you used one of UberLLM's installers, each has its own undo:

# installed with: curl -fsSL https://uberllm.dev/install/claude-code.sh | sh
curl -fsSL https://uberllm.dev/install/claude-code.sh | sh -s -- --uninstall

# installed with: npx uberllm init
npx uberllm init --undo

Uninstalling stops using the key; it does not revoke it. Delete the key in your provider's dashboard for any computer you no longer trust.

UberLLM is prepaid credit with no free tier, so for a first test with one model family, a maker's own Anthropic-compatible endpoint is also a fine place to start. Sections 2 to 8 apply there too.

Sources

來源:DEV Community · dev.to