How to Connect Claude Code to Local Ollama Models (2026)

You open Claude Code in your terminal to refactor a six-file TypeScript module, run two test suites, and fix a broken import. Thirty minutes later, you check your Anthropic console and realize a single afternoon of agent loops burned through $18 in token fees.

Until early 2026, pointing Anthropic’s terminal coding agent at a local model required fragile translation proxies that broke every time Claude Code fired a multi-step tool call. That changed when Ollama v0.14.0 added native Anthropic Messages API compatibility (POST /v1/messages). You can now connect a claude code ollama workflow directly to open-weight coding models on your own GPU (or switch dynamically between local checkpoints, llama-server, and LM Studio) with zero API cost and zero source code leaving your machine.

Below is the complete 2026 setup guide for running Claude Code with local Ollama models on macOS, Linux, and Windows PowerShell, including the exact settings.json flags that stop 90% KV-cache slowdowns, the 64K context window rule, and how to pick the right model for 8 GB, 16 GB, 24 GB, and 32 GB+ GPUs.

Table of Contents

How Claude Code connects to Ollama natively in 2026 (no proxy required)

In 2025, connecting Claude Code to a local model meant spinning up a LiteLLM proxy or a custom Node.js router to translate Anthropic’s /v1/messages payloads into OpenAI’s /v1/chat/completions format. Whenever Claude Code used tool_use blocks, thinking blocks, or streaming input_json_delta events, translation layers frequently dropped arguments and froze the terminal.

According to the official Ollama Anthropic compatibility documentation, Ollama v0.14.0 and newer expose a native /v1/messages endpoint on port 11434. It speaks the exact request and streaming event format that Claude Code expects:

  • What works out of the box: Multi-turn system prompts, streaming token deltas (content_block_delta), file reads and edits (tool_use and tool_result blocks), subagent spawning, base64 image vision inputs, and extended thinking controls (enabled or disabled).
  • What is not supported locally: According to Ollama’s API specification, the local server does not implement /v1/messages/count_tokens, Anthropic-style cache_control prompt-caching headers, forced tool_choice overrides, or the async Batches API (/v1/messages/batches).
  • Zero Claude subscription required: When you point ANTHROPIC_BASE_URL to http://localhost:11434 and set ANTHROPIC_AUTH_TOKEN=ollama, Claude Code bypasses claude.ai authentication entirely and routes every prompt straight to your local GPU.

Method 1: The one-command setup with ollama launch claude

If you already have Ollama v0.14.0+ and Claude Code installed, the fastest way to start a local coding session is the built-in launcher documented in the official Ollama Claude Code integration guide. Open your terminal inside any Git repository and run:

# Pull a tool-calling local coding model first
ollama pull qwen3-coder

# Launch Claude Code wired directly to your local Ollama server
ollama launch claude

Running ollama launch claude opens an interactive model selector in your terminal, sets the local Anthropic environment variables for that session automatically, and boots Claude Code. If you want to skip the interactive menu inside a shell script, Docker container, or CI job, pass --model and --yes before --:

ollama launch claude --model qwen3-coder --yes -- -p "Audit src/auth/ for unhandled promise rejections"

Method 2: Manual environment variables (macOS, Linux, and Windows PowerShell)

Many developers prefer launching claude directly so they can control context sizes, custom Modelfiles, and shell aliases. As verified by both Ollama’s manual setup documentation and Radiance Systems’ December 2026 local Claude Code benchmark report, you need three environment variables: ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN, and an explicitly blank ANTHROPIC_API_KEY.

macOS, Linux, and WSL2 (Bash / Zsh)

# 1. Install Claude Code if you have not already
curl -fsSL https://claude.ai/install.sh | bash

# 2. Point Claude Code to your local Ollama server
export ANTHROPIC_BASE_URL="http://localhost:11434"
export ANTHROPIC_AUTH_TOKEN="ollama"
export ANTHROPIC_API_KEY=""

# 3. Launch Claude Code with your chosen local model tag
claude --model qwen3-coder

Windows 11 PowerShell

# 1. Install Claude Code natively on Windows PowerShell
irm https://claude.ai/install.ps1 | iex

# 2. Set session variables for Ollama on port 11434
$env:ANTHROPIC_BASE_URL = "http://localhost:11434"
$env:ANTHROPIC_AUTH_TOKEN = "ollama"
$env:ANTHROPIC_API_KEY = ""

# 3. Start Claude Code
claude --model gpt-oss:20b

Once Claude Code opens, type /status inside the prompt. You should see Base URL: http://localhost:11434 and your active local model tag instead of Anthropic’s cloud endpoint. If you are setting up Ollama for the first time, start with our Ollama Tutorial: How to Run Local LLMs Offline, plus our comparisons of Ollama vs LM Studio for Local LLMs and vLLM vs Ollama for Local Development.

Permanent ~/.claude/settings.json config (and the flag that prevents 90% slowdowns)

Exporting terminal variables every morning gets old fast, and more importantly, a default Claude Code setup silently breaks two things on local GPUs:

  1. Background Haiku calls fail: Whenever Claude Code generates a conversation title, summarizes a file tree, or spawns a lightweight helper, it tries to call ANTHROPIC_DEFAULT_HAIKU_MODEL (which defaults to a cloud Claude Haiku model ID that does not exist in your local Ollama library).
  2. Dynamic attribution headers wipe your local KV cache: By default, Claude Code injects a changing billing/attribution header block at the very start of every system prompt. Because local inference engines (Ollama, llama.cpp, and LM Studio) only reuse their prompt KV cache when the prefix tokens match 100%, a changing header at token 0 forces your GPU to re-evaluate the entire 18,000-token system prompt on every single turn. As documented in Unsloth and Radiance Systems’ local Claude Code testing, leaving the attribution header enabled can make multi-turn local coding up to 90% slower.

Fix both permanently by creating or editing ~/.claude/settings.json (C:\Users\YourName\.claude\settings.json on Windows) with this exact configuration:

{
  "env": {
    "ANTHROPIC_BASE_URL": "http://localhost:11434",
    "ANTHROPIC_AUTH_TOKEN": "ollama",
    "ANTHROPIC_API_KEY": "",
    "ANTHROPIC_MODEL": "qwen3-coder",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "qwen2.5-coder:7b",
    "CLAUDE_CODE_SUBAGENT_MODEL": "qwen3-coder",
    "CLAUDE_CODE_ATTRIBUTION_HEADER": "0",
    "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
    "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1",
    "API_TIMEOUT_MS": "600000"
  }
}

Notice the clever split here: you can assign your heavy model (qwen3-coder or qwen3.8:27b) to ANTHROPIC_MODEL and CLAUDE_CODE_SUBAGENT_MODEL, or point ANTHROPIC_DEFAULT_HAIKU_MODEL to the exact same loaded checkpoint so Ollama never has to swap models in and out of VRAM between background title generation and main code edits.

How to fix Ollama’s default 4K context window trap for Claude Code

Look through r/LocalLLM threads on running Claude Code with Ollama and you will see the same complaint over and over: “Claude Code launches fine, thinks for ten seconds, and then stops without writing any code or executing a single tool.”

Nine times out of ten, the culprit is Ollama’s default context length. To protect consumer GPUs from crashing, Ollama automatically caps the context window at 4,096 tokens on graphics cards under 24 GB VRAM (and 32,768 tokens on 24 GB to 48 GB cards). However, Claude Code’s built-in system instructions, bash tool schemas, file-edit schemas, and CLAUDE.md project context consume roughly 15,000 to 20,000 tokens before you even type your first prompt.

When you send an 18,000-token payload into a 4,096-token context window, Ollama silently truncates the tool definitions at the top of the prompt. The model no longer sees how to format a tool_use call, so it just prints plain text or stops.

Two ways to lock in a 32K to 64K+ context window

Both Ollama’s official Claude Code guide and the open-source Claude Code Local Models Setup repository recommend a minimum of 32,768 tokens for small single-file tasks and 64,000+ tokens (65536) for multi-file repositories:

# Option A: Start the Ollama server globally with 64K context + 8-bit KV cache quantization
OLLAMA_CONTEXT_LENGTH=65536 OLLAMA_FLASH_ATTENTION=1 OLLAMA_KV_CACHE_TYPE=q8_0 ollama serve

Setting OLLAMA_KV_CACHE_TYPE=q8_0 (8-bit KV cache instead of 16-bit) cuts the VRAM consumed by that 65,536-token context window in half with near-zero quality loss. Alternatively, you can bake a permanent 64K context window into a dedicated model tag using a 3-line Modelfile:

# Option B: Create a dedicated 64K-context model tag for Claude Code
cat << 'MODFILE' > Modelfile.claude
FROM qwen3-coder
PARAMETER num_ctx 65536
PARAMETER temperature 0.2
MODFILE

ollama create qwen3-coder-64k -f Modelfile.claude
claude --model qwen3-coder-64k
2026 Claude Code and Local Ollama Model Sizing, GPU VRAM, Context Window, and Tool-Calling Matrix
2026 local and hybrid Ollama coding models tested with Claude Code, showing 4-bit weight sizes, minimum GPU VRAM at 32K to 64K context, tool-calling reliability, and recommended hardware tiers.

Best local Ollama models for Claude Code by GPU VRAM tier (2026 benchmarks)

Claude Code is far more demanding than an inline autocomplete plugin. A typical feature request triggers 15 to 40 consecutive tool calls (Glob, Grep, Read, Edit, Bash). If a local model hallucinates a single JSON key inside a tool_use block, the entire agent loop halts. Before pulling a huge checkpoint, calculate your exact weights plus 64K KV-cache footprint using our free Local AI VRAM Calculator, and see our deep-dive benchmark on the Best Local LLMs for Coding in 2026 (8GB to 32GB GPUs).

Model / Ollama TagParameters4-Bit Disk SizeMin GPU VRAM (32K-64K Ctx)SWE-bench / Coding ScoreTool-Calling ReliabilityBest GPU Tier
qwen2.5-coder:7b7B dense4.7 GB6 GB – 8 GB (at 32K ctx)88.4% HumanEvalGood (Single-file edits & scripts)RTX 3060 / 4060 8GB
qwen2.5-coder:14b14B dense9.0 GB12 GB – 16 GB (at 32K-64K)89.9% HumanEvalVery Good (2-4 file refactors)RTX 3060 12GB / 4070
gpt-oss:20b20B dense~12.5 GB16 GB VRAM (at 64K q8_0)Official Ollama Claude RecHigh (Strong bash & diff formatting)RTX 4060 Ti 16GB / 4080
qwen3.6-35b-a3b35B MoE (3B active)~19.5 GB24 GB VRAM (at 131K q8_0)49.5% SWE-bench ProHigh (Very fast token/sec speed)RTX 3090 / 4090 / 24GB Mac
qwen3-coder (30B/32B)30B-32B class~18.5 GB24 GB VRAM (at 64K ctx)90.2%+ HumanEvalExcellent (Official Ollama Rec)RTX 3090 / 4090 / M4 Pro
qwen3.8:27b (Apache 2.0)27B dense~16.2 GB24 GB – 32 GB (25 GB @ 131K)61.7% SWE-bench ProBest Local Workstation AgentRTX 4090 / 5090 / 32GB+ Mac
devstral-small-2 (Apache 2.0)24B dense~14.8 GB24 GB – 32 GB (25 GB @ 64K)68.0% SWE-bench VerifiedExcellent (Built for Claude Code)RTX 4090 / 5090 / 32GB+ Mac
qwen3-coder-next (Apache 2.0)80B MoE (3B active)~44 GB48 GB – 64 GB (49 GB @ 131K)70.6% SWE-bench VerifiedNear-Cloud ParityMac Studio 64GB+ / Dual 3090

All SWE-bench Verified, SWE-bench Pro, and HumanEval figures above are drawn directly from Radiance Systems’ November/December 2026 publisher datasheet audit and the Claude Code Local Models benchmark table.

What about Ollama’s :cloud models for laptop users?

If you are coding on an ultrabook with integrated graphics, Ollama’s Claude Code integration also lets you invoke hosted open-weight models like glm-4.7:cloud, glm-5.3-flash, minimax-m2.1:cloud, or kimi-k2.7-code:cloud through the exact same local CLI (claude --model glm-4.7:cloud). Just remember that :cloud tags execute on remote servers rather than your local GPU.

Ollama vs llama.cpp (llama-server) vs LM Studio for Claude Code

Ollama is not the only local runtime that added native /v1/messages support in January 2026. Both llama.cpp (llama-server) and LM Studio (v0.4.1+) also speak the Anthropic Messages API natively. Here is when you should stick with Ollama versus switching to llama-server:

  • Use Ollama (http://localhost:11434) when you want convenience: One-command ollama launch claude, instant model switching, built-in background service on Windows/macOS/Linux, and smooth hybrid access to :cloud fallback models.
  • Use llama.cpp (llama-server on http://127.0.0.1:8080) when you need /v1/messages/count_tokens and granular 8-bit KV cache flags: Unlike Ollama, llama-server implements the /v1/messages/count_tokens endpoint that Claude Code uses to measure context fullness. You can run Unsloth’s dynamic 4-bit GGUF of Qwen3.8-27B at a massive 131,072-token context window in 25 GB of VRAM with this command from Radiance Systems’ verified recipe:
# Direct llama.cpp server recipe with 131K context and 8-bit KV cache (-ctk q8_0 -ctv q8_0)
llama-server -hf unsloth/Qwen3.8-27B-GGUF:UD-Q4_K_XL \
  --alias qwen3.8-27b -c 131072 --port 8080 \
  -ctk q8_0 -ctv q8_0

export ANTHROPIC_BASE_URL="http://127.0.0.1:8080"
export ANTHROPIC_AUTH_TOKEN="local"
export ANTHROPIC_API_KEY=""
claude --model qwen3.8-27b

100% offline privacy checklist: how to stop hidden telemetry and cloud fallbacks

If you are connecting Claude Code to local Ollama models because of a strict client NDA, HIPAA/SOC2 compliance, or proprietary repository rules, pointing ANTHROPIC_BASE_URL to localhost:11434 is only step one. By default, Claude Code still makes background network calls for update checks, telemetry, and web-fetch preflight scans. Lock your environment down completely with these three rules:

  1. Block Ollama from ever routing to a :cloud model: Set OLLAMA_NO_CLOUD=1 in your shell profile (or set "disable_ollama_cloud": true inside ~/.ollama/server.json) so a mistyped model tag can never send repository code to external inference servers.
  2. Disable Claude Code telemetry and update pings: Add "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1" to your ~/.claude/settings.json file.
  3. Disable WebFetch security preflight pings to Anthropic: Even on a local model, if Claude Code invokes its built-in web page fetch tool, it pings api.anthropic.com for a domain safety check unless you set "skipWebFetchPreflight": true inside your Claude Code configuration (or run in an air-gapped network).

Troubleshooting broken tool calls, thinking loops, and model aliases

If your claude code ollama session connects on /status but misbehaves during actual coding tasks, use these targeted fixes:

  • Symptom: A third-party script or plugin rejects custom model tags and insists on claude-3-5-sonnet. Use ollama cp to alias your local coding model to an official Anthropic model name, as shown in the Ollama Anthropic API docs: ollama cp qwen3-coder claude-3-5-sonnet.
  • Symptom: Claude Code hangs in an endless “Thinking…” block and never edits the file. Some reasoning-heavy checkpoints (like qwen3.5:27b or deepseek-r1) spend all of their max_tokens budget inside <think> tags before emitting the JSON tool_use block. Switch to a coding-tuned checkpoint (qwen3-coder, devstral-small-2, qwen3.8:27b, or gpt-oss:20b) or toggle thinking off in Claude Code for straightforward file edits.
  • Symptom: Context fills up and auto-compaction never triggers. Claude Code’s automatic context compaction window (CLAUDE_CODE_AUTO_COMPACT_WINDOW) has a hard floor of 100,000 tokens. If your local GPU is running a 32,768-token or 65,536-token context window, type /compact manually inside Claude Code every 10 to 15 turns (or /clear between separate tickets) so your prompt never overflows the KV cache.
  • Symptom: Server returns "Extra inputs are not permitted". Newer Claude Code releases occasionally attach experimental beta headers or fields that older local servers reject. Add "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1" to the env block of ~/.claude/settings.json.

Want to extend your local Claude Code setup with Model Context Protocol tools for local PostgreSQL databases, Git, or browser automation? Check our tested roundup of the 25 Best MCP Servers on GitHub in 2026 and browse our live GitHub Repo Directory and AI Tools Directory.

My Opinion

On the November 28, 2026 Terminal-Bench 4.1 leaderboard, Claude Opus 5.5 solves 65.8% of complex multi-step terminal engineering tasks, whereas even a 2,800-billion-parameter open model (Kimi K3.1 Pro) hits 59.7%. A 14B or 27B model running on your desktop GPU is not going to one-shot a 40-file architectural migration without supervision.

Where a claude code ollama setup changes your daily workflow is the 80/20 hybrid split. Roughly 80% of what we use terminal coding agents for every day is routine work: writing unit tests for an existing module, converting a bash script to Python, explaining an unfamiliar repository, renaming props across five components, or drafting boilerplate CRUD endpoints. Burning $20 to $40 a week in cloud API tokens on those tasks makes zero financial sense when qwen3-coder, gpt-oss:20b, or qwen3.8:27b at a 64K context window handles them locally for free.

My personal setup uses a two-alias shell workflow: claude-local launches Claude Code pointed at http://localhost:11434 with CLAUDE_CODE_ATTRIBUTION_HEADER=0 and a 64K q8_0 KV cache for 100% free daily coding and NDA client repos, while standard claude stays reserved for the 20% of thorny cross-repository bugs where cloud reasoning earns its keep.

Frequently Asked Questions

Does Claude Code work natively with Ollama in 2026?

Yes. Starting with Ollama v0.14.0 (released January 16, 2026), Ollama natively supports the Anthropic Messages API (/v1/messages). You can connect Claude Code either by running ollama launch claude or by setting ANTHROPIC_BASE_URL=http://localhost:11434, ANTHROPIC_AUTH_TOKEN=ollama, and ANTHROPIC_API_KEY="" before running claude --model <tag>.

Do I need a paid Claude Pro or Max subscription to use Claude Code with Ollama?

No. When you set ANTHROPIC_BASE_URL to your local Ollama server and supply any placeholder value in ANTHROPIC_AUTH_TOKEN (such as ollama), Claude Code skips claude.ai account login and sends all prompts directly to your local machine at $0 API cost.

Why does Claude Code connect to Ollama but refuse to edit files or run bash commands?

That happens for two reasons: either you picked a model that lacks native tool-calling training, or you left Ollama at its default 4,096-token context limit, which truncates Claude Code’s ~18,000-token system prompt and tool schemas. Set OLLAMA_CONTEXT_LENGTH=65536 (or 32768 minimum on 8 GB to 12 GB cards) and use a verified tool-calling model such as qwen3-coder, gpt-oss:20b, qwen3.8:27b, devstral-small-2, or qwen2.5-coder.

What is the best local Ollama model for Claude Code on a 16 GB or 24 GB GPU?

On a 16 GB GPU (RTX 4060 Ti 16GB, RTX 4080, RTX 5070 Ti), use gpt-oss:20b or qwen2.5-coder:14b with a 32K to 64K context window and 8-bit KV cache (OLLAMA_KV_CACHE_TYPE=q8_0). On a 24 GB to 32 GB GPU (RTX 3090, RTX 4090, RTX 5090, or 32 GB+ Apple Silicon Mac), qwen3-coder, devstral-small-2 (24B), and qwen3.8:27b deliver the highest SWE-bench and tool-calling reliability.

What does CLAUDE_CODE_ATTRIBUTION_HEADER=0 do for local LLMs?

By default, Claude Code prepends a changing attribution header to the system prompt on every request. Because local servers like Ollama, llama-server, and LM Studio can only reuse their prompt KV cache when the starting tokens remain identical, that changing header forces a full prompt re-read on every turn. Setting "CLAUDE_CODE_ATTRIBUTION_HEADER": "0" in ~/.claude/settings.json preserves your local KV cache and prevents up to 90% multi-turn slowdowns.

Does my source code ever leave my computer when using Claude Code with Ollama?

Your code stays 100% on your machine provided you meet three conditions: use a downloaded local model (never a :cloud suffix model), set OLLAMA_NO_CLOUD=1, and add "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1" plus "skipWebFetchPreflight": true in your Claude Code configuration.

How do I keep Claude Code from running out of context on a 32K or 64K local window?

Because Claude Code’s built-in auto-compaction trigger (CLAUDE_CODE_AUTO_COMPACT_WINDOW) cannot be lowered below 100,000 tokens, it will not auto-compact on a 32K or 64K local context window. Run the /compact slash command manually every 10 to 15 turns, or run /clear whenever you start a new task.

Can I connect Claude Code to llama.cpp or LM Studio instead of Ollama?

Yes. Both llama-server (from llama.cpp, typically on http://127.0.0.1:8080) and LM Studio v0.4.1+ (http://localhost:1234 via lms server start) added native Anthropic Messages API compatibility in January 2026. Just change ANTHROPIC_BASE_URL to match their port.

Can I switch between local Ollama models and Anthropic’s cloud API on the same machine?

Yes. Instead of hardcoding ANTHROPIC_BASE_URL globally in ~/.claude/settings.json, create a shell alias such as alias claude-local='ANTHROPIC_BASE_URL=http://localhost:11434 ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_API_KEY="" CLAUDE_CODE_ATTRIBUTION_HEADER=0 claude --model qwen3-coder'. That lets you run claude-local for free offline coding and plain claude whenever you want cloud Claude Sonnet or Opus.

1 thought on “How to Connect Claude Code to Local Ollama Models (2026)”

Leave a Comment