Connecting Claude Code to Chinese LLM APIs: A Practical Setup Guide
This guide explains how to configure Anthropic’s Claude Code CLI to work with Chinese LLM providers that offer Anthropic-compatible APIs. It covers environment preparation, installation and verification, onboarding configuration and backup, API key acquisition, `settings.json` configuration, provider-switching tools, editor integration, validation, troubleshooting, and security checks.
1. Understand How the Integration Works
A command-line AI coding tool does not itself understand or generate code. It handles interaction with you, reads project files, organizes requests, and executes commands. The actual reasoning is performed by the large language model behind it. Therefore, setting up the environment can be divided into three steps:
- Install the CLI tool and complete initialization.
- Obtain an API key from a model service provider.
- Send the CLI tool’s requests to the model service address you choose.
Many Chinese model providers now offer interfaces compatible with the Anthropic API protocol. Tools that support this protocol can switch to a Chinese model by changing the request URL, key, and model name. However, compatibility does not mean complete equivalence. Some endpoints, models, and advanced features—such as specific tool calls or image input—may differ. Before each integration, always refer to the provider’s official documentation.
This article uses Anthropic’s official command-line tool, Claude Code, as an example. Other tools follow a similar approach, but configuration file names and field names will differ.
2. Prepare the Runtime Environment
Before starting, confirm the following:
- Node.js version 18 or later; the LTS version is recommended.
- The network can access the npm registry.
- Windows users can use PowerShell or CMD; macOS and Linux users can use the built-in terminal.
Run the following two commands in the terminal to confirm that Node.js and npm are installed:
node -vnpm -v
If version numbers are printed, the environment is ready. If the command is not found, install the LTS version from the Node.js website first.
3. Install Claude Code and Verify It
Install the official package globally with npm:
npm install -g @anthropic-ai/claude-code
After installation, close the current terminal window and open a new one. A global install modifies the system PATH, and an already open old session will not read that change. Running claude directly may therefore produce a “command not found” error. This does not mean the installation failed.
In the new window, run:
claude --version
If a version number is printed, the installation succeeded. If an error still occurs, run npm config get prefix to check the global installation directory and confirm that the bin path under that directory has been added to PATH.
4. Complete the Onboarding Guide and Back Up the Configuration
When Claude Code runs for the first time, it may exit with an error if the onboarding guide has not been completed. The solution is to add a “completed onboarding” flag to the user configuration file.
Configuration file locations:
- Windows:
C:\Users\<your-username>\.claude.json - macOS and Linux:
~/.claude.json
Back up the file before modifying it. This file may already contain other settings, and an incorrect edit can cause configuration loss.
Backup command in Windows PowerShell:
Copy-Item "$env:USERPROFILE\.claude.json" "$env:USERPROFILE\.claude.json.bak"
Backup command in macOS or Linux terminal:
cp ~/.claude.json ~/.claude.json.bak
After backing up, open .claude.json in a text editor and add the following field to the outermost JSON object:
"hasCompletedOnboarding": true
Note two things. First, if the file already contains other fields, append this line to the existing content and add a comma at the end of the previous line. Do not replace the entire file. Second, check that the JSON is valid before saving. You can paste the content into any JSON validation tool to confirm.
After saving, close the terminal, reopen it, and run claude. The tool will ask whether to trust the current working directory. Confirm and press Enter. After entering interactive mode, you can type /exit to quit, or press Ctrl + C twice in the terminal.
5. Obtain an API Key from a Chinese LLM Provider
Using DeepSeek as an example, the application process is as follows:
- Open the official DeepSeek platform website and register or log in.
- Go to the API Keys management page, click the create button, and give the key a recognizable name for this use.
- Copy the key immediately after creation and store it in your password manager.
Most platforms show the full key only once at creation. After closing the dialog, you cannot view it again; you can only delete the old key and create a new one.
Other providers have similar application processes, but the address, field names, and model names are different. Before integration, confirm the following four items in the official documentation:
| Item | Description | How to Check |
|---|---|---|
| Anthropic-compatible Base URL | The address to which requests are sent | Look for sections related to “Anthropic” or “Claude Code” in the documentation |
| Authentication method | Whether to use ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY |
Check request headers or environment variables in documentation examples |
| Model name | For example, an identifier with a version suffix | Check the model list page and pay attention to case |
| Billing rules | Prices vary significantly between versions | Check the pricing page and record the unit price |
6. Configure Model Access via settings.json
Claude Code supports setting environment variables through the env field in ~/.claude/settings.json in the user directory. This allows Claude Code to automatically read the model configuration at every startup without manually modifying system variables.
File locations:
- Windows:
C:\Users\<your-username>\.claude\settings.json - macOS and Linux:
~/.claude/settings.json
Example content is shown below. The Base URL and model name should follow the official documentation of your chosen provider:
{"env": {"ANTHROPIC_BASE_URL": "https://api.deepseek.com/anthropic","ANTHROPIC_AUTH_TOKEN": "YOUR_API_KEY_HERE","ANTHROPIC_MODEL": "deepseek-chat"}}
This file contains a secret key. Do not put it in a code repository. If a similar configuration file also exists in the project directory, add it to .gitignore.
If you prefer to use system environment variables, run the following command. The example below is for Windows PowerShell; reopen the terminal after setting it:
[Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "https://api.deepseek.com/anthropic", "User")
On macOS or Linux, you can add the export statement to ~/.zshrc or ~/.bashrc, then run the source command to apply it.
Using a Provider-Switching Tool
If you need to switch frequently between multiple providers, you can use an open-source configuration-switching tool. For example, cc-switch can graphically manage multiple provider configurations. Before use, note the following:
- Download installation packages only from the project’s GitHub Releases page. Do not use unknown cloud drive links.
- On Windows, choose the
.msiinstaller; on macOS, choose the.dmginstaller. - If the release page provides a SHA256 checksum, verify it before installation.
- When adding a provider, fill in the fields according to the table below:
| Field | Requirement | Common Mistake |
|---|---|---|
| Name | Custom, for example “Chinese model - daily” | Duplicate names causing confusion |
| Base URL | The full address given in the official documentation | Adding or omitting paths such as /anthropic |
| API Key | The fully copied key | Including leading or trailing spaces or line breaks when copying |
| Model name | Exactly consistent with the documentation | Case errors, or selecting a high-cost version by mistake |
| Authentication field | Consistent with documentation requirements | Documentation requires Token, but API Key is entered |
| Timeout and proxy | Adjust only when the network is unstable | Blindly increasing timeout and masking the real problem |
After saving, select the configuration to make it active. A switching tool modifies the currently used configuration, so before switching, confirm that no important task is in progress.
7. Use with a Code Editor
Command-line tools and graphical editors do not conflict. A recommended division of labor is:
- The editor handles code browsing, breakpoint debugging, manual refactoring, and version control.
- The command-line tool handles batch code generation, cross-file modifications, script execution, and code review.
Using VS Code as an example, open the project, go to the project root directory in the integrated terminal, and run claude. The tool can then read the file structure of the current directory.
To reduce the risk of AI modifications, follow this workflow:
- Before the AI starts working, commit or stash current changes to ensure a clean rollback point, for example by running
git stash. - If necessary, create a new branch, for example
git checkout -b ai/feature-xxx, to limit AI changes to an independent branch. - After the AI finishes, run
git statusto view changed files, then rungit diffto review line by line. - After confirming everything is correct, run
git addandgit committo submit.
If an AI modification to a file does not meet expectations, you can restore that file with git restore <file-path>. Before running this command, confirm that the file does not contain unsaved manual changes, because the command discards modifications in the working tree.
8. Verify the Integration
After configuration, verify in the following order:
- Start
claudeand enter/modelin the session to confirm that the current model matches the model name you configured. - Send a simple instruction, such as “Write a quicksort function in Python.”
- If the model returns code normally without authentication or connection errors, the integration is basically working.
- Log in to the provider console and check usage details to confirm that this request was counted against the correct model and account.
9. Troubleshooting Common Errors
When troubleshooting, check in the order of command layer, configuration layer, authentication layer, and network layer rather than changing parameters randomly.
First Layer: Command Layer
- Symptom: “Command not found.”
- Action: Confirm that you reopened the terminal after installation. Run
npm config get prefixand check whether the corresponding bin directory is in PATH.
Second Layer: Configuration Layer
- Symptom: The tool repeatedly asks for initialization, or settings do not take effect.
- Action: Check whether
.claude.jsoncontainshasCompletedOnboarding, check whether the JSON insettings.jsonis valid, and confirm that the tool has been restarted after modification.
Third Layer: Authentication Layer
- Symptom: Returns 401 or 403.
- Action: Copy the key again and confirm there are no extra spaces. Check whether the authentication field name matches the documentation. Confirm that the key has not expired and that the account has available balance.
Fourth Layer: Address and Model Layer
- Symptom: Returns 404, or reports that the model does not exist.
- Action: Check the full path of the Base URL, and do not add or omit path segments. Check the case and version suffix of the model name.
Network Layer
- Symptom: Connection timeout.
- Action: Check whether the network can reach the provider’s domain. If a proxy is used, confirm that environment variables such as
HTTPS_PROXYare set correctly.
Unexpected Costs
- Symptom: The bill is higher than expected.
- Action: Check the model version and unit price in the platform’s usage details, and confirm that a high-cost model was not used by mistake. If the platform supports it, set usage limits or balance alerts.
10. Security Checklist
After configuration, confirm the following items one by one:
- The key does not appear in any code repository, screenshot, or public document.
- Files containing keys, such as
settings.json, have been added to.gitignore. - User configuration has been backed up before modification.
- The switching tool comes from an official release channel, and the installation package has been verified.
- If key leakage is suspected, immediately delete the key in the provider console, create a new key, and update the local configuration.
- Rotate keys regularly, and use different keys for different purposes.
11. Conclusion
This article has covered an executable integration path: install Claude Code and verify the version, back up and complete the onboarding guide, obtain a Chinese model key and check protocol compatibility parameters, configure via settings.json or a switching tool, and then use it together with an editor. Keep three points in mind during use. First, reopen the terminal after installation. Second, back up before modifying configuration, and keep keys only locally and out of repositories. Third, Base URL, authentication fields, and model names should follow the provider’s official documentation. By following these steps, you can use Chinese models locally to assist with programming in a stable way.
