Claude Code with Local Models (Ollama and LM Studio)

Published: 2/5/2025

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

The two key variables

VariablePurpose
ANTHROPIC_BASE_URLBase URL of your local Anthropic-compatible endpoint
ANTHROPIC_AUTH_TOKENPlaceholder 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

  1. Verify the server is running (ollama serve or LM Studio server on).
  2. Run claude --model <your-model>.
  3. 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