A Practical Guide to Configuring CLI Coding Agents with Alternative LLMs
This article provides a comprehensive guide on how to replace the default models in command-line (CLI) coding agents with alternative large language models (LLMs) such as GLM. By leveraging API-compatible endpoints and core environment variables, developers can bypass regional restrictions, high costs, and vendor lock-in while retaining the agent's full capabilities. The guide covers client installation, credential setup, core JSON configuration, verification steps, and troubleshooting tips to help you build a cost-effective AI-assisted programming workflow.
1. Background and Requirements
Command-line (CLI) coding agents have become an essential part of developers’ daily workflows, enabling code generation, modification, and review directly within the terminal. However, relying on default model services can present several challenges: regional availability restrictions, account instability, complex payment requirements, and high costs associated with token-based billing for heavy usage.
A practical solution to these pain points is to retain the CLI agent’s interaction format and engineering capabilities while replacing its underlying model with an alternative LLM service accessible via standard APIs. Some alternative models offer free tiers, reducing initial usage costs to near zero. This article provides a generalized, step-by-step guide to implementing this solution.
2. Technical Principles: Why Direct Model Replacement Works
CLI programming tools typically adopt a decoupled architecture separating the client from the model service. The client handles command parsing, context orchestration, and local file operations, while model inference is executed via remote HTTP calls. Authentication and service endpoints are usually controlled by two environment variables:
- Authentication Token Variable: Stores the API Key for identity verification.
- Service URL Variable: Points to the endpoint compatible with the API protocol expected by the tool.
As long as the model provider offers an interface endpoint compatible with this protocol format, the client requires no code modifications. The model can be swapped simply through configuration. This is the core premise of this solution and the two most critical fields in the setup.
3. Client Installation
Choose one of the following methods based on your operating system:
Method 1: Using a Package Manager
For systems with a package manager like Homebrew (macOS), execute:
brew install --cask claude-code
Method 2: Official Installation Script
For macOS and Linux users:
curl -fsSL https://claude.ai/install.sh | bash
Windows users can execute the following in PowerShell:
curl -fsSL https://claude.ai/install.cmd -o install.cmd && install.cmd && del install.cmd
Tip: After installation, run the version query command in your terminal to confirm a successful setup. This helps distinguish between “not installed” and “configuration error” issues during later troubleshooting.
4. Obtaining Model Service Credentials
- Register an Account: Sign up on your chosen alternative model service platform (for example, a provider offering GLM models). Opt for a service that offers coding-optimized models and potentially free tiers. Navigate to the console after registration.
- Create an API Key: In the console’s key management page, create a new key. Copy and save it immediately. Treat this key as your sole credential: store it securely, avoid committing it to code repositories, and never share it.
5. Core Configuration Steps
5.1 Configuration File Content
Write the following content into the configuration file (using JSON format as an example):
{"env": {"ANTHROPIC_AUTH_TOKEN": "<Replace_with_your_API_Key>","ANTHROPIC_BASE_URL": "https://open.bigmodel.cn/api/anthropic","ANTHROPIC_DEFAULT_HAIKU_MODEL": "GLM-4.7-air","ANTHROPIC_DEFAULT_SONNET_MODEL": "GLM-4.7","ANTHROPIC_DEFAULT_OPUS_MODEL": "GLM-4.7"},"hasCompletedOnboarding": true}
Field descriptions:
ANTHROPIC_AUTH_TOKEN: Enter the key obtained in the previous step.ANTHROPIC_BASE_URL: The compatible endpoint address provided by the model service. The protocol format must match what the client expects.- The three model variables correspond to lightweight, standard, and high-performance tasks within the client. You can map lightweight tasks to smaller, faster models to conserve quota, and complex tasks to more capable models.
5.2 Configuration File Path
- macOS / Linux:
~/.claude/settings.json - Windows:
C:\Users\YourUsername\.claude\settings.json
If the directory or file does not exist, create it manually. Ensure the JSON format is valid: use double quotes for key-value pairs and avoid trailing commas, as these are the most common low-level causes of configuration failures.
6. Verification and Troubleshooting
6.1 Verification Process
- Open your terminal and run
claudeto launch the tool. - If an authorization prompt appears on first launch, accept it.
- Run
/statusin the interactive interface to check the current status. - If the model displayed on the status page matches your configured alternative model, the integration is successful.
6.2 Common Issues and Troubleshooting
- Missing Authentication Info: Verify that the configuration file path is correct, the JSON is valid, and the key is pasted completely (no leading or trailing spaces/newlines). Most of these issues stem from incorrect keys or file locations.
- Request Timeout or Connection Failure: Ensure the
ANTHROPIC_BASE_URLaddress is spelled correctly and that your local network can access the endpoint. - Model Name Error: The model name must exactly match the current version identifier provided by the model platform. Check the model list page to confirm.
- Configuration Changes Not Taking Effect: You must exit and restart the client. Some environment variables are only read during startup.
7. Practical Recommendations
- Tiered Model Configuration: Use lightweight models for routine small tasks and switch to larger models for refactoring or complex debugging to significantly reduce quota consumption.
- Key Security: Regularly rotate keys in the console. For multi-device usage, create independent keys for each device for easier revocation and management.
- Context Management: Token consumption in programming tools is closely tied to the conversation context length. Starting new sessions promptly and avoiding irrelevant history is an effective way to control costs.
- Gradual Workflow Migration: Test the setup in non-critical projects first. Once you are familiar with the tool’s behavior and the model’s capability boundaries, roll it out to your formal development workflow.
8. Summary
By utilizing standard protocol-compatible endpoints and two core environment variables, you can connect CLI coding agents to alternative LLMs without modifying any code. This solution effectively bypasses high costs and vendor restrictions, especially when leveraging free tiers. The overall process can be summarized as: Install the client → Register and obtain an API Key → Write to the configuration file → Launch and verify. Once mastered, developers can flexibly combine tools and models based on their needs, building a highly cost-effective AI-assisted programming workflow.
