How to use Claude Code with Ollama (local models)

Claude Code works with Ollama 0.14.0 or later, because Ollama serves Anthropic's Messages API. Run ollama launch claude, or point Claude Code at http://localhost:11434 with the placeholder token ollama, then start claude with a local model that calls tools and has a 64K-token context window.

Contents

Claude Code runs on Ollama because Ollama, from version 0.14.0, serves Anthropic's Messages API on your own machine. Run ollama launch claude and pick a model, or set ANTHROPIC_BASE_URL=http://localhost:11434 and ANTHROPIC_AUTH_TOKEN=ollama yourself and start claude --model with a local model. Before either, raise the model's context length to 64K tokens.

Everything below comes from the Ollama, Anthropic and LM Studio docs and from Ollama's source, read on 24 October 2026, when the newest Ollama release was 0.40.10. It isn't Claude any more once you do this: the open model on your computer answers, with Claude Code's tools and prompts around it.

How Claude Code talks to Ollama#

Pointed at an address with ANTHROPIC_BASE_URL, Claude Code sends it requests in Anthropic's Messages API format. Ollama answers those requests at http://localhost:11434/v1/messages (Ollama's Anthropic compatibility page), so Claude Code only needs to be told where to send them and what to send with them.

  • ANTHROPIC_BASE_URL is the address. For Ollama on the same computer it's http://localhost:11434.
  • ANTHROPIC_AUTH_TOKEN is the credential. Ollama's local server doesn't check it, and its docs use the word ollama, marked "required but ignored". It still matters to Claude Code: Anthropic's gateway docs say that with only the base URL set, "a saved claude.ai login remains the active credential". With a token set, your Claude sign-in stays saved on the machine but isn't sent.
  • ANTHROPIC_API_KEY="" appears in Ollama's own setup too. It clears any Anthropic key in your shell, so that key isn't sent along.

The quick way: ollama launch claude#

Ollama 0.15 added ollama launch (Ollama blog, 23 January 2026). It asks which model to use, offers to install Claude Code if it's missing, and starts it with the settings in place:

bash
ollama launch claude
ollama launch claude --model qwen3-coder

We read what it sets in Ollama's source for 0.40.10. Besides the base URL and the token, it points Claude Code's Opus, Sonnet and Haiku settings (ANTHROPIC_DEFAULT_OPUS_MODEL, _SONNET_MODEL, _HAIKU_MODEL) and CLAUDE_CODE_SUBAGENT_MODEL at the model you picked, and turns off error reporting, the feedback command and the attribution block at the start of the system prompt. It leaves Claude Code's fourth alias, fable, alone. It sets Claude Code's context window only for Ollama's cloud models, so with a local model the context section below still applies.

Watch the model names. A name ending in :cloud, like glm-4.7:cloud, runs on Ollama's servers once you sign in to Ollama, not on your machine, and your prompts and code go there.

Set up Claude Code with Ollama by hand#

  1. Install or update Ollama to 0.14.0 or later, and Claude Code.
  2. Pull a model that supports tool calling, such as qwen3-coder, one of the local models Ollama's launch post recommends: ollama pull qwen3-coder. Claude Code reads files, edits them and runs commands through tool calls, so a model without tools can only chat.
  3. Set the context length to at least 64000 tokens, in the Ollama app's settings or with OLLAMA_CONTEXT_LENGTH=64000 ollama serve (the next section says why).
  4. In the terminal you'll run Claude Code from, set the variables:
bash
export ANTHROPIC_BASE_URL=http://localhost:11434
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_API_KEY=""
  1. Start it with the model's Ollama name: claude --model qwen3-coder.

Those exports last for the terminal session. We'd keep them in an alias rather than in ~/.claude/settings.json, because an env block there moves every Claude Code session on the machine to Ollama, and plain claude should stay on Anthropic:

bash
alias claude-local='ANTHROPIC_BASE_URL=http://localhost:11434 \
  ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_API_KEY="" \
  ANTHROPIC_DEFAULT_OPUS_MODEL=qwen3-coder \
  ANTHROPIC_DEFAULT_SONNET_MODEL=qwen3-coder \
  ANTHROPIC_DEFAULT_HAIKU_MODEL=qwen3-coder \
  ANTHROPIC_DEFAULT_FABLE_MODEL=qwen3-coder \
  CLAUDE_CODE_SUBAGENT_MODEL=qwen3-coder \
  CLAUDE_CODE_MAX_CONTEXT_TOKENS=64000 \
  claude --model qwen3-coder'

The model variables are there because --model only sets the main conversation. Background tasks are covered without them: behind a base URL with ANTHROPIC_AUTH_TOKEN set, Claude Code runs them on the main model unless ANTHROPIC_DEFAULT_HAIKU_MODEL names another (gateway compatibility guide). Subagents are the reason. A subagent can ask for a model by alias, opus, sonnet, haiku or fable, and the built-in claude-code-guide subagent runs on Haiku. Claude Code turns the alias into a Claude model name and sends it to Ollama, which has never pulled it, unless the matching variable on Claude Code's model configuration page names a local model. CLAUDE_CODE_SUBAGENT_MODEL is for subagents that name no model; those run on the main model anyway, and we set it only to match ollama launch. Our Claude Code environment variables guide lists the rest of them.

Context length: the setting most setups get wrong#

Two defaults work against you here.

Ollama sizes the context window by the memory on your graphics card. Its context length page gives 4K tokens below 24 GiB of VRAM, 32K from 24 to 48 GiB and 256K above that, and says agents and coding tools "should be set to at least 64000 tokens". Claude Code sends its system prompt and tool definitions with every request, and LM Studio's guide warns that tools like it "can consume a lot of context". Run ollama ps while the model is loaded: the CONTEXT column shows what it really got, and PROCESSOR shows whether part of it spilled from the GPU onto the CPU, which is slower.

Claude Code, for its part, doesn't know a name like qwen3-coder. It warns that the model isn't recognized and assumes a 200K-token window, then plans compaction around 200K. Set CLAUDE_CODE_MAX_CONTEXT_TOKENS to the window you gave Ollama. For a model name that doesn't start with claude-, the docs say the variable "applies directly and proactive compaction continues at the declared window".

More context costs memory. Ollama's launch post says glm-4.7-flash needs about 23 GB of VRAM at 64,000 tokens, so a model that fits at 4K may not fit at 64K.

Claude Code with LM Studio#

LM Studio added the same Anthropic endpoint in version 0.4.1 (changelog). From LM Studio's Claude Code page:

  1. Start LM Studio's local server (port 1234 by default), in the app or with lms server start --port 1234.
  2. Set ANTHROPIC_BASE_URL=http://localhost:1234, ANTHROPIC_AUTH_TOKEN=lmstudio and CLAUDE_CODE_ATTRIBUTION_HEADER=0. If you turned on Require Authentication, the token is your LM Studio API token instead.
  3. Run claude --model openai/gpt-oss-20b, or the name of the model you loaded.

LM Studio asks for "more than ~25k context length", a lower bar than Ollama's 64K. The model variables and CLAUDE_CODE_MAX_CONTEXT_TOKENS work the same way as with Ollama. Codex has its own route to both servers, covered in Codex CLI with local models.

What doesn't work, and what to expect#

Ollama's compatibility page lists what it doesn't support, among them the count_tokens endpoint, tool_choice, prompt caching with cache_control, batches, citations and PDF documents. The one you'll notice is token counting: without it, Claude Code falls back to a character-based estimate, so /context shows approximate numbers (gateway compatibility guide).

A few Claude Code features depend on Anthropic. Remote Control turns off when ANTHROPIC_BASE_URL points anywhere other than api.anthropic.com, since Claude Code 2.1.196. The fast mode check and the WebFetch domain safety check call api.anthropic.com directly, so they need a connection to Anthropic.

If you want Claude Code to stay off the network, CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 stops auto-updates, telemetry, error reporting, /feedback and availability checks (environment variables). Web search, web fetch and remote MCP servers need a connection by nature.

Support and quality are the bigger questions. Anthropic says it "doesn't support routing Claude Code to non-Claude models through any gateway", so problems go to Ollama or the model's makers. Claude Code's prompts and tools are written for Claude, and how well an open model follows them varies by model and by task. Try one on a real task in a repository with a clean git state, so you can throw its changes away. A hosted open model behind the same kind of address is another route, with the provider's plan instead of your hardware: Claude Code with GLM or Kimi has those settings.

How Shuriken does it#

In Shuriken, a local model is a source, like your Claude accounts. Open Sources › Add source, pick Claude Code, then Custom endpoint. Under "On this computer", Shuriken checks the usual addresses of Ollama, LM Studio, llama.cpp and vLLM; pick one and it fills in the address, with no key needed. Press Test; "Reached" and a model count mean the server answered. Model names sets which of its models answers when Claude Code asks for Opus, Sonnet or Haiku. Left empty, Shuriken still points each of the three the server has no model for at one of its models, so a subagent that asks for Haiku gets an answer.

Switching never moves a chat onto a local source while a subscription has room. It takes chats when you pick it with Use. Otherwise it waits: once every Claude sign-in and key you added is out, a Claude Code chat can carry on there between turns, as long as the chat's history can be read there (source switching explains the order). It shows as Local, and it runs on your own hardware. The local models page covers Codex and models on another computer on your network. To keep a local model ready for when your Claude limits run out, download Shuriken.

Sources

  1. Claude Code (Ollama docs) (accessed 24 October 2026)
  2. Anthropic compatibility (Ollama docs) (accessed 24 October 2026)
  3. Context length (Ollama docs) (accessed 24 October 2026)
  4. Claude Code with Anthropic API compatibility (Ollama blog, 16 January 2026) (accessed 24 October 2026)
  5. ollama launch (Ollama blog, 23 January 2026) (accessed 24 October 2026)
  6. cmd/launch/claude.go in Ollama v0.40.10 (GitHub) (accessed 24 October 2026)
  7. Model configuration (Claude Code docs) (accessed 24 October 2026)
  8. Environment variables (Claude Code docs) (accessed 24 October 2026)
  9. Other LLM gateways (Claude Code docs) (accessed 24 October 2026)
  10. Claude Code gateway compatibility guide (Claude Code docs) (accessed 24 October 2026)
  11. Claude Code (LM Studio docs) (accessed 24 October 2026)
  12. API changelog (LM Studio docs) (accessed 24 October 2026)

Frequently asked questions

Does Claude Code work with Ollama?

Yes, with Ollama 0.14.0 or later, which serves Anthropic's Messages API at http://localhost:11434. Ollama's docs list Claude Code as a supported integration, and ollama launch claude (Ollama 0.15 or later) sets it up for you.

Does Claude Code with Ollama use my Claude subscription or limits?

No. With ANTHROPIC_AUTH_TOKEN set, Claude Code sends that token in place of your claude.ai sign-in, and Anthropic's docs say the subscription's usage limits don't apply to those requests. Your saved sign-in stays on the machine for when you start Claude Code without the variables.

Which local model should I use with Claude Code?

One that supports tool calling, because Claude Code reads and edits files through tools, run with at least 64K tokens of context, as Ollama's Claude Code page asks. Ollama's launch post recommends glm-4.7-flash, qwen3-coder and gpt-oss:20b; which works best depends on your code and your hardware.

Can Claude Code run fully offline with a local model?

The model's requests stay on your machine, but Claude Code also makes other network calls. CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 stops auto-updates, telemetry and error reports; web search, web fetch and remote MCP servers still need a connection.

Why does Claude Code say the model isn't recognized and assume 200k tokens?

Claude Code doesn't know a local model's name, so it assumes a 200K-token window. Set CLAUDE_CODE_MAX_CONTEXT_TOKENS to the context length you gave the model in Ollama, such as 64000, so compaction happens before that window fills.

Free tools