Skip to content
New: answer your agents from your phoneLearn more

Run local models with Ollama and OpenCode.

Ollama runs open models on your Mac, and OpenCode drives them as a coding agent with no per-token bill. Set the context length before anything else, and hand the hard tasks to a cloud model.

Local models vs cloud models for coding

A local model in Ollama has no per-token bill, and your code and prompts stay on the machine. It works offline, on a train or a plane.

You give up quality and speed. A Mac generates fewer tokens a second than a data centre, and the open models that fit in its memory trail the cloud models on big changes. Give a local model a contained task, such as a bug in one module or a missing test, and keep changes across many files for a cloud model.

Install Ollama

Install Ollama with Homebrew, or download the app from ollama.com/download:

$ brew install ollama

The app starts the Ollama server for you. With Homebrew, run ollama serve, or brew services start ollama to keep the server running in the background.

Ollama models for coding, by Mac memory

A coding agent needs a model that can call tools. The table lists ket's recommended models in its order of preference, with the smallest Mac each one is worth running on. Download sizes come from Ollama's library.

ModelDownloadSmallest Mac
qwen3.8:27b18 GB32 GB
qwen3.6:35b-a3b-coding23–24 GB64 GB
glm-4.7-flash19 GB32 GB
qwen3-coder:30b19 GB32 GB
gemma4:12b7.7–8 GB16 GB
gpt-oss:20b14 GB16 GB

Download one with ollama pull, and list the models you have with ollama list:

$ ollama pull qwen3-coder:30b

A model takes more memory than its download. Ollama also holds a cache for the context, and the cache grows with the context length you give it.

Set Ollama's context length first

Ollama picks a context length from your GPU memory: 4k tokens under 24 GB, 32k from 24 to 48 GB, and 256k from 48 GB up, according to Ollama's context length docs. At 4k, OpenCode's tool calls fail. Ollama's OpenCode guide asks for a context of 64k or more.

If you run ollama serve yourself, set the length as you start it:

$ OLLAMA_CONTEXT_LENGTH=64000 ollama serve

In the Ollama app, drag the context slider in its settings, or set the variable with launchctl setenv OLLAMA_CONTEXT_LENGTH 64000 and restart the app.

Run ollama ps after a model loads to check what it got. The CONTEXT column shows the length, and SIZE shows the memory it holds:

$ ollama ps
NAME          ID              SIZE      PROCESSOR    CONTEXT    RUNNER      UNTIL
qwen3:0.6b    7df6b6e09427    3.4 GB    100% GPU     40960      llamacpp    59 seconds from now

On our 64 GB Mac, Ollama loaded this small model at its full 40,960-token maximum, and a 522 MB download took 3.4 GB of memory. On a large model the cache for a 256k context can take more memory than the weights, so ask for 64k.

Connect OpenCode to Ollama

The quickest way is Ollama's launcher:

$ ollama launch opencode

It lists recommended local and cloud models, plus the ones you've downloaded, then starts OpenCode on the model you pick. The launcher passes the model as an inline config and leaves ~/.config/opencode/opencode.json untouched.

To use Ollama without the launcher, add it as a provider in ~/.config/opencode/opencode.json. This block follows Ollama's OpenCode guide. Use each model's name as ollama list prints it:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Ollama",
      "options": {
        "baseURL": "http://localhost:11434/v1"
      },
      "models": {
        "qwen3-coder:30b": {
          "name": "qwen3-coder:30b"
        }
      }
    }
  }
}

Start opencode, then pick the Ollama model with /models (ctrl+x m).

Run one local agent at a time

By default Ollama answers one request per model at a time (OLLAMA_NUM_PARALLEL is 1). Start a second agent on the same model and its requests wait behind the first one's.

You can raise the setting, but Ollama's FAQ says the memory a model needs scales with OLLAMA_NUM_PARALLEL × OLLAMA_CONTEXT_LENGTH. Two agents at 64k need room for two 64k contexts. A second model loads its own weights as well.

On one Mac, give the local model one task at a time and put parallel work on cloud agents. Each task still needs its own working directory; our guide to running agents in parallel shows how to split the work.

Price the saving from a real session

We priced a routine Claude Code session from our own logs. The task was a fix to ket's menus: give destructive items a red icon to match their red label. Claude Code made 28 requests to Opus 5.5 and 26 tool calls. The session ran on a subscription, so Claude Code wrote to the hour-long prompt cache.

TokensCountOpus 5.5 rate, per millionCost
Fresh input56$4under $0.01
Cache writes, one hour45,359$8$0.36
Cache reads1,531,955$0.20$0.31
Output9,729$20$0.19
Total1,587,099$0.86

At Anthropic's list prices the session costs $0.86, and the same counts at Sonnet 5.5's rates come to $0.43. Cache reads made up 96% of the tokens: Claude Code resends the whole conversation on each turn, and Anthropic bills each resent Opus 5.5 token at a twentieth of the input price. Run the same task on a local model and the token bill is zero; you pay for electricity and the wait.

On a Pro or Max plan you don't pay per token, so the saving comes as room under your usage limits. If a local model needs three attempts where a cloud model needs one, you lose the time those attempts took.

Hand the hard tasks to a cloud model

Send a task to the cloud when it spans many files or when the local model has failed at it twice.

Switch models mid-session

Run /models and pick a cloud model. OpenCode keeps the conversation, and the cloud model has none of it cached, so its first turn reads the whole conversation at the full input price. Switch at the start of a session, while the conversation is short.

Give each OpenCode agent its own model

Set a model per agent in opencode.json, in provider/model form. With this config the plan agent runs on Sonnet 5.5 and the build agent on the local model; press Tab to switch between them:

{
  "agent": {
    "plan": { "model": "anthropic/claude-sonnet-5-5" },
    "build": { "model": "ollama/qwen3-coder:30b" }
  }
}

A subagent can have its own model too. The local agent can hand it a task, or you can call it with @ and its name. OpenCode's agents docs cover the options.

Use Ollama cloud models

Ollama's cloud models have tags that end in cloud, such as gemma4:cloud or gpt-oss:20b-cloud. They run on Ollama's servers through the same local endpoint, so the setup above stays as it is. Sign in with ollama signin. Ollama's free plan runs on pay-as-you-go credits, and its paid plans start at $20 a month with $60 of usage credits. Ollama says it doesn't train on what you send. To keep Ollama local, set OLLAMA_NO_CLOUD=1.

Or let ket run your local models

ket runs OpenCode 2 on your Ollama models as local agents, beside whichever cloud agents you use, with each task in its own worktree.

  • Models that fit. Settings → Agents → On this Mac lists the models you've downloaded and offers downloads sized for your Mac. A model that can't run as an agent shows why: it runs in Ollama's cloud, it's on another machine, it can't call tools, or it's too big for this Mac.
  • The context, set for you. ket makes a copy of each model pinned to the longest context that fits your memory: 64k, then 32k, then 16k. The copy shares the original's files, so it takes no extra disk. On a 16 GB Mac the longest that fits can fall short of the 64k Ollama asks for, so keep local tasks small there.
  • Only Ollama. ket's config lets a local agent's OpenCode use the Ollama on your Mac and denies all other providers, so a subagent can't pick a cloud model either.
  • One task at a time. ket queues prompts for local agents while one of them works, and sends them in order.
  • Hand to the cloud. In a local agent's worktree menu, Hand to followed by your cloud agent's name opens that agent in a new tab of the same worktree and types a note: read git status and git diff, then continue the task. ket doesn't send the note, so nothing about the task leaves the Mac until you press Enter. The cloud agent starts a fresh session, and the diff is all it reads of the local work. The local tab and its changes stay where they are.

The docs cover local agents in more detail. To start OpenCode tasks and answer their prompts from your phone, see running OpenCode from your phone.