Run Claude Code Locally with Ollama & LM Studio (Zero API Cost)
Point Anthropic's Claude Code CLI straight at local Ollama (v0.14.0+) or LM Studio (v0.4.1+) using native /v1/messages endpoints—zero proxies needed.
Both Ollama (v0.14.0+) and LM Studio (v0.4.1+) now expose native Anthropic-compatible /v1/messages endpoints. That means Claude Code works completely offline against local open coding models like Qwen3.8-27B, qwen3-coder, or Gemma 4—with zero proxy scripts and zero per-token bills once you adjust the context window to 32K–64K+.
How It Fits Together
1. Update Local Runner
Upgrade Ollama to v0.14.0+ or LM Studio to v0.4.1+ for native /v1/messages support.
2. Load 64K Context Model
Pull Qwen3.8-27B or qwen3-coder and set context length to at least 32K–64K.
3. Point & Launch CLI
Set ANTHROPIC_BASE_URL, dummy ANTHROPIC_AUTH_TOKEN, unset ANTHROPIC_API_KEY, and launch claude.
Exact Commands for Ollama and LM Studio
Notice two tiny details that trip people up: pass any non-empty dummy string for ANTHROPIC_AUTH_TOKEN, and explicitly unset ANTHROPIC_API_KEY in your shell so Claude Code never tries to validate against Anthropic's cloud servers.
# --- Option 1: Ollama (v0.14.0+) ---
ollama pull qwen3-coder
export ANTHROPIC_BASE_URL=http://localhost:11434
export ANTHROPIC_AUTH_TOKEN=ollama
unset ANTHROPIC_API_KEY
claude --model qwen3-coder
# Tip: Ollama also includes shortcut: ollama launch claude
# --- Option 2: LM Studio (v0.4.1+, set 64K context!) ---
lms load qwen/qwen3-coder-30b --context-length 65536
lms server start --port 1234
export ANTHROPIC_BASE_URL=http://localhost:1234
export ANTHROPIC_AUTH_TOKEN=lmstudio
unset ANTHROPIC_API_KEY
claude --model qwen/qwen3-coder-30b Why Context Length Matters So Much Locally
Claude Code carries multi-file diffs, tool schemas, and conversation logs inside every turn. Leaving Ollama or LM Studio on a tiny 4K–8K default causes instant truncation or broken tool calls.
| Hardware Setup | Context Window Target | Recommended Local Model | Best Use |
|---|---|---|---|
| 16GB Mac / 12GB GPU | 32K Context (minimum) | Gemma 4 12B / Qwen3.8 smaller quants | Scoped single-module fixes & confidential scripts |
| 24GB GPU / 32GB–64GB Mac | 64K Context (sweet spot) | Qwen3.8-27B / qwen3-coder-30b | Daily private repo edits with strong tool-calling reliability |
| Hybrid Cloud + Local OpenCode | 128K–1M Context | Switch freely between local Ollama & Claude / GLM-5.3 | Zero-cost scouting locally, frontier accuracy on commits |
Before You Start
- Always unset ANTHROPIC_API_KEY—leaving a cloud key exported alongside ANTHROPIC_BASE_URL triggers auth conflicts or unintended cloud fallback.
- Never skip setting context length to 32K–64K+ when loading models in LM Studio or custom Ollama Modelfiles; small 4K defaults break Claude Code tool schemas immediately.
- Be realistic about speed and accuracy on tiny 7B models: if a complex refactor stalls locally, switch back to OpenCode or pay-per-token DeepSeek / Claude for that task.