跪拜 Guibai
← All articles
Claude · AI Programming · Ollama

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

By 岛雨AI ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Running Claude Code against a local model eliminates per-token billing and keeps source code off cloud servers, but the integration is fragile: a wrong auth field or leftover VPN proxy setting silently breaks model discovery, and missing tool-calling support degrades the agent's automation without an obvious error.

Summary

A three-layer architecture places CC Switch between Claude Code and a local Ollama instance, intercepting API calls and converting them into OpenAI-compatible chat completions. The setup requires pointing CC Switch at Ollama's default endpoint (`http://localhost:11434/v1`), syncing the model list, and enabling a local routing service to bridge the protocol gap. Once configured, the `/model` command inside Claude Code surfaces the local models alongside the official ones.

Common failures trace back to two misconfigurations: the authentication field must be set to `ANTHROPIC_API_KEY` rather than the default token, and VPN system-proxy settings often hijack localhost traffic, producing 502 errors even after the VPN app is closed. The fix is either adding `127.0.0.1` and `localhost` to the VPN's bypass list or explicitly clearing the system proxy.

Local models bring zero API cost and full data privacy, but two constraints remain. Ollama's default context window is small and needs manual adjustment via the `num_ctx` parameter for longer coding sessions, and models that lack function-calling support will silently break Claude Code's tool-use features.

Takeaways
CC Switch sits between Claude Code and Ollama, translating Anthropic-format requests into OpenAI Chat Completions that Ollama understands.
Point CC Switch at `http://localhost:11434/v1`, set the API format to OpenAI Chat Completions, and use `ANTHROPIC_API_KEY` as the auth field — a dummy API key works since Ollama has no auth.
Enable CC Switch's local routing service under Advanced settings, then toggle the Claude-specific route switch; without it, the protocol mismatch blocks all requests.
After syncing the model list, local models appear inside Claude Code via the `/model` command, selectable alongside official Anthropic models.
VPN software often leaves system proxy settings intact after closing, redirecting localhost traffic externally and causing 502 errors; add `127.0.0.1` and `localhost` to the bypass list or manually clear the system proxy.
Ollama's default context length is too short for complex Claude Code sessions; increase it by setting `num_ctx` in the model's Modelfile.
Models without function-calling support will silently limit Claude Code's ability to execute tool-use workflows.
Conclusions

The integration's fragility comes from a mismatch in defaults: Claude Code expects Anthropic's auth scheme, but Ollama speaks OpenAI's format with no auth, so every field must be explicitly overridden — a single wrong dropdown breaks the whole chain.

VPN system-proxy pollution is a recurring footgun in local LLM setups because the failure mode (502 on localhost) looks like a server crash, not a networking misconfiguration, leading developers to restart services instead of checking proxy settings.

Tool-calling support is the hidden gatekeeper for local models in agentic coding; a model that generates good code but can't participate in Claude Code's tool loop will appear to work while silently degrading the agent's capabilities.

Concepts & terms
CC Switch
A desktop proxy tool that intercepts API calls from Claude Code and translates them into formats compatible with alternative model providers, including local Ollama instances.
Ollama
A tool for running large language models locally, exposing an OpenAI-compatible API on `localhost:11434` that supports chat completions and model management.
num_ctx
An Ollama model parameter that sets the maximum context window size in tokens; the default is often too small for multi-turn agentic coding sessions and must be increased in the Modelfile.
Function Calling / Tool Calling
An API capability that lets a model request execution of external tools or functions; Claude Code relies on it for file operations and command execution, and local models without it lose automation features.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗