Connecting Chinese LLMs to Claude Code: A Practical Setup Guide
This guide explains how to point Claude Code and similar command-line tools that read `ANTHROPIC_*` environment variables at a compatible endpoint from a Chinese LLM provider. It covers when this setup applies, why developers switch models, how the configuration works, and how to verify the connection with curl.
1. Scope
This guide covers one specific approach. Command-line coding tools that read ANTHROPIC_* environment variables, such as Claude Code, can send model requests to a compatible endpoint provided by a Chinese LLM provider. Other AI coding tools may use different variable names, and some may not support custom endpoints. Before you start, check your tool’s documentation to confirm which variable names and versions it supports.
2. Why Switch to a Chinese Model
Developers switch models for three main reasons.
The first is network reliability. Requests to overseas models travel over cross-border links, and long-context tasks are more likely to time out. If a task has already consumed many tokens and then fails with a timeout before producing any output, both time and money are lost.
The second is account access. Some overseas services restrict access based on registration region or payment method, so an account may be limited. For production workloads that run over a long period, this is a risk to evaluate separately.
The third is cost. Chinese models are usually priced well below overseas flagship models. At high call volumes, the price difference can become a noticeable gap on the bill.
Verify all three points with your own data. Whether the network is stable and whether the bill is actually lower depend on your network environment, task types, and pricing model.
3. How It Works
A compatible endpoint is an address that a provider offers. It accepts requests in the same format as a widely used mainstream protocol. For a coding tool, this means that as long as requests reach that address with the correct key and model name, the tool itself does not need changes.
The configuration therefore involves only two types of settings:
- Base URL:
ANTHROPIC_BASE_URLpoints to the provider’s compatible endpoint. - Authentication and model:
ANTHROPIC_AUTH_TOKENholds the API key you obtained, andANTHROPIC_MODELspecifies the model name.
Some tools also support tier variables such as ANTHROPIC_DEFAULT_HAIKU_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL, and ANTHROPIC_DEFAULT_OPUS_MODEL. The tool assigns lightweight subtasks and main tasks to different tiers internally. If a tier is not set, the matching subtask may not find a usable model and fail. Map all three tiers to a model you have confirmed works.
Authentication variable names may change with tool versions or provider documentation. If authentication fails, check the variable names in the current documentation rather than relying on memory.
4. Setup Steps
Step 1: Get an API key and confirm permissions. Many platforms require you to enable model access separately. After you request a key, confirm in the provider’s console that the target model is enabled.
Step 2: Add the settings to your shell config file. A temporary export only lasts for the current terminal session. Back up the file before editing:
cp ~/.zshrc ~/.zshrc.bak
Then append the following block to the end of the file. Do not hardcode the key in the file. Read it from a password manager or from an environment variable in the current session:
# Example values. Replace them with the real endpoint and model name from your provider.export ANTHROPIC_BASE_URL="https://your-provider.example.com/anthropic"export ANTHROPIC_AUTH_TOKEN="$PROVIDER_API_KEY"export ANTHROPIC_MODEL="your-model-name"export ANTHROPIC_DEFAULT_HAIKU_MODEL="your-model-name"export ANTHROPIC_DEFAULT_SONNET_MODEL="your-model-name"export ANTHROPIC_DEFAULT_OPUS_MODEL="your-model-name"
Run source ~/.zshrc to apply the changes. Do not commit a shell config file that contains keys to a code repository, and do not put keys in project-level tool configuration files.
Step 3: Verify the connection with curl. Before you start the tool, send one request directly to the compatible endpoint:
curl -sS -w "\nHTTP status code: %{http_code}\n" "$ANTHROPIC_BASE_URL/v1/messages" \-H "x-api-key: $ANTHROPIC_AUTH_TOKEN" \-H "anthropic-version: 2023-06-01" \-H "content-type: application/json" \-d "{\"model\": \"$ANTHROPIC_MODEL\", \"max_tokens\": 16, \"messages\": [{\"role\": \"user\", \"content\": \"ping\"}]}"
A 200 response means the endpoint, key, and model name are most likely correct. Request paths and headers should follow the provider’s documentation.
