Claude Code with Local Models (Ollama and LM Studio)
How to configure Claude Code to run against local models via Ollama or LM Studio.
If you want to run Claude Code with local models instead of Anthropic’s cloud, use an Anthropic-compatible API endpoint.
Both Ollama and LM Studio provide this compatibility. In Claude Code, you mainly set a base URL and an auth token.
Prerequisites
- Claude Code installed (
claudeavailable in terminal) - A local inference server running:
- Ollama (
http://localhost:11434) - or LM Studio server (
http://localhost:1234)
- Ollama (
- A local coding model available
The two key variables
| Variable | Purpose |
|---|---|
ANTHROPIC_BASE_URL | Base URL of your local Anthropic-compatible endpoint |
ANTHROPIC_AUTH_TOKEN | Placeholder token or real token (if auth is enabled) |
Option A: Claude Code with Ollama
1) Start Ollama and prepare a model
ollama serve
ollama pull qwen3-coder
2) Point Claude Code to Ollama
export ANTHROPIC_BASE_URL=http://localhost:11434
export ANTHROPIC_AUTH_TOKEN=ollama
3) Run Claude Code with a local model
claude --model qwen3-coder
Alternative example:
claude --model gpt-oss:20b
Option B: Claude Code with LM Studio
1) Start LM Studio server
Start the local server in LM Studio, or via CLI:
lms server start --port 1234
2) Point Claude Code to LM Studio
export ANTHROPIC_BASE_URL=http://localhost:1234
export ANTHROPIC_AUTH_TOKEN=lmstudio
If “Require Authentication” is enabled in LM Studio, use your real LM Studio API token instead of lmstudio.
3) Run Claude Code with a local model
claude --model openai/gpt-oss-20b
Note: In LM Studio, use the exact model identifier exposed by the local server.
Make it persistent
To avoid exporting variables every time, add them to ~/.zshrc or ~/.bashrc:
# Example: Ollama
export ANTHROPIC_BASE_URL=http://localhost:11434
export ANTHROPIC_AUTH_TOKEN=ollama
Then reload your shell:
source ~/.zshrc
Quick validation
- Verify the server is running (
ollama serveor LM Studio server on). - Run
claude --model <your-model>. - Try a short prompt, e.g. “Write a Python FizzBuzz function”.
If you get a response, Claude Code is correctly connected to your local model.
Troubleshooting
Connection refused: server not running or wrong port.401/403: with LM Studio auth enabled, set a valid token inANTHROPIC_AUTH_TOKEN.- Model not found: the value in
claude --model ...does not match a locally available identifier. - Weak coding performance: switch to a stronger model and increase context length (>= 25k is often useful for agent workflows).