跪拜 Guibai
← Back to the summary

How to Swap Claude Code's Brain for a Local Ollama Model

1. Introduction

Use ccswitch to connect Ollama local models to Claude Code. The core idea is to let ccswitch act as a "translator" , forwarding Claude Code's requests to your locally running Ollama service. This article will detail how to use CC Switch to connect Ollama local large models to Claude Code, achieving a zero-cost, high-privacy, always-available local AI programming experience, guiding you through the operation without (saving) pitfalls (money)!

This solution is a typical "Agent - Proxy - Core" three-layer architecture:

Component Role Responsibility
Claude Code Agent Framework Project awareness, file operations, command execution, task scheduling
CC Switch API Proxy/Adapter Layer Intercept API requests, convert protocol formats, adapt local calls
Ollama Inference Engine Local model execution, code generation, logical reasoning

The workflow is as follows:

  1. The user proposes a development requirement in Claude Code
  2. Claude Code triggers an API call
  3. CC Switch intercepts the request in real-time
  4. CC Switch converts the request format to an Ollama-compatible format
  5. The request is sent to the local Ollama service for inference
  6. Ollama returns the result, and CC Switch formats it and passes it back to Claude Code
  7. Claude Code executes the next engineering action

2. Preparation

Before starting, ensure you have the following conditions met:

  1. Ollama installed and running: Ensure Ollama is installed and started locally, and you have downloaded the required model via ollama pull <model name> (e.g., qwen3.5:35b).
  2. Claude Code installed: You need a working Claude Code command-line tool.
  3. ccswitch installed: Download and install ccswitch from official channels according to your operating system.

🎯 For Ollama deployment and installation, refer to the author's previous article: Token Freedom - Ollama Local Large Model Deployment Ultra-Detailed Operation Guide

🎯 For Claude Code and ccswitch installation, refer to the author's previous article: # Claude Code Domestic Barrier-Free Access to DeepSeek Usage Guide

3. Configuring the Local Model

1. Start ccswitch and Add a Provider

Open the ccswitch graphical interface and select Claude in the top application bar. Then click the "Add New Provider" button (plus sign).

2. Configure Ollama Connection Information

In the add provider interface, select or manually configure Ollama as the model source. The key configuration information is as follows:

3. Sync Model List

After configuring the basic information, click the "Sync Models" or "Get Model List" button. ccswitch will automatically fetch the list of downloaded models from your Ollama service.

4. Enable and Switch Models

After a successful sync, select the Ollama provider and specific model you just added from ccswitch's model list, then click enable.

5. Verify Usage

After completing the above configuration, open the Claude Code terminal and enter the /model command. You should see and be able to select the Ollama local model configured via ccswitch.

At this point, all your requests in Claude Code will be processed by your local Ollama model.

Connectivity test:

4. Troubleshooting Experience

Problem 1: This provider uses the OpenAI Chat interface format and requires a routing service to function properly. Please start the route first.

This message means the API format you selected (OpenAI Chat Completions) does not match the protocol natively used by Claude Code, requiring CC Switch to start a "translator" role to relay communication.

  1. Open Settings: In the CC Switch main interface, enter the settings page
  2. Find the Route Switch: In settings, go to Advanced -> Local Route
  3. Turn on the main route switch to start the local routing service
  4. Then, find Claude in the list below and turn on its switch

Problem 2: When switching /model on the Claude page, the local model name is not displayed, showing something like:

Select model
Switch between Claude models. Your pick becomes the default for new sessions. For other/previous model names, specify with
--model.

❯ 1. Default (recommended) ✔ Use the default model (currently Opus 5 (1M context)) · ! 5/25 per Mtok
2. Opus (1M context) Opus 5 with 1M context · Best for everyday, complex tasks · ! 5/25 per Mtok

  1. Sonnet Sonnet 5 · Efficient for routine tasks · ! 3/15 per Mtok
  2. Sonnet 5 (1M context) Sonnet 5 for long sessions · ! 3/15 per Mtok
  3. Haiku Haiku 4.5 · Fastest for quick answers · ! 1/5 per Mtok

What you see here is still Anthropic's official cloud model list (Opus, Sonnet, Haiku, etc.). The Ollama local model you configured via CC Switch is not appearing in this list at all.

  1. Confirm whether the authentication field is set to ANTHROPIC_API_KEY, not the default ANTHROPIC_AUTH_TOKEN (default)
  2. Check if the model mapping is configured and the display name has been modified
  3. If you have a proxy enabled, check if you have disabled the system proxy setting, or completely exit the VPN (ensure the software is fully closed)

Problem 3: Proxy software prompts:

✻ 502 Request forwarding failed: Upstream connection failed: error sending request · Retrying in 2s · attempt 4/10

A 502 error after closing the VPN is most likely because the VPN software's system proxy settings were not automatically restored, causing local requests (127.0.0.1) to be incorrectly forwarded to the external network.

  1. Method 1: Set proxy bypass in the VPN software (recommended).
    1. Find the "Proxy Bypass" or "Bypass" setting.
    2. Add 127.0.0.1 and localhost to the bypass list.
  2. Method 2: Turn off the VPN's system proxy
    1. Find and click the "Disable System Proxy" or "Clear System Proxy" button.
  3. Method 3: Check CCSwitch's "Local Route" feature
    1. In CCSwitch's settings, find the "Route" or "Local Route" option and ensure it is enabled.
  4. Finally, restart all services: Restart in order Ollama -> CCSwitch -> Codex/Claude to ensure all configurations take effect.

5. Notes