# Claude Code, Codex or OpenCode on a local model (Ollama)

All three coding agents can run on your own GPU through Ollama. The catch is 64k of context, which Ollama doesn't give you by default. Which agent to pick, and what your card can actually hold.

Updated Nov 17, 2026 · By Ari Vale

This page has referral links: we earn a commission if you buy, at no cost to you.

Our Vast referral code: 623346 (link: https://cloud.vast.ai/?ref_id=623346). New users get no bonus from it; we earn 3% of what referred users spend (Vast's referral program docs: https://docs.vast.ai/guides/reference/referral-program?ref_id=623346).

## The short answer

All three can run on a local model through Ollama: Claude Code, OpenAI's Codex CLI and OpenCode. Since Ollama v0.15.0, `ollama launch claude`, `ollama launch codex` and `ollama launch opencode` set each one up for you. The part people miss is context. Ollama's docs say coding agents need at least 64k tokens, but Ollama's default is 4k on cards under 24 GiB and 32k on 24–48 GiB cards. At 64k, our VRAM engine puts Qwen3-Coder 30B at about 24.0 GiB and gpt-oss 20B at about 15.6 GiB (Q4_K_M, fp16 KV cache). Pick OpenCode if you want an open-source (MIT) tool with a plain config file, Codex CLI if you want Apache-2.0 and gpt-oss, and Claude Code if you already know its workflow and accept a proprietary licence.

- **Set the context to 64k first:** `OLLAMA_CONTEXT_LENGTH=64000 ollama serve` (Ollama docs, Sep 25, 2026).
- **24GB card:** Qwen3-Coder 30B at 64k is right at the limit (24.0 GiB). An 8-bit KV cache brings it to about 21.0 GiB.
- **16GB card:** gpt-oss 20B at 64k is tight (15.6 GiB), and 14.1 GiB with an 8-bit KV cache.
- **Under 16GB:** use Ollama's cloud models, or rent a GPU by the hour.

Plenty of people have tried putting Claude Code or OpenCode on top of a local model this year, and the complaint we keep seeing in their threads isn't "the model is dumb", it's "the agent forgets the task after three files." Almost every time, that's a context problem, not a model problem. This guide is the map we wished existed: what each tool is, how it connects to Ollama, and how much GPU memory the setup the docs ask for actually costs.

> **Versions this guide is pinned to**
>
> Ollama v0.34.4 (released Sep 23, 2026), Codex CLI rust-v0.157.0 (Sep 25, 2026), OpenCode v1.18.32 (Sep 21, 2026). Docs accessed Sep 25, 2026. Re-checked Nov 12, 2026 against Ollama v0.40.16 (Nov 9), Codex CLI rust-v0.167.2 (Nov 11) and OpenCode v1.19.5 (Oct 28): the `ollama launch` commands, the 64k advice and the context defaults by VRAM are unchanged in Ollama's docs. All three tools ship updates weekly, so if a flag here doesn't work, check the linked doc page first. We quote commands from the official docs; we haven't benchmarked these tools ourselves.

![Flat illustration of three developers at a shared desk, their laptops cabled to one small desktop machine underneath](https://media.aliteq.com/media/software/run-coding-agents-on-local-models-ollama-2026.jpg)
One local model server, three different coding agents on top of it. · Illustration generated with Higgsfield

## How this works: three agents, one local server

All three are terminal coding agents. They read your repo, edit files and run commands. Normally each one talks to its maker's cloud API. [Ollama](https://aliteq.com/ollama) makes a local model look like those APIs. It added an [Anthropic Messages API](https://github.com/ollama/ollama/releases/tag/v0.14.0) in v0.14.0 (January 2026), which is what Claude Code speaks. It also offers OpenAI-compatible endpoints, which Codex and OpenCode use. Then [v0.15.0](https://github.com/ollama/ollama/releases/tag/v0.15.0) added `ollama launch`, which configures and starts Claude Code, Codex, OpenCode or Droid for you.

```shell
# Ollama v0.34.4 — one-command setup for each agent
ollama launch claude
ollama launch codex
ollama launch opencode
```

If you already manage models in Ollama, you're most of the way there; our [Ollama model-management guide](https://aliteq.com/how-to-manage-local-ai-models-ollama) covers pulling, listing and removing models. The rest of this page is about the two decisions that matter: which agent, and whether your GPU can give it enough context.

## The 64k context trap (read this before anything else)

Every one of these integrations asks for a lot of context. The Ollama docs say it plainly for each tool. Claude Code: "set the context length to 64k or higher" for larger repositories. OpenCode: "requires a context length of 64k or higher". Codex: "at least 64k tokens". Ollama's [context-length page](https://docs.ollama.com/context-length) says coding tools "should be set to at least 64000 tokens."

Ollama's defaults don't give you that. The same page lists the defaults by VRAM: **under 24 GiB, 4k context; 24–48 GiB, 32k; 48 GiB and up, 256k.** The older [FAQ](https://docs.ollama.com/faq) still says a flat 4,096 tokens. Either way, anyone on a normal gaming card starts well short of 64k, and the agent silently loses the early part of the conversation. On Sep 25, 2026, OpenCode's own [provider docs](https://opencode.ai/docs/providers/) pointed at the same cause: "If tool calls aren't working, try increasing num_ctx." (That line was gone when we re-checked on Nov 12; the page now says OpenCode finds a local Ollama server and its models by itself.)

```shell
# Ollama docs (context-length), accessed Sep 25, 2026
OLLAMA_CONTEXT_LENGTH=64000 ollama serve

# check what you actually got — look at the CONTEXT and PROCESSOR columns
ollama ps
```

`ollama ps` is the honest check. If PROCESSOR shows anything other than 100% GPU, part of the model has spilled into system RAM and generation will crawl. That's the next problem: 64k of context costs memory.

## What 64k context costs in VRAM

Here's our [VRAM engine](https://aliteq.com/tools/vram-calculator) on the two local coding models Ollama users reach for most. Assumptions: Q4_K_M weights, fp16 KV cache (Ollama's default), 0.8 GiB overhead. The KV cache, the memory that holds your context, grows linearly with context length, which is why 4k looks fine and 64k doesn't.

### Total VRAM by context length (our engine, Q4_K_M, fp16 KV)

|   | 4k | 32k | 64k | KV cache at 64k |
| --- | --- | --- | --- | --- |
| Qwen3-Coder 30B-A3B | 18.4 GiB | 21 GiB | 24 GiB | 6 GiB |
| gpt-oss 20B | 12.8 GiB | 14.1 GiB | 15.6 GiB | 3 GiB |
| Qwen3-Next 80B-A3B | — | — | 52 GiB | 6.0 GiB |
| gpt-oss 120B | — | — | 71.2 GiB | 4.5 GiB |

### Does it fit? Qwen3-Coder 30B at 64k context needs ≈24.0 GiB

Qwen3-Coder 30B-A3B · Q4_K_M · 64k context needs about 24 GB of VRAM.

- RTX 5060 Ti / 4060 Ti (16GB) (16 GB): does not fit
- RTX 3090 / 4090 (24GB) (24 GB): fits
- RTX 5090 (32GB) (32 GB): fits
- RTX A6000 / L40 (48GB) (48 GB): fits

Our engine, fp16 KV cache. A 24GB card is at 100%. It technically loads, then fails the moment your desktop wants some VRAM. With OLLAMA_KV_CACHE_TYPE=q8_0 the total drops to about 21.0 GiB.

### Does it fit? gpt-oss 20B at 64k context needs ≈15.6 GiB

gpt-oss 20B · Q4_K_M · 64k context needs about 15.6 GB of VRAM.

- RTX 3060 (12GB) (12 GB): does not fit
- RTX 5060 Ti / 4060 Ti (16GB) (16 GB): fits
- RTX 3090 / 4090 (24GB) (24 GB): fits

Our engine, fp16 KV cache. Ollama's default gpt-oss:20b download is 14 GB (per the Ollama library), a bit larger than our Q4_K_M weight estimate, so treat a 16GB card as tight and use the 8-bit KV cache (≈14.1 GiB).

The lever that helps most isn't a smaller model, it's a smaller KV cache. Ollama's FAQ documents `OLLAMA_KV_CACHE_TYPE=q8_0`, which uses "approximately 1/2 the memory of f16" with "a very small loss in precision". It needs Flash Attention, which Ollama turns on automatically where the hardware supports it. On our numbers, that takes [Qwen3-Coder](https://aliteq.com/qwen3-8-27b-hardware-requirements) at 64k from 24.0 to about 21.0 GiB, which is the difference between "tight" and "fine" on a 24GB card like the [RTX 3090](https://aliteq.com/gpus/rtx-3090) or [RTX 4090](https://aliteq.com/gpus/rtx-4090). On a 16GB [RTX 5060 Ti](https://aliteq.com/gpus/rtx-5060-ti-16gb), gpt-oss 20B is the realistic ceiling. Full per-model tables are on [best GPU for Qwen3-Coder](https://aliteq.com/cost-to-run/qwen3-coder-30b-a3b#best-gpu) and [best GPU for gpt-oss 20B](https://aliteq.com/cost-to-run/gpt-oss-20b#best-gpu).

```shell
# Ollama FAQ, accessed Sep 25, 2026 — global setting, applies to every model
OLLAMA_KV_CACHE_TYPE=q8_0 OLLAMA_CONTEXT_LENGTH=64000 ollama serve
```

**Your card can't hold a 64k-context coder? Rent one by the hour** A 48GB card like the RTX A6000 or L40 is enough for Qwen3-Coder at 64k with room to spare. This link opens Vast.ai with our Ollama and Open WebUI template, so the box opens ready to chat; set the 64k context, then point your agent's base URL at it. Spot instances can be reclaimed, so commit your work often. [See live 48GB GPU prices](https://cloud.vast.ai/?template_id=26e7446a7d6906ee366b620c84e1e422&ref_id=623346) (Referral link: we earn a commission if you buy.) Live prices: https://aliteq.com/vast-ai-pricing

A 48GB [RTX A6000](https://aliteq.com/gpus/rtx-a6000) is the cheapest single card that clears 64k with headroom; [live cloud GPU prices](https://aliteq.com/cloud-gpu) list it cheapest-first. Not sure renting beats buying for your hours? Our [cost-to-run page for Qwen3-Coder](https://aliteq.com/cost-to-run/qwen3-coder-30b-a3b) does the per-hour maths from live prices, and [can you run Qwen3-Coder locally](https://aliteq.com/can-you-run-qwen3-coder-locally) covers the hardware side in more depth.

## Which agent to pick: what the docs actually say

We're only comparing things you can check in each project's docs or repo: licence, how it connects to Ollama, how it's configured, and what's documented as unsupported. We're not going to tell you which one "feels smarter". On a local backend they all use the same model, so the model and your context budget matter more than the wrapper. For which model to load, see the [best local LLM for coding on a single GPU](https://aliteq.com/best-local-llm-for-coding-single-gpu) and, for OpenCode specifically, the [best local coding model for OpenCode](https://aliteq.com/best-local-coding-model-for-opencode).

### Claude Code vs Codex CLI vs OpenCode with Ollama (docs accessed Sep 25, 2026)

|   | Claude Code | Codex CLI | OpenCode |
| --- | --- | --- | --- |
| Licence | Proprietary (Anthropic Commercial Terms) | Apache-2.0 | MIT |
| API it uses via Ollama | Anthropic Messages (/v1/messages) | OpenAI Responses (wire_api = "responses") | OpenAI-compatible (/v1) |
| One-command setup | ollama launch claude | ollama launch codex | ollama launch opencode |
| Manual config | 3 env vars (ANTHROPIC_*) | --oss flag, or a profile TOML | Provider block in opencode.json |
| Windows | PowerShell installer | Via WSL (OpenAI docs) | npm install -g opencode-ai |
| Context Ollama asks for | 64k+ (larger repos) | At least 64k | 64k+ (required) |
| Documented gaps | tool_choice, token counting, hosted web search not fully supported | Web search runs through Ollama (needs ollama signin) | Models only in opencode.json don't appear in the launch picker |

- **OpenCode** if you want an open-source tool (MIT) whose config is one JSON file you can commit. [Set it up with Ollama →](https://aliteq.com/how-to-use-ollama-with-opencode)
- **Codex CLI** if you want Apache-2.0 and plan to use OpenAI's open-weight gpt-oss models; Ollama's docs use them in every Codex example. [Set it up with Ollama →](https://aliteq.com/how-to-use-ollama-with-codex-cli)
- **Claude Code** if you already use it with Anthropic's models and want the same workflow offline, and you're fine with a proprietary licence and the API gaps Ollama lists. [Set it up with Ollama →](https://aliteq.com/how-to-use-ollama-with-claude-code)

## Claude Code vs OpenCode on a local model

This is the matchup people search for most, so here it is head-to-head. The short version: they're closer than you'd think once both are pointed at the same Ollama model. The differences are licensing, configuration and API coverage.

### Claude Code vs OpenCode (with Ollama)

|   | Claude Code | OpenCode |
| --- | --- | --- |
| Licence | Anthropic Commercial Terms | MIT |
| Talks to Ollama via | Anthropic Messages API | OpenAI-compatible API |
| Setup | ollama launch claude, or 3 env vars | ollama launch opencode, or opencode.json |
| Config you can commit to a repo | Settings + env vars | One opencode.json |
| Documented compatibility gaps | tool_choice, count_tokens, hosted web search | None listed by Ollama beyond the context requirement |
| Extras listed in Ollama's docs | Subagents, web search, vision, thinking, /loop | Subagents, web fetch, vision |
| Context Ollama asks for | 64k+ | 64k+ |

My take: if you're starting fresh on a local model, OpenCode is the lower-friction choice. It's open source, its Ollama setup is one config block, and Ollama doesn't list any API gaps for it. If you already work in Claude Code every day, the Ollama route lets you keep your habits offline. Just know that Ollama's [Anthropic compatibility page](https://docs.ollama.com/api/anthropic-compatibility) lists `tool_choice` and the token-counting endpoint as unsupported, so anything that relies on them will behave differently than against Anthropic's own API.

## Already using VS Code?

If you'd rather stay in the editor than live in a terminal, that's a different setup. Our [local AI coding assistant for VS Code](https://aliteq.com/local-ai-coding-assistant-vs-code) guide and the [Qwen3-Coder in VS Code](https://aliteq.com/local-ai-coding-assistant-vs-code) walkthrough cover the extension route. The same 64k context and VRAM maths above still applies. If you're weighing a local coder against a paid editor assistant, our [Qwen3-Coder vs GitHub Copilot](https://aliteq.com/qwen3-coder-vs-github-copilot) comparison sets them side by side.

To serve one Ollama box to several machines or agents at once, our guide to [running local AI as an API server](https://aliteq.com/how-to-run-local-ai-as-an-api-server) covers the network setup, and [how to speed up local LLM inference](https://aliteq.com/how-to-speed-up-local-llm-inference) helps once the agent feels slow on long contexts.

**Want a managed GPU for your coding agent** If you'd rather not manage a marketplace host, Runpod rents 48GB cards in its Secure Cloud datacenters with one-click templates. Install Ollama on the pod, set the 64k context, and point Claude Code, Codex or OpenCode at it. [Rent a 48GB A6000 by the hour](https://console.runpod.io/signup?ref=nuninlts) (Referral link: we earn a commission if you buy.) Live RTX A6000 prices on Runpod, from and typical: https://aliteq.com/runpod-pricing

This guide is part of our [how to run a local AI model on your own PC in 2026 — a genuinely simple beginner's guide](https://aliteq.com/how-to-run-a-local-llm-on-your-pc-beginners-guide) guide. Related guides: [OpenClaw vs Hermes Agent: which to run on a local model](https://aliteq.com/run-openclaw-hermes-local-model-ollama), [Should you use Ollama or LM Studio to run AI on your own PC?](https://aliteq.com/ollama-vs-lm-studio-which-should-you-use), [Open WebUI or LM Studio: which should you use to run local AI?](https://aliteq.com/open-webui-vs-lm-studio).

## Quick answers

**Q: Can Claude Code run on a local model?**

A: Yes, through Ollama's Anthropic-compatible API, added in Ollama v0.14.0. Run `ollama launch claude`, or set ANTHROPIC_AUTH_TOKEN=ollama, ANTHROPIC_API_KEY="" and ANTHROPIC_BASE_URL=http://localhost:11434, then `claude --model <model>` (Ollama docs, accessed Sep 25, 2026). Ollama lists tool_choice, token counting and hosted web search as not fully supported.

**Q: Why does my coding agent forget what it's doing with Ollama?**

A: Usually context length. Ollama's docs say coding tools need at least 64k tokens, but Ollama defaults to 4k on GPUs under 24 GiB and 32k on 24–48 GiB. Start the server with OLLAMA_CONTEXT_LENGTH=64000 and check the CONTEXT column in `ollama ps`.

**Q: What's the best local model for Claude Code, Codex or OpenCode on a 24GB GPU?**

A: Qwen3-Coder 30B-A3B is the usual pick. On our VRAM engine it needs about 24.0 GiB at 64k context (Q4_K_M, fp16 KV cache), right at a 24GB card's limit. Setting OLLAMA_KV_CACHE_TYPE=q8_0 brings it to about 21.0 GiB. On a 16GB card, gpt-oss 20B is the realistic option at about 15.6 GiB (14.1 GiB with the 8-bit KV cache).

**Q: Is OpenCode or Claude Code better with local models?**

A: On the same Ollama model the differences are licence and configuration, not intelligence. OpenCode is MIT-licensed and configured in one opencode.json, and Ollama lists no API gaps for it. Claude Code is proprietary, and Ollama's Anthropic compatibility layer doesn't fully support tool_choice or token counting.

**Q: Do I need a GPU at all?**

A: Not strictly. Ollama's docs point to cloud models (ollama.com/search?c=cloud) that run on Ollama's servers with no download. Or rent a GPU by the hour and run Ollama on it. Running a 30B coder at 64k context on CPU and system RAM alone will be very slow.

## Sources

- [Ollama docs — Claude Code integration (accessed Sep 25, 2026)](https://docs.ollama.com/integrations/claude-code)
- [Ollama docs — Codex CLI integration (accessed Sep 25, 2026)](https://docs.ollama.com/integrations/codex)
- [Ollama docs — OpenCode integration (accessed Sep 25, 2026)](https://docs.ollama.com/integrations/opencode)
- [Ollama docs — Context length (accessed Sep 25, 2026)](https://docs.ollama.com/context-length)
- [Ollama docs — FAQ (accessed Sep 25, 2026)](https://docs.ollama.com/faq)
- [Ollama docs — Anthropic compatibility (accessed Sep 25, 2026)](https://docs.ollama.com/api/anthropic-compatibility)
- [Ollama release notes v0.14.0 / v0.15.0](https://github.com/ollama/ollama/releases)
- [Ollama releases on GitHub (v0.40.16, Nov 9, 2026; read Nov 12, 2026)](https://github.com/ollama/ollama/releases)
- [OpenCode docs — Providers](https://opencode.ai/docs/providers/)
- [OpenAI Codex (GitHub, Apache-2.0)](https://github.com/openai/codex)
- [OpenCode (GitHub, MIT)](https://github.com/anomalyco/opencode)
- [Claude Code licence (GitHub)](https://github.com/anthropics/claude-code/blob/main/LICENSE.md)
- [Higgsfield plans and pricing (official page, checked Dec 2, 2026)](https://higgsfield.ai/pricing?fpr=lax-8e2251&fp_sid=run-coding-agents-on-local-models-ollama--md)
- [Illustration generated with Higgsfield](https://higgsfield.ai?fpr=lax-8e2251&fp_sid=run-coding-agents-on-local-models-ollama--md)

---

Canonical: https://aliteq.com/run-coding-agents-on-local-models-ollama

This page has referral links: we earn a commission if you buy, at no cost to you.
