How to Connect AI Coding Tools to Domestic LLMs: A Practical Guide
This guide explains how to route terminal-based AI coding tools to domestic large language models by configuring three core environment variables. It covers multi-model switching strategies, rigorous evaluation methods, observability for stability and cost, and common pitfalls to avoid. Readers will learn how to build a stable, low-cost, and maintainable AI-assisted development environment.
1. Background: Why Connect AI Coding Tools to Domestic Models
Over the past two years, terminal-based AI coding tools have become essential assistants for developers’ daily work. These tools typically follow Anthropic Messages-style API conventions and use a set of environment variables to specify the service endpoint, authentication token, and model name. Default configurations usually point to overseas official models, and developers in China often encounter three types of friction in real usage:
- Account and compliance risks: Cross-border calls involve account risk controls, and occasional bans can directly interrupt workflows.
- Insufficient network stability: Long-context requests take a long time, and cross-border link jitter causes timeouts, wasting all the tokens already consumed.
- Unfriendly cost structure: Flagship models have high unit prices, while most daily coding tasks are of low-to-medium complexity. Using flagship models for them is a clear waste.
Meanwhile, domestic LLMs keep improving on tasks such as general code understanding, single-file generation, and refactoring suggestions, and most vendors already offer service endpoints compatible with mainstream API protocols. This means the switching cost is reduced to “changing a few environment variables.” It is worth emphasizing that models still differ in ultra-long-context reasoning and complex multi-file engineering changes. Selection should be based on small-scale testing aligned with your own task distribution, rather than leaderboard rankings.
2. How It Works: Three Environment Variables Control Everything
Model routing in AI coding tools is essentially controlled by three groups of variables:
ANTHROPIC_BASE_URL: The base URL where requests are sent, determining “where to connect.”ANTHROPIC_AUTH_TOKEN: The authentication credential, determining “what identity to use.”ANTHROPIC_MODELandANTHROPIC_DEFAULT_HAIKU_MODEL,ANTHROPIC_DEFAULT_SONNET_MODEL,ANTHROPIC_DEFAULT_OPUS_MODEL: Determine “which model to use.” The latter three correspond to the tool’s internal calls for lightweight, standard, and flagship capability tiers.
A key practical point: not all requests inside the tool go through the main model. Scenarios such as generating conversation titles, judging task complexity, and creating lightweight summaries often call lower-tier models. If you only set the main model and omit the lower-tier variables, you will see the strange phenomenon of “the main flow works but auxiliary features fail.” Therefore, it is recommended to point all three tiers to the same domestic model, or configure a smaller, faster model for the lower tiers to further reduce costs.
Example configuration (reload after writing to the shell configuration file):
export ANTHROPIC_BASE_URL="https://<a cloud vendor's compatible endpoint>"export ANTHROPIC_AUTH_TOKEN="<your API key>"export ANTHROPIC_MODEL="<model name>"export ANTHROPIC_DEFAULT_HAIKU_MODEL="<model name>"export ANTHROPIC_DEFAULT_SONNET_MODEL="<model name>"export ANTHROPIC_DEFAULT_OPUS_MODEL="<model name>"
Writing the configuration into ~/.zshrc or ~/.bashrc avoids re-exporting it every time you open a terminal. A more robust approach is to extract it into a separate script and source it on demand, avoiding global pollution of other projects.
3. Running Multiple Models in Parallel: From Manual Edits to Command-Level Switching
Environment variables are sufficient when using only one model. Once you need to switch frequently among domestic models, overseas official models, and local inference services, manual edits quickly become hard to maintain. There are three progressive engineering paths:
Path 1: Function encapsulation. Define a function in the shell configuration to reduce switching to a single command.
use_model() {export ANTHROPIC_BASE_URL="$1"export ANTHROPIC_AUTH_TOKEN="$2"export ANTHROPIC_MODEL="$3"export ANTHROPIC_DEFAULT_HAIKU_MODEL="$3"export ANTHROPIC_DEFAULT_SONNET_MODEL="$3"export ANTHROPIC_DEFAULT_OPUS_MODEL="$3"}
Path 2: Configuration files + symbolic links. Maintain multiple configuration fragments and switch the active configuration by changing a symbolic link. This suits scenarios with many configuration items.
Path 3: Routing proxy. Run a compatibility layer service locally that forwards requests to different backends according to rules. The tool side only points to a fixed local address, and switching logic is consolidated in the proxy layer. It can also implement failure retries, extended timeouts, request logging, and usage statistics. This is the most recommended form for team collaboration.
4. How to Make Model Comparisons Rigorous
Claims online that “model X crushes model Y” are mostly single-point experiences with limited reference value. A reproducible evaluation should include:
- Task layering: Break daily tasks into five categories—completion, single-file generation, cross-file refactoring, debugging, and test writing—and measure each separately.
- Metric combinations: First-pass rate, percentage of lines manually modified, average time to first byte, end-to-end latency, and token cost per task.
- Context control: Fix prompts and repository slice scope to avoid unequal inputs across rounds.
- Repeated sampling: Run the same task multiple times and observe output variance, rather than drawing conclusions from a single run.
Conclusions usually show layered characteristics: domestic models have a clear cost-performance advantage on lightweight tasks; architecture-level changes with ultra-long context still rely more on flagship models. A reasonable strategy is layered routing, not a global either-or choice.
5. Observability for Stability and Cost
After connecting to third-party endpoints, observability becomes a must-have. It is recommended to at least log request timestamps, model tier, input and output token counts, status codes, and retry counts, and to set alerts for timeout rates and rate-limit hits. If you use general cloud capabilities, you can use log services for retrieval and monitoring alerts for threshold reminders, and aggregate costs by project tags. For context management, promptly clearing irrelevant history and changing large-file reads to on-demand retrieval often reduces costs more significantly than switching models.
6. Common Pitfalls Checklist
- Only changing the main model variable, causing auxiliary features to fail due to missing lower-tier variables.
- Adding an extra path at the end of the endpoint URL, resulting in 404.
- Committing the key into a version repository; use environment variables or a key management service instead.
- Ignoring variable name changes caused by tool version upgrades.
- Not setting reasonable timeouts and retries for long tasks, so a single jitter invalidates the entire round.
7. Conclusion
Localizing the backend model of AI coding tools is essentially an engineering effort of “compatibility layer + routing strategy,” not a simple name replacement. First clarify the environment variables and the three-tier model mapping, then introduce command-level switching or a local routing proxy, and finally drive selection with layered evaluation and observability metrics. This yields a stable, low-cost, and continuously evolvable development assistant environment.
