How to Connect Claude Code to Anthropic-Compatible Chinese LLM APIs: A Practical Guide

    This guide explains how to configure Claude Code to use Anthropic-compatible endpoints from Chinese LLM providers. It covers prerequisites, Node.js and Claude Code installation, API key and endpoint setup, environment variables, optional GUI configuration management, verification, troubleshooting, cost estimation, and compliance and security considerations.

    Confirm Two Prerequisites First

    Before you start installing, confirm two prerequisites.

    First, Claude Code uses the Anthropic Messages API protocol to send requests by default. You can point Claude Code to a provider only if that provider offers an endpoint compatible with this protocol. If a provider only offers an OpenAI-compatible interface, Claude Code cannot call it directly. In that case, you need a different tool that supports the protocol or a protocol conversion gateway.

    Second, using a third-party API requires compliance with the provider’s terms of service and local laws and regulations. Do not skip this step. The compliance section below covers it in more detail.

    Different providers vary in their support scope and naming for compatible endpoints. Providers such as DeepSeek, Zhipu GLM, and Moonshot Kimi have published Anthropic-compatible access instructions in their official documentation. This article does not replace any provider’s documentation. The Base URL, model ID, and authentication method should always follow the official documentation of the provider you choose.

    Step 1: Prepare the Node.js Runtime

    Claude Code is installed through npm and requires Node.js 18 or later. Installing an LTS (Long-Term Support) version is recommended for better stability. You can download it from the official Node.js website or from a trusted npm mirror.

    After installation, open a terminal and run the following commands to check the versions:

    1. node -v
    2. npm -v

    Both commands should output version numbers. On Windows, PowerShell users can run Get-Command node to confirm the executable location. On macOS and Linux, users can run which node. If the command is not found, the usual cause is that Node.js was not added to the PATH environment variable during installation. Reinstall Node.js, select the option to add it to PATH, and then open a new terminal window.

    Step 2: Install Claude Code

    If the default npm registry is slow in your network, you can switch to a mirror. The following command points npm package downloads to the npmmirror registry:

    1. npm config set registry https://registry.npmmirror.com

    A mirror only speeds up package downloads. It does not affect API protocol compatibility. If you need to restore the official registry later, run:

    1. npm config set registry https://registry.npmjs.org/

    Then perform a global installation:

    1. npm install -g @anthropic-ai/claude-code

    After installation, check the version:

    1. claude --version

    If installation fails, you can first uninstall the incomplete package and then reinstall it:

    1. npm uninstall -g @anthropic-ai/claude-code

    Step 3: Obtain an API Key and Record Key Parameters

    After choosing a provider, complete registration and identity verification in its console, then create an API key. The key is usually displayed in full only once at creation time. Copy it immediately and save it in a password manager or the operating system’s secure storage.

    When adding credit, start with a small amount for testing. Most platforms provide usage details and spending limit settings. Enable these features and create a separate key for Claude Code to make troubleshooting and revocation easier later.

    In the provider documentation, record the following three pieces of information:

    1. Base URL: The address of the Anthropic-compatible endpoint, usually ending with a path such as /anthropic.
    2. Model ID: The exact model name to call. It must match the documentation exactly.
    3. Authentication method: The request header or environment variable name required by the provider. Different providers may use different variable names, so configure them according to their documentation.

    Step 4: Configure Environment Variables

    Claude Code reads the endpoint address, key, and model through environment variables. The table below lists commonly used variables. Variable names should follow the official documentation for the version you installed. Example values are placeholders.

    Variable Purpose Example value (placeholder)
    ANTHROPIC_BASE_URL Provider-compatible endpoint address https://<provider-domain>/anthropic
    ANTHROPIC_AUTH_TOKEN API key <your-key>
    ANTHROPIC_MODEL Default model ID <model-id-from-provider-docs>

    On macOS or Linux, you can write the configuration into your shell profile, such as ~/.zshrc or ~/.bashrc:

    1. export ANTHROPIC_BASE_URL="https://<provider-domain>/anthropic"
    2. export ANTHROPIC_AUTH_TOKEN="<your-key>"
    3. export ANTHROPIC_MODEL="<model-id>"

    On Windows PowerShell, you can temporarily set variables for the current session:

    1. $env:ANTHROPIC_BASE_URL="https://<provider-domain>/anthropic"
    2. $env:ANTHROPIC_AUTH_TOKEN="<your-key>"
    3. $env:ANTHROPIC_MODEL="<model-id>"

    If you want the settings to persist, use the setx command or add them in the system environment variable settings. setx affects only terminals opened afterward; the current window will not be updated.

    Claude Code also supports writing these variables in the env field of the user configuration file. See the documentation for the installed version for the exact file location and format. This approach is convenient for centralized management, but the configuration file will contain the key in plaintext, so file permissions should be restricted so that only you can read it.

    After configuration is complete, be sure to open a new terminal window before running claude to start.

    Optional: Manage Multiple Configurations with a GUI Tool

    If you need to switch between multiple providers, manually changing environment variables can be tedious. Some community GUI configuration tools can centrally manage multiple configurations, such as cc-switch.

    Before using such a tool, note the following:

    • Download the installer only from the project’s official GitHub repository Releases page. Do not use unknown mirrors, forum reposts, or third-party packaged installers.
    • After downloading, verify the SHA256 checksum provided on the release page. If the project provides a digital signature, verify that as well. Do not install if the checksum does not match.
    • These tools modify environment variables or configuration files. Before installing, understand which locations they will write to.
    • If the tool has problems, you can always revert to manual configuration.

    The tool usually requires the following fields. Use the names shown in its interface:

    Field Description
    Provider name A label for distinction, such as “Provider A”
    Base URL Compatible endpoint address
    API Key The key for that provider
    Model ID The model name to call
    Timeout Maximum wait time for a single request
    Max output tokens Upper limit for a single response, used to control cost

    Back up the current configuration file before switching configurations. If switching fails, restore the backup file and reopen the terminal to roll back.

    Verify That the Configuration Works

    Start Claude Code in a new terminal and enter a simple test request:

    1. Write a Python quicksort function with brief comments.

    If the model returns code and an explanation, the whole chain is working. Next, test tasks such as code explanation and unit test generation in a real project to judge whether response speed and output quality meet your needs.

    Response speed is affected by the network path, provider load, and context length. If it is noticeably slow, try changing the model or reducing the maximum output tokens.

    Troubleshooting Common Issues

    1. EACCES Permission Error During Global Installation

    This usually happens on macOS or Linux when the current user does not have permission to write to npm’s global directory. Installing with sudo is not recommended. A safer approach is to change the npm global directory to a user directory:

    1. npm config get prefix
    2. mkdir -p ~/.npm-global
    3. npm config set prefix ~/.npm-global

    Then add ~/.npm-global/bin to PATH and run the global installation again.

    2. API Returns 401 or 403

    This indicates authentication failure. Check the following items in order:

    • Whether the key was copied completely, with no extra spaces or missing trailing characters.
    • Whether the environment variable names match the provider documentation.
    • Whether the Base URL matches the provider that issued the key.
    • Whether the account has completed identity verification and has sufficient balance.

    Do not paste keys into screenshots, chat histories, or logs during troubleshooting.

    3. Model Not Found or Model ID Error

    Copy the exact model ID from the model list in the provider documentation and compare it character by character. Some providers offer multiple versions of the same model, and an incorrect version number can also cause this error.

    4. Environment Variables Not Taking Effect

    Check the following:

    • Whether you opened a new terminal window after changing the configuration.
    • On macOS or Linux, run echo $ANTHROPIC_BASE_URL; on Windows PowerShell, run echo $env:ANTHROPIC_BASE_URL to confirm the variable is set.
    • If you are using an IDE’s integrated terminal, restart the IDE.
    • Check whether the shell profile is loaded correctly.

    5. Request Timeout or No Response

    First check whether proxy variables are set. On macOS or Linux, run env | grep -i proxy; on Windows, run set | findstr /i proxy. If variables such as HTTP_PROXY exist and the proxy is not suitable for the provider domain, temporarily clear them or add the provider domain to NO_PROXY.

    How to Estimate Costs

    Many articles give a fixed cost number, but provider pricing, models, input/output ratios, and cache hit rates vary widely, so a fixed number has little reference value. A more reliable approach is to estimate it yourself:

    Monthly cost ≈ (Daily input tokens ÷ 1,000,000 × input price + Daily output tokens ÷ 1,000,000 × output price) × 30

    Assume a provider charges 1 currency unit per million input tokens and 4 currency units per million output tokens, with 2 million input tokens and 200,000 output tokens per day. The daily cost would be about 2.8, and the monthly cost about 84. These numbers are only for demonstrating the calculation method and do not represent any provider’s actual prices.

    When estimating, note that input tokens include conversation context and code file contents, so actual consumption is often higher than expected. If the provider offers cache billing, the price for cache-hit portions may be lower. Prices change over time, so use the official price at the time of purchase.

    Also compare API costs with the price of an official subscription. Check the official website for current subscription pricing.

    Compliance and Security Notes

    Pay attention to the following before connecting:

    • Read the provider’s terms of service and the relevant terms of use for Claude Code, and confirm that this type of access is permitted.
    • Violating the terms of service may lead to account restrictions, service interruption, or price changes. This article cannot guarantee that any service will remain available long term.
    • Comply with local laws and regulations. Do not use the tool for illegal purposes.
    • Follow the principle of least privilege for API keys. Create a separate key for Claude Code, set spending limits, rotate it regularly, do not commit it to code repositories, and do not expose it in screenshots or logs.
    • If a key is accidentally leaked, revoke it in the console immediately and check usage details.
    • An official subscription is a compliant and stable fallback option for scenarios that require high service continuity.

    Conclusion

    After completing the steps above, you have configured a terminal-based AI coding environment locally. To keep it running reliably over time, develop the following habits: before updating tools, back up configuration files and read the changelog; when downloading a new installer, verify its checksum; keep a backup provider configuration so you can switch quickly if the primary service becomes unavailable. After every configuration change, verify it with a test request to confirm the result meets expectations.