Local Alternatives for AI Coding After Service Disruptions: A Practical Guide to Model Switching and Terminal Configuration
This guide explains how to keep using a terminal-based AI coding assistant when its default model endpoint is unreachable. It covers redirecting the Anthropic-compatible API base URL and authentication token to another provider, installing the required tools on Windows, persisting environment variables in the PowerShell profile, verifying the setup, and troubleshooting common errors.
1. Problem Analysis and Overview of Alternative Strategies
1.1 Core Pain Points
Many developers rely on two capabilities in their AI coding workflow. The first is the chat and code generation model built into a graphical AI coding tool. The second is an AI coding assistant that runs independently in the terminal. When the underlying services of either one become unreachable because of network policy changes, the tools appear to fail. In practice, the connection to the API endpoint has been interrupted.
1.2 Core Logic of the Alternative
The general solution is not to find a replacement application. Instead, you change the API endpoint of the model provider. Several cloud providers and AI labs in mainland China now offer model services that are compatible with mainstream API protocols, such as the Anthropic API protocol. You can keep your familiar terminal assistant or coding tool and point its base URL and authentication token to a new provider.
Technical note: Most AI coding assistants support multiple models. By overriding
ANTHROPIC_BASE_URLandANTHROPIC_AUTH_TOKENthrough environment variables or configuration files, you can route requests to another model service that supports the same protocol.
2. Environment Preparation and Basic Installation
Before you begin, make sure your operating system meets the following requirements:
- Operating system: Windows 10 or 11. This guide uses Windows 11 as the example. macOS and Linux follow similar steps.
- Node.js: AI coding assistants are often built on Node.js. Install Node.js 18 or later, and make sure the
npmcommand is available. - Network access: You must be able to reach the API domain of the alternative model provider you choose.
2.1 Enable Script Execution (Windows PowerShell)
On Windows, you need to change the PowerShell execution policy before you can run the configuration scripts in this guide. Run the following command as administrator or as the current user:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
Note: The RemoteSigned policy allows local scripts to run and requires downloaded scripts to be signed by a trusted publisher. It is a relatively safe option.
2.2 Install the AI Coding Assistant CLI
Run the following command in the terminal to install the tool globally:
npm install -g @anthropic-ai/claude-code
Installation tip: If the terminal shows “not recognized” when you type claude after installation, the npm global installation path is not in the system PATH variable. Run the following command to add it, then close and reopen the terminal window:
[Environment]::SetEnvironmentVariable("Path",$env:Path + ";$env:APPDATA\npm",[System.EnvironmentVariableTarget]::User)
3. Core Configuration: Switching to a Model Service Available in Mainland China
This is the most important step in the migration. You modify your terminal profile ($PROFILE) so that model requests are forwarded to a compatible provider in mainland China.
3.1 Open the PowerShell Profile
if (!(Test-Path $PROFILE)) { New-Item -Type File -Path $PROFILE -Force }notepad $PROFILE
If the $PROFILE path is empty, run New-Item -Type File -Path $PROFILE -Force first to create the file.
3.2 Add the Model Service Environment Variables
Paste the following template into the file that opens in Notepad, then replace the placeholders with your own values:
# ========== Model service adaptation configuration ==========# Set the API base URL. Replace it with the compatible endpoint from your chosen provider.$env:ANTHROPIC_BASE_URL = "https://api.example-provider.com/api/anthropic"# Set the authentication token. Replace it with the API key you obtained from the provider.$env:ANTHROPIC_AUTH_TOKEN = "your_API_access_key"# Map the lightweight task model (for example, fast responses and summaries).$env:ANTHROPIC_DEFAULT_HAIKU_MODEL = "light-model-1"# Map the main model for complex tasks (for example, code generation and reasoning).$env:ANTHROPIC_DEFAULT_SONNET_MODEL = "deep-thinking-model-v2"# Map the top-tier model.$env:ANTHROPIC_DEFAULT_OPUS_MODEL = "deep-thinking-model-v2"# ==========================================
Parameter details:
ANTHROPIC_BASE_URL: The core setting. It points to the server address that supports the compatible protocol. All requests are sent to this address instead of the default overseas endpoint.ANTHROPIC_AUTH_TOKEN: The key used for authentication. The provider uses it to identify your account and bill your usage.- Model name variables: These map the model aliases that the tool uses internally to the actual model names offered by the provider. For example, you can map
haiku, the original lightweight model, to a fast-response model from the provider.
Save the file, close Notepad, and restart the terminal window for the changes to take effect.
4. Verify the Configuration and Start the Assistant
After you finish the configuration, follow these steps to confirm that the settings are active:
- Start the assistant: Type
claudein the terminal and press Enter. - Enter interactive mode: Wait for the prompt to appear.
- Check the model: Enter the
/modelcommand, or askWhat model are you?.
If the response shows the model name you configured, such as deep-thinking-model-v2, along with the provider information, the configuration is working. Your terminal AI coding assistant now runs on the model service available in mainland China.
5. Troubleshooting Guide
| Symptom | Possible Cause | Solution |
|---|---|---|
claude command not found |
The Node.js global path is not configured | Run the PATH fix script from section 2.2, then restart the terminal. |
| Environment variables must be configured every time the terminal opens | The variables were not saved to the $PROFILE file |
Make sure the code from section 3.2 is pasted into the $PROFILE file. Fully restart the terminal, not just open a new tab. |
| “Model not found” or “Authentication failed” | ANTHROPIC_AUTH_TOKEN was not replaced correctly, or the API endpoint is wrong |
Check that the token contains no extra spaces, then copy and paste the key again. Confirm that ANTHROPIC_BASE_URL matches the provider’s documentation. |
| Slow responses or timeouts | Network route problems or high provider load | Try a backup domain from the provider, or contact the provider to check network routing. |
6. Best Practices and Considerations
- Protect your keys: Never commit files that contain
ANTHROPIC_AUTH_TOKENto a public code repository. Use an environment variable manager or the operating system’s credential store instead. - Switch between configurations: If you often switch between network environments, you can write separate
.ps1scripts that load different configurations. You can also use conditional logic (if-else) in$PROFILEto load different environment variables. - Evaluate performance: Different providers use different model architectures and quantization levels. Even with the main model mapped, code generation quality and speed can vary. For critical projects, test the model with small code snippets before relying on it.
- Check compatibility limits: Not every feature of the API protocol is fully replicated. Some providers may not support certain advanced features of the original product, such as specific tool calls or precise control over long-context memory. Read the compatibility documentation from the provider before you start.
7. Summary
When overseas AI coding models cannot be used directly in mainland China, you can still work efficiently in your familiar terminal tools by changing the API endpoint and remapping the model names to a model service available in mainland China. This approach works for command-line tools and for graphical AI coding IDEs that let you customize the service address. After completing the setup, developers can work around specific network restrictions and focus on code logic and project architecture.
