How to Set Up a CLI AI Coding Environment from Scratch: Connecting Domestic LLMs and Switching Between Models

    This guide walks you through building a complete command-line AI coding environment, from installing the required runtimes to connecting a domestic large language model through a compatible API endpoint. You will learn how to prepare Node.js and Git across Windows, macOS, and Linux, install the Claude Code CLI, replace the default endpoint with an OpenAI-compatible provider using environment variables or a settings file, and manage multiple model configurations with the cc-switch desktop tool. The article also covers connectivity testing, common error troubleshooting, and practical security and team collaboration tips such as protecting API keys and building cost-aware model usage habits.

    1. Why Set Up This Environment First

    Over the past two years, AI-assisted coding has shifted from a nice-to-have feature to part of the everyday infrastructure for many developers. Yet a common pattern remains: people get stuck at the environment setup stage long before they experience the value of AI coding, turned away by dependency conflicts, permission errors, or models that simply will not connect.

    From a technical standpoint, command-line (CLI) AI coding tools have gained traction for three core reasons:

    • They fit naturally into a terminal-based workflow: code search, file reading and writing, and command execution all happen in the terminal you already use, without constantly switching between windows.
    • Strong context awareness: the tool can read the project directory structure, configuration files, and version control state directly, so its responses reflect the real engineering context instead of relying on manually pasted snippets.
    • Extensibility and replaceability: the model invocation layer is abstracted away, so in theory the underlying model can be swapped as long as the target service is compatible with mainstream open API specifications.

    The third point is the key angle of this article. What many developers actually need is not “I must use a specific model” but “I want to switch models on demand and pick the best value for each scenario.” In recent years, domestic Chinese large models have kept improving in code generation, long-context understanding, and Chinese semantic comprehension, and they often come with more approachable access methods and cost structures. A more pragmatic approach, therefore, is to set up the toolchain first and then make the model layer pluggable.

    One reminder: this step cannot be skipped. If you genuinely want to learn AI-assisted development systematically, install the full environment in one go before writing any code; otherwise every debugging session later becomes another obstacle.


    2. Prerequisites: Runtime and Version Control

    Before installing any AI coding CLI tool, confirm two foundational dependencies: the Node.js runtime and the Git version control tool.

    The former is the host environment for most CLI tools (most are distributed as npm packages), while the latter underpins an AI tool’s ability to detect project changes, generate commit messages, and compare diffs.

    2.1 Check What Is Already Installed

    Open a terminal and run the following commands in order:

    1. node --version
    2. npm --version
    3. git --version

    If all three commands return version numbers (for example, v20.x, 10.x, 2.x), the environment is ready and you can skip to the next section.

    2.2 Installing on Windows

    Windows users are advised to use the system’s built-in package manager to avoid PATH configuration issues caused by manually downloaded installers. Open PowerShell as an administrator:

    1. # Install Node.js LTS
    2. winget install OpenJS.NodeJS
    3. # Install Git
    4. winget install Git.Git
    5. # Verify
    6. node --version
    7. npm --version
    8. git --version

    After installation, close and reopen the terminal so the environment variables take effect.

    2.3 Installing on macOS

    macOS users are advised to use the widely adopted Homebrew package manager (if it is not installed yet, initialize Homebrew following the official documentation first):

    1. # Install Node.js
    2. brew install node
    3. # Install Git
    4. brew install git
    5. # Verify
    6. node --version
    7. npm --version
    8. git --version

    2.4 A Few Notes on Versions

    • Choose the Node.js LTS release and avoid versions that are too old (below 18) or bleeding-edge non-stable builds, to reduce the chance of native module compilation failures.
    • If you need multiple Node.js versions coexisting on the same machine, introduce a Node version manager and pin the version used by each project through a configuration file, keeping every team member’s environment consistent.
    • Beyond installing Git itself, configure your user name and email, otherwise subsequent commits will fail:
    1. git config --global user.name "your_name"
    2. git config --global user.email "your_email@example.com"

    3. Installing the Command-Line AI Coding Tool

    Once the dependencies are confirmed, install the main tool. The mainstream official Anthropic CLI is distributed globally via npm:

    3.1 Linux / macOS

    1. # Global installation
    2. sudo npm install -g @anthropic-ai/claude-code
    3. # Verify
    4. claude --version

    3.2 Windows

    Open PowerShell or Command Prompt as an administrator:

    1. npm install -g @anthropic-ai/claude-code
    2. claude --version

    3.3 Launch and Initialization

    Simply run the following from the project root:

    1. claude

    On the first run, an interactive configuration flow starts, typically requiring a few steps:

    1. Choose theme and notification preferences (purely interactive settings, with no effect on functionality);
    2. Directory trust confirmation: the tool asks whether you trust the current working directory; confirm only for code repositories you trust;
    3. Model connection configuration: this is the most critical step, involving the API endpoint and key, which the next section covers in detail.

    If you encounter an EACCES permission error, do not resort to a blunt global install with sudo. A safer approach is to configure the npm global directory under your user space, or simply use an environment managed by a Node version manager.


    4. Core Step: Connecting a Domestic Model

    This is the core pain point the article aims to solve. The underlying tool calls overseas services by default, and we need to point it at a domestic model service that is compatible with open API specifications.

    4.1 Understanding the “Compatible API Specification” Prerequisite

    An industry de facto standard has gradually emerged: many model providers offer invocation methods compatible with mainstream open API specifications. This means that as long as the provider declares compatibility, switching is theoretically just a matter of replacing two parameters: the API Base URL and the API Key.

    The core variables to configure usually include:

    Variable (illustrative) Meaning
    ANTHROPIC_BASE_URL The invocation address of the target model service
    ANTHROPIC_AUTH_TOKEN The access credential issued by the target service

    Variable names may differ slightly between tools; refer to the corresponding tool’s configuration documentation.

    4.2 Option 1: Environment Variables (Suitable for Temporary Testing)

    1. # Temporarily effective on Linux / macOS
    2. export ANTHROPIC_BASE_URL="https://your-provider-endpoint/v1"
    3. export ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxx"

    This approach stops working once the terminal is restarted, making it suitable for quickly verifying whether a particular model is available.

    4.3 Option 2: Write to a Shell Configuration File (Suitable for Fixed Personal Use)

    1. # zsh users
    2. vim ~/.zshrc
    3. # bash users
    4. vim ~/.bashrc

    Append the following at the end of the file:

    1. export ANTHROPIC_BASE_URL="https://your-provider-endpoint/v1"
    2. export ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxx"
    3. source ~/.zshrc

    Create a tool configuration file in your home directory (for example, ~/.claude/settings.json) and organize different model services into multiple groups that you can enable as needed. The benefit is that the configuration is readable, versionable, and easy to share across a team.


    5. Multi-Model Switching: “Use Whichever Model You Want”

    Manually editing environment variables is highly inefficient. In real scenarios, a developer might use a long-context model in the morning to read a legacy project, a cost-effective model in the afternoon to refactor code in bulk, and a strong reasoning model in the evening for architecture design. This calls for a graphical model configuration switching tool.

    5.1 The Value of a Desktop Switching Tool

    Such tools are essentially a “configuration manager plus quick launcher”:

    • Manage multiple model provider configurations visually;
    • Switch with one click and automatically rewrite the underlying configuration files;
    • Include preset templates for common domestic models, ready to use once you fill in the key;
    • Support configuration import and export, making multi-device synchronization easy.

    5.2 Getting It on Windows

    Because it is released through the open-source community, the Windows version is usually provided as a portable archive that runs without installation:

    1. # 1. Download the portable build (check the releases page for the latest version)
    2. Invoke-WebRequest -Uri "https://github.com/farion1231/cc-switch/releases/download/v3.10.3/CC-Switch-v3.10.3-Windows-Portable.zip" -OutFile "$env:USERPROFILE\Downloads\CC-Switch-Portable.zip"
    3. # 2. Extract
    4. Expand-Archive -Path "$env:USERPROFILE\Downloads\CC-Switch-Portable.zip" -DestinationPath "$env:USERPROFILE\Desktop\CC-Switch"
    5. # 3. Launch
    6. Start-Process "$env:USERPROFILE\Desktop\CC-Switch\CC Switch.exe"

    Security note: when downloading binaries from an open-source repository, always verify the checksum information on the releases page and confirm the source is trustworthy. In enterprise environments, validate it in an internal sandbox before distributing it.

    5.3 Getting It on macOS

    On macOS, installing through Homebrew Cask is recommended for easier unified upgrades and uninstalls later:

    1. # Add the corresponding tap source
    2. brew tap farion1231/ccswitch
    3. # Install the desktop app
    4. brew install --cask cc-switch
    5. # Launch
    6. open -a "CC Switch"

    If a “cannot verify developer” message appears on first launch:

    1. Close the warning dialog first;
    2. Open System Settings → Privacy & Security;
    3. Click Open Anyway in the security notice area;
    4. It will launch normally afterward.

    6. Core Workflow of cc-switch

    6.1 Adding a Model Provider

    After launching the tool, click “Add Provider.” There are usually two modes:

    Mode A: Use a preset template

    The tool ships with presets for several providers, with fields for name, endpoint, and a key placeholder. You only need to:

    1. Select the corresponding provider from the dropdown list;
    2. Paste your API key;
    3. Save and click “Enable.”

    Mode B: Custom configuration

    If you use a self-hosted gateway or a privately deployed model service, choose “Custom” and fill in:

    1. Name: My internal model
    2. Endpoint: https://internal-gateway.example.com/v1
    3. Key: your-internal-token

    6.2 Verifying Connectivity

    After saving, run the following in the terminal:

    1. claude

    Then enter a simple instruction in the conversation (for example, “Introduce yourself in one sentence”). If content returns normally, the model connection is working.

    A 401 error usually means the key is wrong or has expired; a 404 usually means the endpoint path is incorrect (check whether the /v1 suffix is needed).

    6.3 Strategy for Using Multiple Models in Parallel

    It is recommended to build a “scenario → model” mapping into a personal usage guide:

    • Long-context reading / legacy code understanding → models with large context windows;
    • Daily completion and small-scale refactoring → fast, low-cost models;
    • Architecture design and complex bug diagnosis → models with strong reasoning ability;
    • Sensitive projects / internal code → privately deployed models.

    7. Troubleshooting Checklist

    Symptom Likely cause Fix direction
    command not found: claude npm global directory not in PATH Configure npm prefix or reopen the terminal
    EACCES permission error global directory owned by root Use a Node version manager
    Still using the old model after switching environment variables take priority over the config file Clear old exports in the shell
    Request timeout network or proxy issue Check proxy settings and firewall
    Truncated response content output length limit Adjust the maximum output tokens in the configuration

    8. Security and Team Collaboration Tips

    Finally, three easily overlooked but important practices:

    1. Never commit keys in plaintext

    An API key is equivalent to an account password. Always store it in a local configuration file or the system keychain, and exclude the relevant paths in .gitignore. If a leak occurs, revoke and rotate it immediately in the provider’s console.

    2. Maintain a unified configuration baseline for the team

    It is recommended that the team maintain a sanitized configuration template (without real keys). New members copy it to their own local environment when they join, fill in their personal keys, and avoid the maintenance disaster of “one setup per person.”

    3. Build cost awareness for model usage

    Most domestic model providers offer free quotas or tiered pricing, but it is still advisable to check usage in the console regularly. For high-frequency scenarios (such as automatically generating comments in CI), prefer lightweight models; reserve high-capability models for critical decision-making scenarios.


    9. Summary

    Looking back at the whole chain, there are only five key actions: install the runtime → install the main tool → configure the API endpoint → install the switch manager → pick a model by scenario.

    Once this configuration is complete, you have an AI coding workbench of your own, with a freely swappable underlying model. Whether it is a model upgrade, a pricing change, or adopting a privately deployed solution later, it is just a matter of changing one line of configuration.

    For developers, the real barrier has never been the tools themselves, but whether you are willing to spend ten minutes setting up the environment properly once. Cross that step, and the efficiency gains from AI coding can truly land in your daily development work.