Magic Tools
Pitfall NotesBy CooconAugust 24, 2026582 views4 min read

You Set ANTHROPIC_BASE_URL. Claude Code Ignored It.

The Symptom

Point Claude Code at a third-party API gateway — the one-api / new-api family, anything that speaks the Anthropic wire format at your own URL. The setup looks textbook. Three lines in ~/.zshrc:

export ANTHROPIC_BASE_URL="https://xxxx.example.com"
export ANTHROPIC_AUTH_TOKEN="sk-..."
export CLAUDE_CODE_USE_VERTEX=0

New shell, run claude, and the header says:

Fable 5 · Google Vertex AI

Vertex. The gateway's base URL might as well not exist.

Same day, a second problem. CloudCLI — the renamed claudecodeui, a web UI that drives the local claude binary remotely — runs under macOS launchd (the Mac's equivalent of systemd). Log in, and it keeps asking me to pick a provider, claiming it isn't authenticated. Meanwhile the CLI in my terminal is happily hitting the gateway. Same user. Same machine.

Two symptoms, two unrelated root causes, one thing in common: you think the env var is set; the process disagrees.

Pitfall 1: settings.json env Outranks Your Shell Export

First, verify the shell itself. echo $ANTHROPIC_BASE_URL — correct. echo $CLAUDE_CODE_USE_VERTEX — 0. Nothing wrong at that layer.

So go read Claude Code's own config. Sitting in the env block of ~/.claude/settings.json, three lines of leftover test config:

{
  "env": {
    "CLAUDE_CODE_USE_VERTEX": "1",
    "ANTHROPIC_VERTEX_PROJECT_ID": "hello",
    "CLOUD_ML_REGION": "global"
  }
}

ANTHROPIC_VERTEX_PROJECT_ID is literally hello. Obvious placeholder from a Vertex experiment months ago, never cleaned up.

That's the root cause: the env field in settings.json is a forced injection, and it outranks anything you export in your shell. The CLAUDE_CODE_USE_VERTEX=0 in .zshrc gets overwritten with "1" at process start. Claude Code sees a complete Vertex config, takes the Vertex path, and never so much as looks at ANTHROPIC_BASE_URL — that variable isn't on the code path it chose.

The fix is deleting three lines.

claude -p "1+1"
# answers normally, over the gateway

One distinction worth nailing down. The env precedence is not the same rule as the model precedence chain (--model > ANTHROPIC_MODEL > settings.json > default, which I tested separately). In the model chain, settings.json sits below environment variables: it applies only if you didn't set anything else. The env sub-field inverts that: if it's set, it wins. And it hides inside a JSON file you never open, unlike a shell config you scroll past daily — which is exactly why it bites harder than the model chain does.

Pitfall 2: launchd Services Never Read Your Shell Config

CLI fixed. CloudCLI still unauthenticated.

Open its plist:

<!-- ~/Library/LaunchAgents/com.xxxx.claudecodeui.plist -->
<key>EnvironmentVariables</key>
<dict>
    <key>PATH</key>
    <string>/usr/local/bin:/usr/bin:/bin</string>
    <key>HOME</key>
    <string>/Users/xxxx</string>
</dict>

PATH and HOME. That's the whole environment. No ANTHROPIC_BASE_URL, no ANTHROPIC_AUTH_TOKEN.

Root cause: launchd does not read ~/.zshrc. Exactly like systemd on Linux — a service process is not forked from your interactive shell, so every export in .zshrc / .bashrc is invisible to it. CloudCLI's own environment is empty, so the environment it hands down to the claude child process is empty too.

The fix is not stuffing the variables into the plist. That means writing a token into an XML file and maintaining a second copy of your config. Put the gateway settings in the env block of ~/.claude/settings.json instead:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://xxxx.example.com",
    "ANTHROPIC_AUTH_TOKEN": "sk-..."
  }
}

From there, both paths pick it up:

  • the Claude Code CLI reads settings.json's env on every start, independent of whatever the parent process handed it;
  • CloudCLI's auth logic reads that same file on purpose — the comment on its loadSettingsEnv says so outright: it works even when the service process env is empty.

Same mechanism, opposite roles. In pitfall 1 the env block was the culprit; in pitfall 2 it's the cure.

Verification: Simulate the Service Environment With env -i

Don't verify by restarting launchd and logging into the web UI again — that feedback loop is far too slow. Wipe the environment right in your terminal and reproduce what the service actually gets:

env -i HOME="$HOME" PATH="/usr/local/bin:/usr/bin:/bin" claude -p "1+1"

env -i drops every inherited variable and keeps only what you pass explicitly — which is precisely as much as launchd provides. If that command returns an answer, your config genuinely lives somewhere the tool reads for itself, rather than being spoon-fed by the shell.

The trick generalizes to every "works in my terminal, breaks in the service" report: cron, systemd, Docker entrypoints. Same environment discontinuity, same one-line probe.

Lessons to Take Away

  1. When an env var "is set" but does nothing, check env in ~/.claude/settings.json first. A matching key there wins, no matter how many times you export it in your shell. And note the direction is the opposite of the model precedence chain — don't reuse one mental model for both.
  2. Service managers don't read shell config. Processes started by launchd, systemd, or cron cannot see your .zshrc. The moment an env-var-driven tool is launched by one of them, the config has to move somewhere the tool reads itself: settings.json, the service's own EnvironmentVariables, or a dedicated env file.
  3. env -i is the fastest way to fake a clean service environment. No restarts, no re-login — one command tells you whether the config landed in the right place.
  4. Clean up test leftovers. ANTHROPIC_VERTEX_PROJECT_ID: "hello" was obviously throwaway; it should have died with the experiment. It never throws an error. It just quietly fights your next config, months later, from the last file you'd think to open.

Get field notes like this every Saturday

Subscribe to Dev Breakfast: daily AI coding picks at 8:00, plus a Saturday roundup of this week's hands-on tests with Claude Code / Codex / local models. Written in Chinese.

Related Articles

Blocking rm -rf in Claude Code: A Deny Rule Stopped 14 Deletes in Two Weeks, 7 Other Spellings Still Get Through, and a Hook That Closes the Gap

To keep Claude Code from deleting things by accident, I added a deny rule, Bash(rm -rf *), to my global settings. Across my sessions from October 7 to 21 it blocked 14 deletes. After each denial the model picked a new directory name 8 times and dropped the delete 6 times. It never tried another spelling. Then I replayed 22 delete commands verbatim against Claude Code 2.1.285 through a stub. rm -fr, rm -r -f, rm -Rf and rm -r deleted the directory in manual, auto and bypass mode alike. /bin/rm, find -delete and bash -c deleted it in manual and bypass mode. In auto mode, rm -fr inside the working directory skips the classifier because it 'would be allowed in acceptEdits mode'. A second, built-in dangerous-rm check fires on targets like $D/*, even in bypass mode, but loses track of a variable assignment when a heredoc comes earlier in the same command. The post ends with a PreToolUse hook that blocks every recursive spelling in all three modes and tells the model what to do instead.

claude-codehooks+5
pitfallsOct 21, 20268 min
952

Claude Code Auto Mode with DeepSeek, GLM or Kimi: What Each "temporarily unavailable" Reason Means and When Retrying Is Pointless (Fault-Injection Test)

On a third-party model, Claude Code auto mode sends one extra classifier request per side-effecting action, from your session model to the same backend: a 132,470-character system prompt, about 29.7K input tokens per check. When it fails, you see "xxx is temporarily unavailable, so auto mode cannot determine the safety of Bash". We injected faults and recorded each message: 500 (server error), 429 (rate-limited), 529 (overloaded) and connection resets (connection failed) are retried 5 times; no reply in 60 s gives (timed out). **No parentheses means a 4xx rejection: tried once, and retrying never helps.** A reply with no verdict gives "could not evaluate"; three in a row bring back manual approval. CLAUDE_CODE_AUTO_MODE_MODEL has no effect. Claude Code 2.1.285, real DeepSeek v4-pro and MiMo v2.5 backends.

claude-codedeepseek+7
pitfallsOct 11, 202612 min
908

Claude Code "Prompt is too long" vs "maximum context length": Why Auto-Compact Works for One and Not the Other (Tested)

Claude Code decides a request was too long by matching the error text. If the backend says prompt is too long or input is too long for requested model, it auto-compacts the conversation and retries — invisible to you. DeepSeek and OpenAI-style gateways say This model's maximum context length is …, which it doesn't recognize: you get API Error: 400 on every turn. Manual /compact works; the better fix is to declare the real window with CLAUDE_CODE_MAX_CONTEXT_TOKENS (non-claude- model IDs) or CLAUDE_CODE_AUTO_COMPACT_WINDOW (claude- IDs) so it compacts before hitting the limit. Tested on Claude Code 2.1.285 against a local stub.

claude-codedeepseek+6
pitfallsOct 5, 20267 min
890

Claude Code Stuck on a Spinner With No Response: It Waits 6 Minutes per Attempt, and 10 Retries Can Hang It for Over an Hour (Tested)

When Claude Code spins without output or sits at Retrying in 0s, the backend is usually not sending anything. Tested on 2.1.285 (API key + ANTHROPIC_BASE_URL): with no response headers it waits 6 minutes (360 s) per attempt before timing out — setting API_TIMEOUT_MS to 600000 or 900000 doesn't change that; only lower values work. With the default 10 retries it can hang for over an hour. A gateway that buffers the reply until generation finishes will never deliver a reply that takes over 6 minutes. Press Esc to interrupt. All tested against a local stub.

claude-codetroubleshooting+5
pitfallsOct 5, 20269 min
763

Published by Magic Tools