AI Coding CLI Alternatives: How to Switch Claude Code to a GLM Model with Environment Variables
This article explains how to keep an AI coding assistant working in your terminal when a closed model service becomes unavailable in your region. Instead of hunting for another locked-down product, it takes an architecture approach: separating the client tool from the model layer and routing requests through an API-compatible endpoint. Using Claude Code on Windows 11 (and generally on *nix/macOS) as the example, you will learn how to install the CLI, fix the npm global path issue, and persist model routing through ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN, and the default model mapping variables in your PowerShell profile. It also covers verifying the switch, troubleshooting common errors such as command not found, invalid API key, and 403 Forbidden, and handling token security more safely.
1. Background: AI Coding Tools as a Supply Chain Problem
The AI-assisted coding space has recently produced a textbook supply chain risk scenario. Developers have found that a specific closed model inference service, deeply integrated into certain smart code editors (IDEs), along with its companion standalone CLI tool, has begun enforcing strict regional access restrictions. Developers in affected regions are blocked from code completion, complex logic refactoring, cross-file dependency analysis, and natural language to code conversion.
From an industry perspective, the Claude Sonnet family is widely regarded as top tier for programming because of its long context window understanding, complex algorithmic reasoning, and ability to follow complex instructions. Those capabilities, however, depend on a centralized cloud API. When a provider narrows service coverage for compliance, geopolitical, or commercial reasons, the end user’s stack is exposed to a supply cutoff.
Given this, looking for a replacement should go beyond finding another equally capable closed product. The more durable move is architectural decoupling:
- Separate the tool layer from the model layer. IDE plugins like Cursor and CLI tools like Claude Code have typically bound the client interaction layer tightly to one underlying model. Modern practice favors open or partially open tools that support custom API endpoints, making the backend model pluggable.
- Use protocol compatibility as the bridge. Many terminal agent tools let you override the API base URL and token through environment variables, which works because some vendors designed their APIs to be compatible with widely used message formats (such as the Anthropic API specification).
- Bring in a domestic or compliance-friendly model as the substitute. Models from Chinese vendors have improved markedly in code generation, completion, and logic understanding. Zhipu AI’s GLM-4.7 series, for example, is competitive with top closed models on code writing and long text handling, while being fully compliant and stable in service.
The core of this article is a demonstration of how to take a terminal coding assistant originally built for a specific closed model (Claude Code here) and, through precise environment variable configuration, point it at an API-compatible model service from a Chinese vendor, rebuilding an efficient AI coding experience in the terminal.
2. The Alternative Approach: Full Configuration from Model to Terminal
The goal is to install a command line interactive AI coding assistant on Windows 11 (and generally on *nix/macOS) and switch its underlying model from the restricted closed service to a domestic model API.
2.1 Why These Choices
- Client tool: A widely used command line AI coding assistant serves as the frontend. These tools usually offer rich interaction modes (direct Q&A, code block edits, file read/write assistance), and their key advantage is support for API environment variable injection, which lets you specify a non-default model source.
- Backend model: A model from a vendor with traceable provenance. GLM-4.7, for instance, performs well on coding benchmarks and exposes an interface compatible with common API formats (such as the Anthropic-compatible endpoint offered by Z.AI), which makes a seamless swap possible.
2.2 Architecture
[User terminal (PowerShell/CMD/VS Code Terminal)]|| (runs: claude)v[Claude Code / compatible CLI client]|| (reads env vars: ANTHROPIC_BASE_URL & AUTH_TOKEN)v[Generic API proxy/router]|| (HTTPS request)v[Target model service (e.g., Z.AI or a GLM-compatible endpoint)]|| (returns JSON response stream)v[Results rendered in the UI]
3. Step-by-Step Configuration: Build Your Terminal AI Copilot (Windows 11)
The following guide covers the full setup on Windows 11 using PowerShell.
3.1 Prerequisites
- OS: Windows 11.
- Node.js: Must be installed (v16+ recommended), since AI CLI tools generally run on Node or need global package management via npm.
- PowerShell permissions: Some scripts or persisted environment variable settings may require adjusting the execution policy.
3.2 Install the CLI Tool
Installation is normally done through the Node package manager. Open PowerShell and run the global install:
# Globally install the common CLI AI assistantnpm install -g @anthropic-ai/claude-code
Note: If claude is reported as unrecognized after installation, the npm global path is usually missing from the system Path. Apply this one-time fix:
# Fix the path for the current session and add it permanently to user environment variables$npmGlobalPath = "$env:APPDATA\npm"[Environment]::SetEnvironmentVariable("Path",[Environment]::GetEnvironmentVariable("Path", "User") + ";$npmGlobalPath","User")# Note: after this, close the current terminal and reopen it to load the new environment variables.
3.3 Configure Model Routing (Key Step: Persist the Environment Variables)
This is the heart of the swap. The CLI tool sends requests to the official API by default. Environment variables force it to send requests to the substitute model service instead.
Step 1: Create a PowerShell profile ($PROFILE)
PowerShell can run a script automatically at every launch. Putting the configuration in this file ensures every terminal window loads the model settings.
# 1. Create the profile if it does not existif (!(Test-Path $PROFILE)) {New-Item -Type File -Path $PROFILE -Force}# 2. Open the profile in Notepad to edit itnotepad $PROFILE
Step 2: Paste and adjust the configuration
In the $PROFILE file you just opened (usually Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1), paste the following and replace the API key with your own:
# ========== AI coding assistant + GLM-4.7 configuration start ==========# Core setting: replace the default API address with the compatible domestic model proxy address$env:ANTHROPIC_BASE_URL = "https://api.z.ai/api/anthropic"# Auth token: replace with your domestic model API key$env:ANTHROPIC_AUTH_TOKEN = "YOUR_API_KEY_HERE"# Model mapping: map the high-tier models to your substitute models# Map Haiku to a fast/lightweight model$env:ANTHROPIC_DEFAULT_HAIKU_MODEL = "glm-4.5-air"# Map Sonnet to the main coding model (GLM-4.7)$env:ANTHROPIC_DEFAULT_SONNET_MODEL = "glm-4.7"# Map Opus to the strongest model (GLM-4.7)$env:ANTHROPIC_DEFAULT_OPUS_MODEL = "glm-4.7"# ========== Configuration end ==========
Save and close the file.
3.4 Verify the Configuration
Close all existing terminal windows and open a new PowerShell or VS Code integrated terminal.
- Start the assistant:
claude
- In the interactive session, confirm the model identity:
If the answer mentions “GLM-4.7” or related vendor information, the model routing has switched successfully.What model are you?
4. Troubleshooting: Keeping Your Terminal AI Stable
A few specific problems come up repeatedly during setup. Here is a structured debugging guide.
4.1 Problem: claude reports command not found
- Cause: The Node global package path is not in effect.
- Fix: Make sure you ran the path repair script in section 3.2. If it still fails, check manually whether
$env:Pathcontains%APPDATA%\npm. If it is missing, add it to the system environment variables yourself.
4.2 Problem: The tool reports an invalid API key or a rejected request at startup
- Cause:
ANTHROPIC_AUTH_TOKENis not set correctly, or the token did not load. - Fix:
- Confirm the
$PROFILEfile was saved and the token string has no stray spaces. - Run
$env:ANTHROPIC_AUTH_TOKENin the terminal to check the value. - Confirm your API key is enabled on the relevant platform and has quota remaining.
- Confirm the
4.3 Problem: The tool still calls the official API (e.g., 403 Forbidden)
- Cause:
ANTHROPIC_BASE_URLis misconfigured or not being read. - Fix:
- Check that the value of
ANTHROPIC_BASE_URLexactly matches the compatible API endpoint you are using (watch for a trailing slash). - Some tools probe default API paths, so make sure your base URL points only to the domain or root path and does not include a suffix such as
/v1/messages.
- Check that the value of
4.4 Advanced Consideration: Token Security
Writing the token directly into $PROFILE is convenient but carries security risk. For team collaboration or production environments, prefer:
- System-level secret management: Store the token in the keychain service provided by the operating system.
- Config file reference: Use a script to read the token from an encrypted local config file and inject it into the environment variables.
5. Conclusion: From Dependency to Self-Reliant Development
The evolution of AI coding toolchains shows that access to the underlying model is no longer purely a technical matter; it is a supply chain matter shaped by commercial and compliance considerations. The configuration above does more than solve an immediate “the tool stopped working” problem. It illustrates a shift in development paradigm:
- Value protocol openness. Prefer toolchains that support standard API protocols (such as compatibility with a given request format). That is what gives us the freedom to switch between model providers.
- Use domestic models to fill the gap. Chinese models such as GLM-4.7 have reached an internationally advanced level on key dimensions like code understanding, generation, and modification, and can serve as the core engine of a high-quality development environment.
- The terminal is the core. In complex software engineering, being able to invoke powerful AI directly in the terminal is key to protecting flow state and efficiency.
With this guide, developers get a capable AI assistant usable in any terminal environment, along with the technical autonomy to handle future swings in AI service availability. Open your terminal, run the command, and start coding with a top-tier domestic model driving the experience.
