A JOURNAL OF ARTIFICIAL INTELLIGENCE
my ai daily

How to Run Claude Code With Ollama: Local Setup & Traps

Run Claude Code against local open-weight models using Ollama’s Anthropic Messages API (/v1/messages) via ollama launch claude or ANTHROPIC_BASE_URL—and avoid the 4K default context trap that breaks tool calls.

AI-generated editorial illustration of a brushed-steel developer command console on the left linked via a braided amber-and-cyan optical cable into a matte-black local workstation tower housing glowing cyan GPU compute cores on the right.
AI-generated illustration / my ai dailyIllustrative image.

THE STORY IN THREE POINTS

  • Zero-proxy native bridge: since Ollama v0.14.0 introduced Anthropic Messages API support at http://localhost:11434/v1/messages (with ollama launch claude streamlined in newer 0.15+ / 0.40+ builds), Claude Code runs directly against local weights with no Anthropic cloud account or proxy.
  • Two setup paths: run ollama launch claude --model qwen3.8:27b to configure env vars inside a child session safely, or export ANTHROPIC_BASE_URL=http://localhost:11434, ANTHROPIC_AUTH_TOKEN=ollama, and ANTHROPIC_API_KEY="" manually for LAN GPU hosts.
  • Mandatory fix before coding: Ollama defaults to 4,096 tokens under 24 GiB VRAM, silently truncating Claude Code’s built-in tool schemas; set OLLAMA_CONTEXT_LENGTH=64000 (or 65536 for 64K) and verify with ollama ps.

How Ollama’s Anthropic Messages API runs Claude Code locally

Starting in Ollama 0.14.0, Ollama natively exposes an Anthropic-compatible Messages API endpoint at http://localhost:11434/v1/messages, letting developers run Anthropic’s Claude Code terminal harness directly against open-weight models without third-party translation proxies or cloud token charges, as documented in Ollama’s official Claude Code integration docs and StationX’s walkthrough.

Think of the setup as decoupling the coding harness from the model backend: Claude Code still manages repository reads, file diffs, shell approvals, hooks, and subagent orchestration locally (installed cleanly via our Windows and WSL2 Claude Code setup guide), while Ollama substitutes local GPU weights for Anthropic’s cloud API—requiring zero paid Anthropic subscription when routed solely to localhost.

Method 1: One-command launch with ollama launch claude

If you have Claude Code and a current Ollama build installed (check with ollama --version; 0.14.0 adds Messages API support, 0.15.0+ adds ollama launch, and 0.40+ improves streaming tool-call reliability), Ollama’s built-in launcher handles session wiring automatically without altering your default cloud profile, as shown in Michael Wolfinger’s workshop and Morph’s Ollama guide.

First pull a tool-capable local coding checkpoint such as ollama pull qwen3.8:27b, then start an isolated session with ollama launch claude --model qwen3.8:27b. Under the hood, ollama launch injects ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN, and an empty ANTHROPIC_API_KEY strictly inside the spawned Claude Code child process—leaving normal terminal tabs untouched for cloud Claude Opus 5.5 work.

If you omit --model, running ollama launch claude opens an interactive terminal selector listing compatible downloaded checkpoints, and passing --config writes standalone Claude settings if you want sessions routed to Ollama permanently. Before accepting edits inside a large repository, open a second terminal tab and verify ollama ps reports both your expanded 64K context length and 100% GPU processor placement.

Method 2: Manual environment variables and remote LAN GPU servers

Developers who prefer explicit shell control—or who host Ollama on a dedicated Linux GPU server across a VPN or Tailscale mesh—can point Claude Code manually using standard environment variables documented on the Ollama Blog. On macOS or Linux, export ANTHROPIC_BASE_URL=http://localhost:11434, ANTHROPIC_AUTH_TOKEN=ollama, and ANTHROPIC_API_KEY="", then start claude --model qwen3-coder:30b.

On Windows PowerShell, set the same variables using $env:ANTHROPIC_BASE_URL="http://localhost:11434", $env:ANTHROPIC_AUTH_TOKEN="ollama", and $env:ANTHROPIC_API_KEY="" before invoking claude. Setting ANTHROPIC_AUTH_TOKEN=ollama satisfies the SDK’s non-empty auth header check while clearing ANTHROPIC_API_KEY prevents accidental fallback to a stored Anthropic cloud key. To connect a laptop to a headless workstation box on your LAN, bind OLLAMA_HOST=0.0.0.0:11434 on the GPU server behind Tailscale or a firewall, confirm curl http://<server-ip>:11434/api/tags answers from your laptop, and point ANTHROPIC_BASE_URL=http://<server-ip>:11434.

Picking local models that actually execute Claude Code tools

Claude Code relies continuously on structured streaming tool calls (Bash, Read, Edit, Grep) governed by lengthy system prompts. Small legacy 7B or 8B checkpoints routinely fail schema adherence and dump raw JSON into chat instead of editing files. Selecting proven tool-calling weights from our hardware-tiered local coding picks prevents broken edit loops on turn one.

On 16GB GPUs or 16GB–24GB Macs, pull gpt-oss:20b for raw speed or gemma4:12b (7.7–8.0 GB) for lightweight 64K headroom. On 24GB GPUs or 32GB Macs, standardize on qwen3.8:27b (~18 GB at Q4_K_M) or qwen3-coder:30b for reliable multi-step edits—remembering to enable OLLAMA_KV_CACHE_TYPE=q8_0 on tight 24GB cards and toggle thinking mode off during quick inline edits so reasoning loops do not slow completions. On 64GB+ workstations and Mac Studios, step up to the 80B MoE Qwen3-Coder-Next (~52 GB download) and verify 100% GPU placement via our Ollama GPU troubleshooting steps.

The #1 trap: fixing Ollama’s 4K default context truncation

Even after connecting cleanly, the most frequent failure developers hit is Claude Code ignoring CLAUDE.md instructions, forgetting active files, or failing to trigger file edits. As Michael Wolfinger highlights, Ollama defaults to 4,096 tokens on machines under 24 GiB VRAM. Because Claude Code’s system prompt and built-in tool schemas alone occupy thousands of tokens on turn one, a 4K window truncates tool definitions immediately.

Ollama’s documentation and Masai School’s Claude Code guide recommend setting at least 64K tokens for coding agents. Set OLLAMA_CONTEXT_LENGTH=64000 (or 65536—never OLLAMA_NUM_CTX, which the server ignores) via sudo systemctl edit ollama on Linux systemd, launchctl setenv OLLAMA_CONTEXT_LENGTH 64000 plus a full menu-bar restart on macOS, or Windows User Environment Variables. Restart ollama serve and inspect the CONTEXT column in ollama ps as walked through in our Ollama context length fix guide.

Honest tradeoffs: local Ollama privacy vs. cloud Claude Opus 5.5

Running Claude Code over Ollama eliminates per-token API bills, supports offline travel or air-gapped networks, and guarantees zero source snippets leave your LAN. For unit-test generation, boilerplate scaffolding, and sensitive proprietary repos, local 27B–80B models handle routine edits comfortably once context length and tool schemas are properly sized.

At the same time, local 20B–30B checkpoints trail cloud Claude Opus 5.5 and Sonnet 5.5 on ambiguous cross-service architectural refactors and deep debugging chains. Most production engineers adopt a hybrid split: run ollama launch claude --model qwen3.8:27b for private daily edits and tests, while reserving cloud Opus 5.5 for complex multi-file design reviews.

To keep local sessions snappy within a 64K context budget, disable idle MCP servers inside .claude before opening sprawling monorepos, keep OLLAMA_KV_CACHE_TYPE=q8_0 enabled on 24GB GPUs so KV blocks remain in VRAM without host-RAM swapping, and scope Claude Code prompts around targeted packages or failing unit tests rather than asking a local 27B checkpoint to audit thirty unrelated directories in a single pass.

Sources: Claude Code Integration Guide|Ollama Docs ↗ Running Claude Code With Local LLMs via Ollama|Michael Wolfinger ↗ Claude Code + Ollama Setup & Tool Calling|Morph ↗ Connect Claude Code to Local Ollama Models|StationX ↗ Anthropic API Compatibility Announcement|Ollama Blog ↗ Claude Code Context & Setup Explained|Masai School ↗

THE BROADER CONTEXT · CLAUDE DOSSIER

Explore full product specifications, pricing analysis, and continuous coverage in the Claude Desk Hub. Independent benchmarks available on the AI Intelligence Index and Coding Agents Leaderboard.

02The next story

News

Claude Code "Prompt Is Too Long" Fix: /compact & /context

When Claude Code halts with "Prompt is too long", your request exceeded the active model context window (1M default on Opus/Sonnet 5.5, or 200K with CLAUDE_CODE_DISABLE_1M_CONTEXT=1)—not your usage quota. Here is how /context, /mcp disable, /compact, and claude update fix it.

How to Install Claude Code on Windows (Native & WSL2)

Anthropic has deprecated npm install for Claude Code in favor of standalone native installers—yet native Windows lacks OS command sandboxing. Here is when to choose PowerShell vs. WSL2, exact setup commands, and fixes for PATH and Git Bash errors.

Aider vs Claude Code: Git-First CLI vs Subagent Runtime

Both Aider and Claude Code live inside the terminal—yet one treats Git commits as its core contract with any cloud or local model, while the other ships hooks, MCP, and parallel subagents tuned strictly for Anthropic’s top-ranked Claude checkpoints.

Follow your curiosity.

EXPLORE THE ARCHIVESOURCES & CONTEXT INCLUDED