{"slug": "point-claude-code-at-a-local-model-with-ollama-in-its-own-worktree-with-no-api", "title": "Point Claude Code at a Local Model With Ollama, in Its Own Worktree, With No API Bill", "summary": "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.", "body_md": "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.\n\nIt 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.\n\nPick 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.\n\n```\nollama pull qwen3-coder:30b\n```\n\nThe 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.\"\n\n```\nOLLAMA_CONTEXT_LENGTH=64000 ollama serve\n```\n\nIf 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.\n\n*The agent loop stays the same. Only the thing answering it moves onto your machine.*\n\nThe 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`).\n\n```\nollama launch claude --model qwen3-coder:30b --yes\n```\n\nThe 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:\n\n``` bash\n#!/bin/sh\n# ~/bin/claude-local: Claude Code on a local Ollama model\nM=qwen3-coder:30b\nexport ANTHROPIC_BASE_URL=http://localhost:11434\nexport ANTHROPIC_AUTH_TOKEN=ollama\nexport ANTHROPIC_API_KEY=\"\"\nexport ANTHROPIC_DEFAULT_OPUS_MODEL=\"$M\"\nexport ANTHROPIC_DEFAULT_SONNET_MODEL=\"$M\"\nexport ANTHROPIC_DEFAULT_HAIKU_MODEL=\"$M\"\nexport CLAUDE_CODE_SUBAGENT_MODEL=\"$M\"\nexport CLAUDE_CODE_MAX_CONTEXT_TOKENS=64000\nexec claude --model \"$M\" \"$@\"\n```\n\nRun `chmod +x ~/bin/claude-local` and make sure `~/bin` is on your `PATH`. Line by line:\n\n`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.\nKnow 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`.\n\nA [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:\n\n```\ncd ~/code/app\ngit worktree add -b try/local-model ../app-local\ncd ../app-local\nclaude-local\n```\n\nGive 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.\n\nFirst 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.\n\nKeep 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.\n\n**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:\n\n```\ncurl -s http://localhost:11434/v1/messages \\\n  -H \"content-type: application/json\" \\\n  -H \"x-api-key: ollama\" \\\n  -H \"anthropic-version: 2023-06-01\" \\\n  -d '{\n    \"model\": \"qwen3-coder:30b\",\n    \"max_tokens\": 512,\n    \"tools\": [{\n      \"name\": \"read_file\",\n      \"description\": \"Read a file from the repository\",\n      \"input_schema\": {\n        \"type\": \"object\",\n        \"properties\": {\"path\": {\"type\": \"string\"}},\n        \"required\": [\"path\"]\n      }\n    }],\n    \"messages\": [{\"role\": \"user\",\n      \"content\": \"What scripts does package.json define?\"}]\n  }' | jq '.content[] | select(.type == \"tool_use\")'\n```\n\nIf [`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.\n\n**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.\n\n**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:\n\n```\ncurl -s http://localhost:11434/api/generate -d '{\n  \"model\": \"qwen3-coder:30b\",\n  \"prompt\": \"Write a JavaScript slugify function.\",\n  \"stream\": false\n}' | jq '{\n  prefill_tok_s:\n    (.prompt_eval_count / (.prompt_eval_duration / 1e9)),\n  gen_tok_s: (.eval_count / (.eval_duration / 1e9))\n}'\n```\n\nWatch 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.\n\nWhen you're done, run the tests yourself and review what changed, from inside `../app-local`:\n\n```\ngit status --short\ngit diff --stat\ngit diff main...try/local-model --stat\n```\n\nThe 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:\n\n```\ncd ~/code/app\ngit worktree remove ../app-local\ngit branch -D try/local-model\n```\n\n`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.\n\nI 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.\"\n\n*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.*\n\nSo `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.\n\n*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.*\n\nThat 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.\n\nI 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).\n\nSwap 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.", "url": "https://wpnews.pro/news/point-claude-code-at-a-local-model-with-ollama-in-its-own-worktree-with-no-api", "canonical_source": "https://dev.to/gabrielanhaia/point-claude-code-at-a-local-model-with-ollama-in-its-own-worktree-with-no-api-bill-3nf0", "published_at": "2026-11-28 13:08:40+00:00", "updated_at": "2026-11-28 13:13:47.754581+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "large-language-models", "developer-tools", "ai-infrastructure"], "entities": ["Ollama", "Claude Code", "Anthropic", "qwen3-coder"], "also_reported_by": [], "takeaway": "Ollama's default context window is chosen from VRAM (4k below 24 GiB, 32k up to 48 GiB), so set OLLAMA_CONTEXT_LENGTH=64000 and confirm the allocated window with ollama ps before pointing Claude Code at a local model.", "recommendation": {"advice": "When routing Claude Code to a local Ollama model, set OLLAMA_CONTEXT_LENGTH=64000 and CLAUDE_CODE_MAX_CONTEXT_TOKENS=64000, then check ollama ps for CPU spill before trusting it with real work.", "playbook": "llm-cost", "playbook_url": "https://wpnews.pro/playbooks/llm-cost"}, "alternates": {"html": "https://wpnews.pro/news/point-claude-code-at-a-local-model-with-ollama-in-its-own-worktree-with-no-api", "markdown": "https://wpnews.pro/news/point-claude-code-at-a-local-model-with-ollama-in-its-own-worktree-with-no-api.md", "text": "https://wpnews.pro/news/point-claude-code-at-a-local-model-with-ollama-in-its-own-worktree-with-no-api.txt", "jsonld": "https://wpnews.pro/news/point-claude-code-at-a-local-model-with-ollama-in-its-own-worktree-with-no-api.jsonld"}}