Safa API · aisafa.xyz

Claude Code Environment Variables Reference (2026): Every Config Flag Explained

发布于 2026-07-30 · Safa API

Claude Code is configured almost entirely through environment variables and a project-level settings.json file, not a GUI. If you've ever wondered which variable controls the model, the API endpoint, or the thinking-token budget, this is the reference: every commonly used flag, what it does, and where to set it.

Core Environment Variables

These are the variables you'll touch most often. Set them in your shell profile (.zshrc/.bashrc), a .env file loaded by your terminal, or directly before launching claude.

VariablePurpose
ANTHROPIC_API_KEYYour API key when calling Anthropic's API (or a compatible relay) directly.
ANTHROPIC_BASE_URLOverrides the default API endpoint. Point this at a third-party relay to change billing/pricing without changing any code.
ANTHROPIC_AUTH_TOKENAlternative to ANTHROPIC_API_KEY for bearer-token style auth used by some relays and enterprise gateways.
ANTHROPIC_MODELForces a specific model id (e.g. claude-opus-5, claude-sonnet-5) instead of the CLI default.
ANTHROPIC_SMALL_FAST_MODELModel used for lightweight background tasks (file search summaries, quick classification) — set this to a cheaper/faster model to cut cost.
CLAUDE_CODE_MAX_OUTPUT_TOKENSCaps max output tokens per turn; useful for cost control on long sessions.
MAX_THINKING_TOKENSSets the extended-thinking budget when reasoning mode is on.
DISABLE_AUTOUPDATERSet to 1 to stop Claude Code from checking/installing updates automatically — handy for locked-down CI images.
DISABLE_TELEMETRY / DISABLE_ERROR_REPORTINGOpt out of anonymous usage/error telemetry.
HTTP_PROXY / HTTPS_PROXY / NO_PROXYStandard proxy variables; Claude Code respects them for all outbound requests, including streaming.

settings.json vs Environment Variables

Environment variables are best for secrets and per-machine overrides (API keys, base URLs). For project-shared config that should live in version control, use .claude/settings.json at the repo root — it supports most of the same keys in camelCase form (e.g. model, maxOutputTokens) plus permission rules and hook definitions. Precedence, from highest to lowest: command-line flags → environment variables → project settings.json → user-level ~/.claude/settings.json → built-in defaults. If a teammate says “it works on my machine,” check for a stray env var overriding the committed settings file.

Common Configuration Mistakes

A few issues account for most support threads:

Verifying Your Configuration

After changing any of the above, don't just assume it took effect — verify it. Run claude config list (or check the startup banner) to confirm which model and base URL are actually active for the current session; Claude Code prints the effective base URL on first connect in a fresh terminal. If a request fails with an auth error right after switching relays, first rule out a leftover ANTHROPIC_API_KEY still exported alongside a new ANTHROPIC_AUTH_TOKEN — mixing the two is the single most common cause of confusing 401s after a relay migration. A clean way to test in isolation is to unset both variables, export only the ones the new provider expects, then run one throwaway prompt before resuming real work.

Pointing Claude Code at a Cheaper Relay

Because Claude Code just reads ANTHROPIC_BASE_URL and an auth variable, switching providers is a two-line change — no code edits, no SDK swap. This is exactly how people cut costs without giving up the native Claude Code UX: set the base URL to a relay endpoint, keep the same CLI commands, hooks, and subagents you already built.

This is the setup we use with Safa API: one ANTHROPIC_BASE_URL value gives you Claude Opus 5 / Sonnet 5 at a lower per-token price than direct billing, with prompt caching supported end-to-end so repeated system prompts and long file context stay cheap. No US credit card required — pay by Alipay — and the same account/API key also works as an OpenAI-compatible endpoint if you want GPT or Gemini models in the same project without juggling separate keys.

export ANTHROPIC_BASE_URL="https://aisafa.xyz/v1"
export ANTHROPIC_AUTH_TOKEN="sk-your-safa-key"
claude

Frequently Asked Questions

Does changing ANTHROPIC_BASE_URL break Claude Code's built-in tools (subagents, hooks, MCP)?

No. Base URL only changes where model requests are sent; local features like subagents, hooks, and MCP servers run entirely client-side and are unaffected as long as the relay is OpenAI/Anthropic-API compatible.

Can I set different models for different projects?

Yes — put model in each project's .claude/settings.json instead of a global env var, and each repo will use its own default model.

Why does Claude Code ignore my env var change until I restart?

Claude Code reads environment variables once at process start. After editing your shell profile or exporting a new value, open a new terminal session (or run exec zsh/exec bash) before launching claude again.

立即开始使用 Safa API API 中转

官方直连 · 一个接口接入 Claude / GPT / Gemini · 7×24 稳定

免费注册试用 →