Setting Up Claude Code in a Domestic Network Environment and Connecting to LLM APIs
This article provides a step-by-step guide for developers to install Claude Code in an environment with restricted network access and connect it to domestic large language model (LLM) API providers. It covers Node.js installation, Claude Code setup, selecting a compatible model provider, configuring environment variables, verifying the integration, and estimating API costs.
1. Prerequisites and Scope
This guide is intended for developers who want to use an AI-powered coding assistant in their daily workflow. The setup instructions below rely exclusively on resources accessible from within a restricted network environment, including local npm mirrors and domestic LLM service providers. This article does not discuss or endorse any methods for bypassing network restrictions.
Before you begin, ensure you have the following ready:
- A working computer running Windows, macOS, or Linux.
- A terminal application. On Windows, use PowerShell; on macOS and Linux, use the built-in terminal.
- An active account with a domestic LLM service provider that has model services enabled.
It is important to note that the AI coding assistant itself only handles the interaction and task execution; the actual reasoning is performed by the underlying LLM. Therefore, the core of the integration is to configure the assistant to access an available model API endpoint and to provide the correct authentication key and model name.
2. Step 1: Install and Verify Node.js
Claude Code is installed via the npm package manager, which requires Node.js version 18 or higher. The exact version requirement may be updated by the official documentation, so it’s recommended to check the official Node.js documentation before installation.
Downloading the Installer
Visit the official Node.js website or a trusted local mirror site and download the LTS (Long-Term Support) version. LTS versions are known for stability, making them suitable for daily development. Ensure you are downloading from a legitimate source to avoid security risks. If checksums are provided on the download page, use them to verify the integrity of the installer file.
Installation Methods
- Windows: Download the
.msiinstaller and double-click it to run the setup wizard. - macOS: Download the
.pkginstaller and double-click it to run the setup wizard. - Linux: Use your system’s package manager or follow the official Node.js installation documentation.
Verifying the Installation
After installation, open a new terminal window and run the following commands:
node -vnpm -v
If node -v outputs a version number like v18.20.4 and npm -v outputs a version number, your environment is ready. If you encounter a “command not found” error, it is likely that the environment variables have not been updated. Close all terminal windows and reopen them. If the issue persists, check that the Node.js installation directory is included in your system’s PATH environment variable.
3. Step 2: Install Claude Code
Configuring the npm Registry
npm uses the official registry by default. If the download speed is slow or has frequent timeouts, you can switch to a local mirror. The following example uses the npmmirror registry:
npm config set registry https://registry.npmmirror.comnpm config get registry
The second command will confirm the currently configured registry address. To revert to the official registry, execute:
npm config set registry https://registry.npmjs.org/
Global Installation
Install Claude Code globally using the following command:
npm install -g @anthropic-ai/claude-code
The installation time depends on your network connection. If you encounter a permission error, do not use sudo to force a global installation. A safer approach is to configure a user-level global directory for npm and then attempt the installation again. Refer to the official npm documentation for detailed instructions.
Verifying the Installation
claude --version
If a version number is displayed, the installation was successful. If the command is not found, check that the npm global bin directory is in your PATH. You can find the global installation path by running npm config get prefix.
Please note that the official installation method for Claude Code may change. If there’s a discrepancy between the official documentation and this guide, always defer to the official documentation.
4. Step 3: Choose a Domestic LLM Provider and Obtain an API Key
Provider Selection Criteria
When choosing a provider, consider the following factors:
- API Compatibility: Claude Code uses the Anthropic API format by default. Your chosen provider must offer an endpoint compatible with this format or offer a gateway that can translate between protocols. If the provider only offers a different API format, you’ll need to find a workable conversion solution to integrate successfully.
- Model Capability: Evaluate the provider’s models on code comprehension, generation quality, and long-context handling based on your development needs.
- Rate Limits: Understand the limits on requests per minute and concurrent connections. Frequent calls can cause task interruptions if you hit these limits.
- Pricing Transparency: Confirm how input, output, and cache are billed and check whether the price list is updated over time.
- Invoicing and Compliance: If you require formal documentation for expense reports, verify the provider’s invoicing process and identity verification requirements.
- Data Handling Policies: Review the provider’s privacy policy to understand if your data may be used for model training and how long it is retained.
Application Process
The general process for setting up an account with most providers is as follows:
- Register for an account and complete any necessary identity verification.
- Enable the LLM service from your provider’s console.
- Create an API key. If the console allows, select the option with the least required permissions for your use case.
- Set up a spending limit or budget alert.
- Recharge your account as needed. Some providers may offer a small amount of free credits upon registration.
API Key Security Checklist
An API key acts as a credential for your account; leakage can lead to unauthorized usage and charges. Be sure to:
- Never commit your key to a code repository, document, or chat log.
- Avoid displaying your key in screenshots or screen recordings.
- Prefer storing the key in an environment variable or a local configuration file readable only by you.
- Set up billing alerts and spending limits in your provider’s console.
- Rotate your keys regularly. If you suspect a key has been compromised, immediately revoke it in your console and create a new one.
- Use separate keys for different applications to allow for independent management and revocation.
5. Step 4: Configure Environment Variables to Connect to the Model Service
Claude Code can be configured to use a specific model service endpoint, key, and model name through environment variables. The exact variable names below are based on the official documentation and may be subject to change.
ANTHROPIC_BASE_URL: The compatible endpoint URL provided by your model service provider.ANTHROPIC_AUTH_TOKENorANTHROPIC_API_KEY: The API key you created in your provider’s console. The specific variable to use depends on the provider’s requirements.ANTHROPIC_MODEL: The name of the model you wish to use from your provider.
Temporary Configuration (Current Session Only)
For macOS or Linux:
export ANTHROPIC_BASE_URL="<your-provider-endpoint-url>"export ANTHROPIC_AUTH_TOKEN="<your-api-key>"export ANTHROPIC_MODEL="<your-model-name>"
For Windows PowerShell:
$env:ANTHROPIC_BASE_URL = "<your-provider-endpoint-url>"$env:ANTHROPIC_AUTH_TOKEN = "<your-api-key>"$env:ANTHROPIC_MODEL = "<your-model-name>"
Persistent Configuration (Saved in a File)
To have your configuration loaded every time you open a terminal, you can add an env field to your Claude Code configuration file located in your user directory. The official documentation provides the exact file path and format. Below is an example:
{"env": {"ANTHROPIC_BASE_URL": "<your-provider-endpoint-url>","ANTHROPIC_AUTH_TOKEN": "<your-api-key>","ANTHROPIC_MODEL": "<your-model-name>"}}
After writing the configuration file, set its permissions so that only your current user can read and write it. Configuration changes will take effect after you open a new terminal window.
Important Notice
- Replace the placeholder values in angle brackets with your actual data. Do not commit your real configuration file to version control.
- If you have both environment variables and a configuration file set, the priority of each is defined by the official documentation. When troubleshooting, always check the environment variables in your current terminal first.
- If you use a third-party tool to manage multiple configurations, download it only from the official source and verify the publisher and checksum to avoid installing unverified software.
6. Step 5: Verify the Integration
Basic Verification
Navigate to a code project directory and start Claude Code:
cd your-projectclaude
In the interactive interface, ask a simple question, such as “Describe yourself in one sentence.” If the terminal returns a normal text response without any authentication or network errors, the integration is successful.
Expected Results and Common Errors
| Observation | Possible Cause | Troubleshooting Steps |
|---|---|---|
| Normal response received | Integration is successful | Proceed to test real development tasks. |
| Authentication failure (e.g., 401, 403) | Incorrect key, extra spaces or quotes around the key, or an incorrect variable name | Copy the key again, and check that the variable name matches the provider’s requirements. |
| “Endpoint not found” (e.g., 404) | Incorrect Base URL or an extra path in the URL | Review the provider’s documentation to confirm the correct Base URL. |
| “Model not found” | Model name is misspelled or the model is not activated | Verify the model name and its activation status in the provider’s console. |
| Insufficient balance or quota | Account balance or quota limit is insufficient | Check your usage and balance in the provider’s console. |
| No response for a long time | Network instability or rate limiting by the provider | Try again later and review the rate limit details in the provider’s console. |
Rollback Procedure
To revert to default settings or troubleshoot issues:
- Delete or comment out the
envfield in your configuration file. - On macOS or Linux, run
unset ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN ANTHROPIC_MODEL. On Windows PowerShell, useRemove-Item Env:ANTHROPIC_BASE_URLand similar commands for the other variables. - Open a new terminal and run
claudeagain to verify.
This rollback only changes local settings and does not affect the keys in your provider account. If you are certain you will no longer use a particular key, revoke it in the provider’s console.
7. Estimating Your Costs
The cost of using a pay-as-you-go LLM service is determined by the number of tokens processed and the provider’s per-token price. The formula is:
Cost = (Input Tokens / 1,000,000) × Input Price per Million Tokens + (Output Tokens / 1,000,000) × Output Price per Million Tokens
Input and output prices are subject to change based on the provider’s real-time pricing. It is not recommended to rely on fixed numbers for budgeting, as prices may change with time and model versions.
Let’s illustrate the calculation with a hypothetical example. These prices are for demonstrating the process only and do not reflect any specific provider’s actual rates:
- Assume a task uses 20,000 input tokens and 3,000 output tokens.
- Assume an input price of 2 yuan per million tokens and an output price of 8 yuan per million tokens.
- Cost = (20,000 / 1,000,000) × 2 + (3,000 / 1,000,000) × 8 = 0.04 + 0.024 = 0.064 yuan.
In practice, other factors can influence your final cost:
- Context Length: Longer conversations mean more input tokens are sent with each request.
- File Reading Scope: The more code files the coding assistant reads, the higher your input token count.
- Caching: Some providers may use a different billing rate for cached content.
- Task Complexity: Multi-step modifications and longer tasks will consume more tokens.
We recommend running small test tasks first, monitoring your actual usage in the provider’s console, and then estimating your regular expenses based on those results.
8. Common Troubleshooting
Issue 1: npm installation is stuck or times out
First, verify that the mirror registry is correctly configured by running npm config get registry. Also, check your network connection. You may need to try a different mirror.
