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:
- The user proposes a development requirement in Claude Code
- Claude Code triggers an API call
- CC Switch intercepts the request in real-time
- CC Switch converts the request format to an Ollama-compatible format
- The request is sent to the local Ollama service for inference
- Ollama returns the result, and CC Switch formats it and passes it back to Claude Code
- Claude Code executes the next engineering action
2. Preparation
Before starting, ensure you have the following conditions met:
- 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). - Claude Code installed: You need a working Claude Code command-line tool.
- 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:
- Provider Name: Customizable, e.g.,
ollama-local - Request Address (Base URL): Enter
http://localhost:11434/v1. This is the default API address for the Ollama service. If your Ollama is not on the local machine, replacelocalhostwith thecorresponding IP address - API Key: Since the Ollama local service usually has no authentication, this field can be filled in arbitrarily, e.g.,
ollamaornot-used - API Format: Some versions of ccswitch may require you to manually specify the API format as
OpenAi Chat Completions, because Ollama is compatible with OpenAI's interface format - Authentication Field: Select
ANTHROPIC_API_KEY
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.
- Open Settings: In the CC Switch main interface, enter the settings page
- Find the Route Switch: In settings, go to Advanced -> Local Route
- Turn on the main route switch to start the local routing service
- 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
- Sonnet Sonnet 5 · Efficient for routine tasks · ! 3/15 per Mtok
- Sonnet 5 (1M context) Sonnet 5 for long sessions · ! 3/15 per Mtok
- 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.
- Confirm whether the authentication field is set to
ANTHROPIC_API_KEY, not the default ANTHROPIC_AUTH_TOKEN (default) - Check if the model mapping is configured and the display name has been modified
- 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.
- Method 1: Set proxy bypass in the VPN software (recommended).
- Find the "Proxy Bypass" or "Bypass" setting.
- Add
127.0.0.1andlocalhostto the bypass list.
- Method 2: Turn off the VPN's system proxy
- Find and click the "Disable System Proxy" or "Clear System Proxy" button.
- Method 3: Check CCSwitch's "Local Route" feature
- In CCSwitch's settings, find the "Route" or "Local Route" option and ensure it is enabled.
- Finally, restart all services: Restart in order Ollama -> CCSwitch -> Codex/Claude to ensure all configurations take effect.
5. Notes
- Context Length: Ollama's default context length may be low and insufficient for Claude Code's complex tasks. You may need to adjust the Ollama model's
num_ctxparameter (e.g., set in the Modelfile) to increase the context window.- Refer to Chapter 3, Section 3 of my previous article https://juejin.cn/post/7634768133992759296
- Tool Calling Support: Not all Ollama models support function calling (Tool Calling). If using a model without this feature, some of Claude Code's automation capabilities may be limited.
- Command Line Alternative: Besides the graphical interface, some ccswitch variants also provide command-line tools. For example, you can use the command
ccswitch-ollama --model <model name>to quickly switch.