How to Set Up a Local AI Coding Assistant from Scratch and Switch Between Models Flexibly

    This guide walks you through building a local command-line AI coding assistant environment from zero: preparing the Node.js and Git toolchain, installing CLI coding tools via npm, connecting to model services through Base URL and API key configuration, and switching between multiple models using shell functions or GUI configuration managers. It also covers common troubleshooting tips so you can keep model selection fully under your own control.

    How to Set Up a Local AI Coding Assistant from Scratch and Switch Between Models Flexibly

    1. Why You Need a Local Command-Line AI Coding Assistant

    Over the past two years, AI-assisted programming has evolved from a novelty tool into everyday infrastructure for developers. Compared with web-based conversational interfaces, command-line AI coding tools offer several clear advantages:

    1. Direct awareness of code context: The tool runs inside your current project directory, reads the file structure, and analyzes dependencies, so its suggestions reflect your actual codebase rather than generic example code.
    2. Seamless integration with terminal workflows: You can ask questions, generate code, explain refactors, and draft commit messages without leaving your editor or terminal, which significantly reduces context-switching costs.
    3. Scriptable and integrable: CLI tools naturally fit into CI pipelines and code review processes, making it easier to standardize team practices.

    At the same time, the diversity of model services introduces a new challenge: different models vary in code generation, long-context understanding, reasoning, response speed, and cost. Developers often want to switch models based on the task at hand. This article walks through the full workflow from scratch: environment preparation, tool installation, model connection, and model switching.

    2. Preparation: Installing the Runtime and Version Control Tools

    Most command-line AI coding tools are distributed through the JavaScript runtime ecosystem, so you need two basic dependencies before installation: the Node.js runtime and Git. If you have already installed them, verify the versions and skip this section.

    2.1 Windows

    Windows users are recommended to use the built-in winget package manager. Run the following in PowerShell as an administrator:

    1. # Install the Node.js runtime
    2. winget install OpenJS.NodeJS
    3. # Install Git
    4. winget install Git.Git

    2.2 macOS

    macOS users are recommended to use Homebrew as the package manager:

    1. # Install the Node.js runtime
    2. brew install node
    3. # Install Git
    4. brew install git

    2.3 Verifying the Installation

    Regardless of platform, verify the versions after installation to confirm the toolchain is ready:

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

    If all three commands print a version number, your base environment is ready. A few important notes: on Windows, run the terminal as administrator when installing package managers or global npm packages to avoid permission failures; on macOS, if you previously relied on an older system-provided Node.js, consider managing versions with a version manager such as nvm, which makes future upgrades and rollbacks much easier.

    3. Installing a Command-Line AI Coding Tool

    Once the base environment is ready, you can install a mainstream command-line AI coding tool. These tools are typically distributed via global npm installation, and the command pattern is highly consistent:

    1. # Global installation (works on Linux / macOS / Windows)
    2. npm install -g <tool-package-name>
    3. # Verify the installation
    4. <tool-command> --version

    Practical tips during installation:

    • Use your code editor’s integrated terminal: For example, open a new terminal inside your usual editor and run the installation there. Once installed, the tool can be launched directly within the editor for the best “code and ask side by side” experience.
    • Global installation directory permissions: On Linux and macOS, if you hit permission errors, avoid reaching for sudo as a blunt fix. Instead, configure npm’s global installation prefix to a directory under your user account, which eliminates permission issues at the root.
    • Network issues: If access to the official npm registry is slow in your region, switching to a mirror registry to speed up downloads is a common industry practice.

    After installation, simply type the tool’s command in any project directory to start an interactive session. On first launch, the tool usually guides you through authentication or key configuration.

    4. Connecting to Model Services: Keys and Environment Variables

    Connecting a command-line AI coding tool to a model service fundamentally depends on two pieces of configuration: the endpoint (Base URL) and the access key (API Key). Once you understand this, you can apply the same approach to any model service compatible with standard API protocols.

    4.1 Configuring via Environment Variables

    This is the most flexible approach. Using common environment variable names as an example (refer to your tool’s official documentation for the exact variable names):

    1. # Linux / macOS (persist by adding to ~/.bashrc or ~/.zshrc)
    2. export ANTHROPIC_BASE_URL="https://your-model-service-endpoint"
    3. export ANTHROPIC_AUTH_TOKEN="your-api-key"
    4. export ANTHROPIC_MODEL="the-model-identifier-you-want"
    1. # Windows PowerShell (write to user environment variables)
    2. [Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "endpoint", "User")
    3. [Environment]::SetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", "api-key", "User")

    Reopen the terminal for the variables to take effect. The advantage of this approach is that you never modify the tool’s own files — switching models is just a matter of changing environment variables.

    4.2 Obtaining a Model Service Key

    The general process: register an account on the model service platform → create an API key in the console → confirm the service’s API protocol compatibility → record the endpoint and model identifier. Many model providers offer access methods compatible with mainstream API protocols, and you can choose based on pricing, context length, and coding capability. For example, the large-model capabilities offered by cloud platforms (such as the foundation model services available on Baidu AI Cloud’s Qianfan platform) also support standard API calls and can be evaluated as alternative connection targets.

    5. Two Approaches for Freely Switching Between Multiple Models

    When you have two or three model services at hand, manually editing environment variables becomes inefficient. The following two management approaches are recommended.

    5.1 Approach 1: Quick Switching with Shell Functions/Scripts

    Define a function for each model in your shell configuration file:

    1. # Switch to Model A
    2. use-model-a() {
    3. export ANTHROPIC_BASE_URL="endpoint-a"
    4. export ANTHROPIC_AUTH_TOKEN="key-a"
    5. export ANTHROPIC_MODEL="model-a-identifier"
    6. echo "Switched to Model A"
    7. }
    8. # Switch to Model B
    9. use-model-b() {
    10. export ANTHROPIC_BASE_URL="endpoint-b"
    11. export ANTHROPIC_AUTH_TOKEN="key-b"
    12. export ANTHROPIC_MODEL="model-b-identifier"
    13. echo "Switched to Model B"
    14. }

    Then simply type use-model-a or use-model-b in the terminal to switch instantly, and launch the coding tool for it to take effect. This approach has zero dependencies and is fully transparent, suiting developers who like to control every detail.

    5.2 Approach 2: GUI Configuration Management Tools

    If you prefer a graphical interface, you can also use “model configuration switcher” desktop tools that are common in the community. The general workflow for such tools:

    1. After installing and launching the app, if the system shows a security warning on first open (such as “unidentified developer”), choose “Open Anyway” in your system security settings;
    2. Click “Add Provider” and fill in the provider name, endpoint, API key, and model identifier;
    3. Enter each of your model services one by one;
    4. Click the target configuration on the main screen to switch to it with one click — the tool automatically rewrites the corresponding configuration file or environment variables.

    When choosing this type of tool: prefer open-source, auditable options; confirm that it only modifies local configuration files and does not upload your keys; and check for updates regularly to patch potential security issues.

    6. Troubleshooting Common Issues

    • Permission errors during installation: On Windows, retry in an administrator terminal; on macOS/Linux, adjust the npm global directory prefix.
    • Authentication failure on startup: Check whether the key was copied completely without extra spaces, and confirm the environment variables are effective in a new terminal (verify with echo $VARIABLE_NAME).
    • Abnormal responses or timeouts: Confirm the endpoint is filled in correctly and that the trailing path matches the provider’s documentation requirements; check whether local proxy settings are interfering with requests.
    • Configuration not taking effect after switching: GUI switching tools usually require restarting the coding tool’s process; with the shell approach, make sure the function was actually executed in the current session.

    7. Conclusion

    The core path to building a local AI coding environment can be summarized in four steps: prepare the Node.js and Git base environment → globally install a command-line coding tool → configure model service access via environment variables → enable one-click multi-model switching with scripts or GUI tools. Once you master the universal pattern of “endpoint + key + model identifier,” you can connect to and switch between any new model service within minutes, keeping model selection firmly in your own hands and building an efficient, controllable AI-assisted programming workflow.