Connecting Coding Agents to Chinese LLMs: A Practical Guide

    This guide explains how to connect intelligent coding tools to Chinese large language models using Anthropic Messages-style compatible endpoints. It covers the required environment variables, a three-step setup process, routing multiple backends through a local proxy, building a small in-house evaluation set instead of relying on public leaderboards, common troubleshooting, and security and cost considerations.

    1. Why Connect Coding Agents to Chinese LLMs

    Intelligent coding tools — the command-line utilities that provide code completion and conversational coding assistance — have become part of the daily development workflow over the past couple of years. They typically offer three capabilities: reading repository context, invoking tools to run commands on demand, and completing refactoring and debugging tasks through multi-turn conversations. Such tools are often bound to a single overseas model service by default, which creates two practical problems: billing and network access barriers, and queuing or rate limiting when quotas tighten during peak hours.

    As Chinese LLMs continue to improve in code generation, long-context understanding, and tool calling, connecting them to the coding tools you already run locally has become a cost-effective path. This article does not rank individual vendors. Instead, it discusses a general approach: using an Anthropic Messages-style compatible interface so that a client originally designed for a single backend can point at any compatible service endpoint.

    2. How It Works: The Compatibility Layer Is the Key

    Most coding agents let you override the default service address and credentials through environment variables. The common ones are:

    1. export ANTHROPIC_BASE_URL="https://<compatible-endpoint>/anthropic"
    2. export ANTHROPIC_AUTH_TOKEN="<your-api-key>"
    3. export ANTHROPIC_MODEL="<main-model-id>"
    4. export ANTHROPIC_DEFAULT_HAIKU_MODEL="<lightweight-model-id>"
    5. export ANTHROPIC_DEFAULT_SONNET_MODEL="<balanced-model-id>"
    6. export ANTHROPIC_DEFAULT_OPUS_MODEL="<strong-model-id>"

    The three DEFAULT variables map to the client’s internal lightweight, balanced, and strong model tiers. The tool calls different tiers when generating conversation titles, compacting context, and running the main task, so pointing all three at the same model is the simplest starting configuration. If the provider offers a cheaper small model, you can point the HAIKU tier at it separately to cut costs further.

    The core semantics a compatibility layer must implement include: the multi-turn structure of the message array, the system prompt as a separate field, tool definitions and tool result回填, and the event chunking of streaming responses. As long as the server aligns on these semantics, the client works without modification.

    3. Setup in Three Steps

    Step 1: Get an API key. Create a key in the console of the target model provider, and confirm that your account has call permissions and quota for the corresponding model.

    Step 2: Write the configuration file. To avoid exporting the variables every time you open a terminal, append the configuration to your shell profile:

    1. # ~/.zshrc or ~/.bashrc
    2. export ANTHROPIC_BASE_URL="https://<compatible-endpoint>/anthropic"
    3. export ANTHROPIC_AUTH_TOKEN="<your-api-key>"
    4. export ANTHROPIC_MODEL="<model-id>"
    5. export ANTHROPIC_DEFAULT_HAIKU_MODEL="<model-id>"
    6. export ANTHROPIC_DEFAULT_SONNET_MODEL="<model-id>"
    7. export ANTHROPIC_DEFAULT_OPUS_MODEL="<model-id>"

    Run source ~/.zshrc to apply it. It is advisable to put the key in a separate file with 600 permissions and source that file, so the key is not accidentally committed to a repository.

    Step 3: Launch and verify. After starting the tool, try a minimal task first — for example, “list the current directory structure and explain what each file does” — and check whether it reads context correctly and returns a structured result.

    4. Switching Between Models Frequently: The Routing Approach

    If you need to switch back and forth between multiple backends, editing environment variables repeatedly is a poor experience. A common industry practice is to run a local routing process: the client always points at a local address, and the routing layer forwards requests to different upstreams according to rules, so switching only requires changing the routing configuration or running a single command.

    1. # Illustrative: abstract command for switching upstreams
    2. router use <profile-a>
    3. router use <profile-b>
    4. router use <official>

    The value of a routing layer goes beyond convenient switching. It can also split traffic by task type (long-context tasks go to models with larger windows), retry and fall back automatically on failures, log calls and token consumption in one place, and perform intermediate processing of prompts and tool definitions locally. In practice, it is recommended to keep the routing configuration under version control while injecting keys through the environment.

    5. Evaluating Results: Don’t Rely on Leaderboards Alone

    Public leaderboards and usage rankings can serve as references, but they usually cover only general question answering and a few code benchmarks, which differ substantially from your real repository. A more reliable approach is to build your own small evaluation set:

    • Pick 20 to 30 real historical issues or commits, covering four task types: bug fixes, small refactors, writing tests, and reading unfamiliar code;
    • Fix the prompts and tool permissions, and record the first-pass success rate, the number of rounds needing manual correction, average time, and token consumption;
    • Run each candidate model three times and take the median, so a single fluctuation does not mislead the conclusion;
    • Watch for performance degradation under long context and the rate of malformed tool calls — these two factors most affect the usability of a coding agent.

    6. Troubleshooting Common Issues

    A 401 usually means the key is not in effect or an environment variable is overridden by another configuration; check with env | grep ANTHROPIC. A 404 typically means the endpoint path is missing or has an extra /anthropic suffix. Garbled or interrupted streaming output calls for checking whether a proxy that does not support chunked forwarding is in the path. Frequent tool call failures often indicate that the model follows the tool schema poorly; you can simplify tool descriptions or reduce the number of parallel tools. If an overly long context raises an error, enable history compaction on the client side or narrow the scope of a single task.

    7. Security and Cost Notes

    Do not hardcode keys into scripts or commit them to repositories; for team scenarios, prefer the sub-accounts and quota isolation offered by the provider; redact sensitive fields in logs before retaining them. On cost, beyond unit price you also need to account for the hidden consumption from retries and failed tasks — a cheap model with a low first-pass success rate may not have a lower total cost. Platforms such as Baidu AI Cloud generally provide model services along with common capabilities like logging and monitoring, which can be combined with quota alerts for cost management.

    8. Summary

    Connecting coding agents to Chinese LLMs comes down to three things: protocol compatibility, configuration management, and routing. First get the minimal loop working with environment variables, then introduce a routing layer to handle multi-model switching, and finally replace leaderboards with a small in-house evaluation set for decisions — that is how you find the right balance between efficiency and cost.