How to Configure Claude Code for Domestic (China) LLM Providers: Setup and Troubleshooting

    This article explains how to redirect Claude Code, a terminal-based AI coding tool, from its default overseas API endpoint to a domestic (China-based) LLM provider that offers an Anthropic-compatible interface. It covers pre-configuration checks, environment variable setup, secure API key storage, switching between official and domestic models via shell functions, and step-by-step troubleshooting for common connection failures.

    How to Configure Claude Code for Domestic (China) LLM Providers: Setup and Troubleshooting

    Terminal coding tools (using Claude Code as an example) connect to overseas services by default. To use a domestic (China-based) large language model instead, the core task is to redirect the tool’s requests to a compatible domestic endpoint and specify the model to use. This article covers pre-flight checks, environment variable configuration, secure key storage, model switching methods, and failure diagnosis steps. Variable names follow Claude Code’s official documentation; always verify against your installed version.

    1. Four Things to Confirm Before Integration

    1. Tool version: Run claude --version and note the version number. Environment variable names and behavior may change between versions. The information here is based on the documentation-verified version.
    2. Provider endpoint: Confirm that your provider’s documentation explicitly states support for an Anthropic-compatible API. If the provider only offers an OpenAI-compatible interface, Claude Code cannot connect directly; you would need an additional protocol translation gateway, which is outside the scope of this article.
    3. Model names: Use the exact model identifiers listed in your provider’s documentation. Do not rely on memory. Misspelled or deprecated model names will cause immediate request failures.
    4. Account and terms: Verify that your account has API access enabled and that you comply with the provider’s terms of use. The key’s permissions (e.g., callable models, quota limits, IP restrictions) are managed in the provider’s console. It is recommended to grant only the minimum permissions required.

    2. Environment Variables to Configure

    The table below lists the main variables read by Claude Code.

    Variable Purpose Required Format
    ANTHROPIC_BASE_URL The endpoint URL to send requests to The provider’s compatible address, including the full path prefix
    ANTHROPIC_AUTH_TOKEN Authentication credential The API key generated in the provider’s console
    ANTHROPIC_MODEL Default model to use The model identifier from the provider’s documentation
    ANTHROPIC_DEFAULT_HAIKU_MODEL Model for lightweight tasks Suitable for completions, quick Q&A, and speed-sensitive requests
    ANTHROPIC_DEFAULT_SONNET_MODEL Model for standard tasks The primary model for everyday coding
    ANTHROPIC_DEFAULT_OPUS_MODEL Model for complex tasks Large-scale refactoring, cross-module reasoning

    Example configuration (replace the values inside angle brackets with the real values from your provider):

    1. export ANTHROPIC_BASE_URL="https://<provider-compatible-endpoint>"
    2. export ANTHROPIC_AUTH_TOKEN="<injected from key manager, see Section 3>"
    3. export ANTHROPIC_MODEL="<provider-model-name>"
    4. export ANTHROPIC_DEFAULT_HAIKU_MODEL="<lightweight-model-name>"
    5. export ANTHROPIC_DEFAULT_SONNET_MODEL="<standard-model-name>"
    6. export ANTHROPIC_DEFAULT_OPUS_MODEL="<complex-model-name>"

    If both ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN exist in the environment, which one the tool uses depends on the version. It is recommended to keep only one; run unset ANTHROPIC_API_KEY to clear the older variable.

    3. How to Store API Keys Securely

    Do not write the key directly into ~/.zshrc or ~/.bashrc. These files are stored as plain text and are easily synced to cloud drives, captured in screenshots, or accidentally committed to code repositories. Three safer alternatives are:

    • Use a password manager or cloud key management service: Inject the key when the terminal launches, so it never touches disk.
    • Use a local key file: Place the file outside your project directory and restrict permissions to the current user only.
    • In CI/CD environments: Use the pipeline’s encrypted variables or secret references; never hardcode plaintext in scripts.

    Example of a local key file setup:

    1. mkdir -p ~/.config/llm
    2. chmod 700 ~/.config/llm
    3. # Write the key into this file, then tighten permissions
    4. chmod 600 ~/.config/llm/domestic.key
    5. export ANTHROPIC_AUTH_TOKEN="$(cat ~/.config/llm/domestic.key)"

    If you suspect the key has been leaked, revoke it immediately in the provider’s console and generate a new one.

    4. Switching Between Official and Domestic Models with Shell Functions

    To switch frequently, define two functions in ~/.zshrc. The functions only contain the endpoint and model names, not the key itself.

    1. use_domestic() {
    2. export ANTHROPIC_BASE_URL="https://<provider-compatible-endpoint>"
    3. export ANTHROPIC_AUTH_TOKEN="$(cat ~/.config/llm/domestic.key)"
    4. export ANTHROPIC_MODEL="<provider-model-name>"
    5. export ANTHROPIC_DEFAULT_SONNET_MODEL="<provider-model-name>"
    6. }
    7. use_official() {
    8. unset ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN
    9. unset ANTHROPIC_MODEL ANTHROPIC_DEFAULT_SONNET_MODEL
    10. }

    If you need tiered model selection, add the Haiku and Opus variables inside use_domestic. After editing the functions, reload your shell with source ~/.zshrc or open a new terminal.

    5. Troubleshooting Common Failures

    When requests fail, check the following in order:

    1. Verify environment variables are loaded: Run env | grep ANTHROPIC to confirm the variables are set correctly. Ensure ANTHROPIC_BASE_URL has no trailing slash or missing path prefix if the provider requires one.
    2. Test the endpoint manually: Use curl -v <ANTHROPIC_BASE_URL> to confirm the endpoint is reachable. If you get a 401/403 error, your key is invalid or lacks necessary permissions.
    3. Check model name spelling: Compare the model string against the provider’s documentation. Also confirm the model is still in service.
    4. Inspect error messages: Run Claude Code with ANTHROPIC_LOG=debug to obtain more detailed logs. The error message usually indicates whether the issue is connectivity, authentication, or model availability.
    5. Confirm the tool version: Older versions may not support certain environment variables. Update Claude Code to the latest stable release if syntax changes are suspected.

    If all checks pass but the connection still fails, contact your provider’s support with the debug logs and request confirmation of their Anthropic-compatible endpoint format.