Point Claude Code at a Local Model With Ollama, in Its Own Worktree, With No API Bill A developer documented a method for running Claude Code's full agent loop against a local Ollama model, using Ollama v0.14.0's Anthropic Messages API compatibility to swap the base URL and environment variables instead of paying per-token API costs. The writeup recommends qwen3-coder:30b, warns that Ollama's VRAM-based default context window (4k below 24 GiB, 32k from 24–48 GiB) is too small for agents, and advises setting OLLAMA_CONTEXT_LENGTH=64000 and verifying the allocated window with ollama ps. It also provides a wrapper script setting ANTHROPIC_BASE_URL, model aliases, CLAUDE_CODE_SUBAGENT_MODEL and CLAUDE_CODE_MAX_CONTEXT_TOKENS to keep the configuration out of the normal shell. Since Ollama v0.14.0 https://ollama.com/blog/claude , announced on January 16, 2026, a local Ollama server answers the same Anthropic Messages API that Claude Code https://code.claude.com/docs/en/overview talks to. Change a base URL and a few variables, and the whole agent loop read a file, edit it, run the tests, read the failure runs on a model sitting on your own disk. No Anthropic key, no per-token bill. It also means a model that was never tuned for Claude Code's tool set is now driving it. Before you trust it with real work, find out three things: does it call tools properly, does Claude Code know how big its context really is, and is it fast enough to wait for. You can check all three in one sitting, inside a git worktree you throw away afterwards. Pick a model with tool support. qwen3-coder https://ollama.com/library/qwen3-coder is the one Ollama uses in its own Anthropic-compatibility examples https://docs.ollama.com/api/anthropic-compatibility . Its 30b tag is a 19GB download with a 256K context window and the tools badge on its library page. ollama pull qwen3-coder:30b The download is the easy part. The trap is the context window. Ollama doesn't run a model at its full window by default; it picks one from your VRAM, and the context-length docs https://docs.ollama.com/context-length put that at 4k below 24 GiB, 32k from 24 to 48 GiB, and 256k at 48 GiB or more. On a 48 GiB card the default already clears 64k, but the bottom two tiers leave little room for an agent that sends its system prompt, tool definitions and the conversation so far on every turn. The same docs say coding tools and agents "should be set to at least 64000 tokens." OLLAMA CONTEXT LENGTH=64000 ollama serve If you run the Ollama desktop app, use the context-length slider in its settings instead. Then load the model once and run ollama ps . The CONTEXT column shows the window Ollama actually allocated, and PROCESSOR shows whether the model fits on the GPU or spills onto the CPU. Ollama's docs advise avoiding the CPU spill for speed, so if you see it, drop the context or pick a smaller tag. The agent loop stays the same. Only the thing answering it moves onto your machine. The short route is Ollama's own launcher. Per the Claude Code integration page https://docs.ollama.com/integrations/claude-code , ollama launch claude sets Claude Code up for Ollama and starts it, --model picks the model, and --yes skips the selectors it needs --model . ollama launch claude --model qwen3-coder:30b --yes The integration page doesn't say whether the launcher corrects Claude Code's assumed context window, so launcher users should still run the check in Step 3. The long route is a handful of environment variables, and it shows you every setting the launcher doesn't. Put them in a small wrapper script so they never leak into your normal shell: bash /bin/sh ~/bin/claude-local: Claude Code on a local Ollama model M=qwen3-coder:30b export ANTHROPIC BASE URL=http://localhost:11434 export ANTHROPIC AUTH TOKEN=ollama export ANTHROPIC API KEY="" export ANTHROPIC DEFAULT OPUS MODEL="$M" export ANTHROPIC DEFAULT SONNET MODEL="$M" export ANTHROPIC DEFAULT HAIKU MODEL="$M" export CLAUDE CODE SUBAGENT MODEL="$M" export CLAUDE CODE MAX CONTEXT TOKENS=64000 exec claude --model "$M" "$@" Run chmod +x ~/bin/claude-local and make sure ~/bin is on your PATH . Line by line: ANTHROPIC AUTH TOKEN=ollama is "required but ignored", per the ANTHROPIC API KEY is set to empty on purpose: Claude Code's opus , sonnet and haiku aliases to the local model. Claude Code uses the CLAUDE CODE SUBAGENT MODEL is the default model for subagents and agent-team teammates that aren't assigned one another way CLAUDE CODE MAX CONTEXT TOKENS fixes a mismatch between the two tools. When Claude Code doesn't recognize a model ID, it claude- takes the variable directly, and qwen3-coder:30b qualifies. Know what you give up. Ollama's compatibility layer doesn't support token counting, prompt caching cache control , tool choice , batches, citations or PDF document blocks, and accepts but doesn't enforce thinking's budget tokens compatibility page https://docs.ollama.com/api/anthropic-compatibility . Claude Code's own Remote Control https://code.claude.com/docs/en/env-vars is disabled when ANTHROPIC BASE URL points anywhere other than api.anthropic.com . A git worktree https://git-scm.com/docs/git-worktree is a second working folder on its own branch, sharing the repo's history. Whatever the local model does there stays off your main checkout: cd ~/code/app git worktree add -b try/local-model ../app-local cd ../app-local claude-local Give it a task with a clear right answer and a test to prove it. Something like: "Add a test for slugify in src/slug.js covering empty strings and accents, then run the test suite and fix anything that fails." That one task makes the model read a file, write a new one, run a shell command and react to the result, which is most of what Claude Code asks of a model. First thing in the session, run /status . Ollama's integration page points out that Claude settings files can override shell variables, and /status is how you confirm the base URL and model are the ones you set. Then run /context https://code.claude.com/docs/en/commands , which shows what's filling the window. It should reflect the 64000 you set rather than the 200K Claude Code assumes for unknown models https://code.claude.com/docs/en/model-config correct-the-window-for-a-gateway-or-custom-model-id . The startup warning that the model isn't recognized and its context is assumed to be 200k should also be gone. Keep the permission prompts on. Ollama's docs mention --dangerously-skip-permissions for unattended setups, and only in an isolated environment. A worktree doesn't count. It only separates files inside git, and the model's shell commands still run as you, with access to your home directory and the network. Tool calls. In the session, watch what happens when the model needs a file. A model that handles tools well calls Claude Code's read and edit tools. A weaker one tends to describe the edit in prose, paste a whole file into chat, or call a tool with malformed arguments. You can test the raw capability outside Claude Code too, with one request against Ollama's Messages endpoint: curl -s http://localhost:11434/v1/messages \ -H "content-type: application/json" \ -H "x-api-key: ollama" \ -H "anthropic-version: 2023-06-01" \ -d '{ "model": "qwen3-coder:30b", "max tokens": 512, "tools": { "name": "read file", "description": "Read a file from the repository", "input schema": { "type": "object", "properties": {"path": {"type": "string"}}, "required": "path" } } , "messages": {"role": "user", "content": "What scripts does package.json define?"} }' | jq '.content | select .type == "tool use" ' If jq https://jqlang.org prints a tool use block named read file with "path": "package.json" in its input, the model and the endpoint agree on the format. If it prints nothing, the model answered in text instead, and Claude Code will have the same problem at a larger scale. Context. Let the task run long enough to read a few files, then run /context again and compare it with ollama ps . If Claude Code's numbers climb toward 64k and it compacts, the window line did its job. Speed. Ollama's generate API https://docs.ollama.com/api/generate returns token counts and durations in nanoseconds, which is all you need for tokens per second: curl -s http://localhost:11434/api/generate -d '{ "model": "qwen3-coder:30b", "prompt": "Write a JavaScript slugify function.", "stream": false }' | jq '{ prefill tok s: .prompt eval count / .prompt eval duration / 1e9 , gen tok s: .eval count / .eval duration / 1e9 }' Watch both numbers. gen tok s is how fast it writes. prefill tok s is how fast it reads a prompt, and an agent sends long ones, as Step 1 showed. Your hardware decides both, so I won't quote numbers here. Measure them on your machine and decide whether you can wait for them. When you're done, run the tests yourself and review what changed, from inside ../app-local : git status --short git diff --stat git diff main...try/local-model --stat The first two show what the model changed but didn't commit, which is the usual state after a one-task trial. The third shows anything it did commit; swap main for your default branch if it has another name. Then clean up: cd ~/code/app git worktree remove ../app-local git branch -D try/local-model git worktree remove refuses to delete a folder with modified or untracked files unless you pass --force . If it refuses, look at what's left before you force it. -D deletes the branch even though it was never merged, which is what you want for a throwaway. I build Hermes IDE https://hermes-ide.com , so weigh this part with that in mind. Hermes runs each coding agent in its own real terminal, and its ⌘N task launcher Ctrl+Shift+N on Windows and Linux starts every task on its own hermes/ branch in its own worktree. That's Step 3 done for you on every task. The agent picker lists the built-in agents plus Custom agent , and the site https://hermes-ide.com says Hermes runs "any command you add as a Custom agent." The ⌘N agent picker: Claude Code 2.1.299 and Codex 0.145.0 installed, the other supported agents marked Not installed, and Custom agent. So claude-local from Step 2 can be that command. Add it as a custom agent and it sits in the same picker as the subscription-backed Claude Code. The launcher also picks the account, model, effort and approval mode per task, and has a Save as preset… button, so a "local, try it first" combination can live next to your paid one. Before anything starts, the launcher shows the exact command Hermes will run. The ⌘N New task sheet: the task, the agent, project, worktree, approval, model and effort chips, and the "Hermes will run" preview of the command. That preview is where you catch the mistake that matters here: a task you meant for the local model launching plain claude on your subscription. Read it before you press Enter. I haven't checked which of Hermes's Claude-specific extras follow Claude Code into a custom agent, so treat those as something to try rather than something promised. The branch and worktree come from the launcher, not the agent, and the Review Desk ⌘G lists the task's staged, unstaged and untracked files with an inline diff, so you can read what the local model changed before you land it. Hermes is free, source-available under BSL 1.1, and runs on macOS, Windows and Linux; download it https://hermes-ide.com/download or read the code on GitHub https://github.com/hermes-hq/hermes-ide . Swap the model under an agent and the prompt and tools stay identical while the behavior changes, so you can't see the difference from outside. Step 4 is a small version of the habit that catches it: measure tool-call success, context use and latency. Observability for LLM Applications is the long version, covering tracing, evals, cost and quality regressions for LLM features once they're in front of real users.