GuidesHosting choices

OpenCode with Ollama: setup, context length, remote host

Run OpenCode on local Ollama models: discovery in OpenCode 2, ollama launch opencode, the 64K context setting, which models to pull, and a remote Ollama.

November 23, 2026The Everpod team
The short answer

On OpenCode 2 (2.4.0 as of November 23, 2026), a local Ollama needs no configuration. OpenCode probes http://127.0.0.1:11434, adds every model Ollama can chat with to /models as ollama/<name>, and reads each one’s tool support and context size from Ollama. Pull a model that supports tools, such as ollama pull gemma4:12b, then set "model": "ollama/gemma4:12b" in opencode.json, or pick it in /models. In 2.4.0, /models shows only OpenCode’s free models unless a provider is connected with /connect; turning off the new designs brings the full list back (see the last table). Ollama’s own shortcut, ollama launch opencode, picks the model, offers to install OpenCode if it’s missing (its installer gives you OpenCode 1), and starts it with the limits filled in, without editing your config.

What decides whether the agent can actually use its tools is the context window. Ollama serves 4K tokens on a machine with less than 24 GiB of video memory, and its docs say OpenCode needs 64K or more. Start Ollama with OLLAMA_CONTEXT_LENGTH=64000 and give OpenCode the same figure as the model’s limit.context. For Ollama on another machine, set providers.ollama.settings.baseURL to http://<host>:11434/v1.

OpenCode 2 finds Ollama by itself

With Ollama running on the same machine, this is the whole setup:

ollama pull gemma4:12b
opencode models     # ollama/gemma4:12b is listed once discovery has run
opencode            # then /models, or Ctrl+X then M
Copied

In 2.4.0, if /models lists only free models, see the first row of the last table.

Per OpenCode’s providers docs, it “discovers completion models, and adds them to /models without an account connection.” In 2.4.0’s source it re-reads Ollama’s list every 30 seconds, so a model you pull appears without a restart, and it keeps the last list it got if Ollama is briefly down. Embedding-only models are left out. The model ID is exactly what Ollama reports, tag included: pull qwen3.8 with no tag and OpenCode lists ollama/qwen3.8:latest.

Two things come from Ollama’s metadata for each model, and both matter later. Tool support is taken from the model’s capabilities: a model Ollama does not list as tools is added with tools turned off, so it can answer questions but cannot read, edit or run anything. And the context limit is the model’s maximum, the .context_length value in its file, not the window Ollama is serving it with. To see what Ollama reports for a model:

curl -s http://127.0.0.1:11434/api/show -d '{"model": "gemma4:12b"}' | jq .capabilities
Copied

To make an Ollama model the default for every session, or for one project, set model in ~/.config/opencode/opencode.json or the project’s own file (where each file goes and which wins is in OpenCode’s config guide). For one headless run, opencode run --model ollama/gemma4:12b "Explain this repository". To stop OpenCode probing Ollama at all, add -opencode.provider.ollama to plugins.

If opencode --version starts with 1

Ollama’s manual instructions install OpenCode 1. As of November 23, 2026, curl -fsSL https://opencode.ai/install | bash installs the newest 1.x (1.19.6), and so does npm install -g opencode-ai; the install script itself notes that the opencode-ai package “only contains OpenCode 1”. OpenCode 2 is curl -fsSL https://opencode.ai/v2/install | bash or npm install -g @opencode/cli, and the install guide covers moving across. On version 1, Ollama goes in as a provider block that names each model, as Ollama’s page shows it. Adding a limit tells OpenCode the window you actually serve:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Ollama",
      "options": { "baseURL": "http://localhost:11434/v1" },
      "models": {
        "gemma4:12b": { "name": "Gemma 4 12B", "limit": { "context": 64000, "output": 8192 } }
      }
    }
  }
}
Copied

OpenCode 2 still reads this file; its migration page says “The V1 config format remains supported.”

The shortcut: ollama launch opencode

Ollama’s OpenCode page leads with one command, which opens a model picker and then OpenCode. In Ollama 0.40.17’s source, it does this:

ollama launch opencode --model gemma4:12b skips the picker, and ollama launch opencode --config sets things up without starting OpenCode. Models you defined only in opencode.json don’t appear in the launcher’s picker. With OpenCode 2 you need Ollama 0.40.12 or later; the 0.40.12 release note says “ollama launch now works with OpenCode v2”.

Context length: the setting tool calling depends on

OpenCode’s system prompt, its tool definitions and the files it reads all have to fit in the window Ollama serves. Ollama picks that window from the video memory it finds, per its context length page:

Video memoryOllama’s default window
Under 24 GiB, or none
4K tokens
24 to 48 GiB
32K tokens
48 GiB or more
256K tokens

Ollama’s OpenCode page says “OpenCode requires a context length of 64k or higher,” and the context page says coding tools “should be set to at least 64000 tokens.” OpenCode’s older version 1 docs are gentler, with a tip that says “If tool calls aren’t working, try increasing num_ctx in Ollama. Start around 16k - 32k.” OpenCode 2’s docs give no figure; 64K is Ollama’s, on its OpenCode page and in its announcement of the launcher. (Ollama’s FAQ gives a single default of 4,096 tokens, the table’s figure for smaller machines, and its Modelfile reference says 2,048, which the server’s own default no longer matches.)

OpenCode talks to Ollama’s OpenAI-compatible endpoint, and Ollama’s compatibility page is plain that “The OpenAI API does not have a way of setting the context size for a model.” So the window is set on Ollama’s side, in one of three ways. In the desktop app, use the context length slider in Settings. For the server as a whole, start it with OLLAMA_CONTEXT_LENGTH=64000 ollama serve, or, where Ollama runs as a systemd service on Linux, put it in an override:

sudo systemctl edit ollama.service
# in the editor, add:
#   [Service]
#   Environment="OLLAMA_CONTEXT_LENGTH=64000"
sudo systemctl daemon-reload
sudo systemctl restart ollama
Copied

For one model only, make a copy with its own window from a two-line Modelfile, then select the copy in OpenCode (it appears as ollama/gemma4-12b-64k:latest):

FROM gemma4:12b
PARAMETER num_ctx 64000
Copied
ollama create gemma4-12b-64k -f ./Modelfile
Copied

Then check what you got. After a request, ollama ps shows each loaded model’s window under CONTEXT and where it runs under PROCESSOR. Ollama advises against a model that spills onto the CPU, and a bigger window takes more memory: “Setting a larger context length will increase the amount of memory required to run a model.” If 64K pushes the model out of video memory, both pieces of Ollama’s advice point to a smaller model at 64K over a larger one at 4K.

Parallel work multiplies this. OpenCode 2.3 and later run subagents in the background alongside the main session, and Ollama handles one request per model at a time on most backends (OLLAMA_NUM_PARALLEL defaults to 1 except on Apple’s MLX), so the rest wait. Raising it has a cost the FAQ spells out: “a 2K context with 4 parallel requests will result in an 8K context and additional memory allocation.”

Tell OpenCode the window Ollama really serves

Setting Ollama’s window is half the job. OpenCode 2 budgets a session against the limit it read from Ollama’s metadata, which is the model’s maximum: 256K for gemma4:12b and qwen3.8:27b by Ollama’s library pages. Its compaction summarises older conversation “When a session approaches the model’s context limit”, so with Ollama serving 64K, that point never arrives before the real window is full. The report in issue #63899 (open as of November 23, 2026) describes the result: compaction never fires, and long answers end in “Response still truncated after two output token limit continuations”.

Reproductions in that issue, on OpenCode 2.2.6 and 2.2.7, found that a limit you set yourself wins over the discovered one, as long as the model ID is spelled exactly as Ollama reports it, tag included. Set the context to what Ollama serves, and the output to what the launcher would choose:

{
  "$schema": "https://opencode.ai/config.json",
  "model": "ollama/gemma4:12b",
  "providers": {
    "ollama": {
      "models": {
        "gemma4:12b": { "limit": { "context": 64000, "output": 8192 } }
      }
    }
  }
}
Copied

Run opencode reload to apply it without restarting OpenCode’s background server. If you change Ollama’s window later, change this figure with it.

Which models the makers point at

OpenCode 2’s docs name no Ollama models beyond the examples in their setup snippets, qwen3:8b and gemma3:4b. Ollama’s picks are the recommendations ollama launch puts in its model picker, a list it fetches from ollama.com. As read on November 23, 2026:

ModelRunsOllama’s descriptionWhat it needs, per Ollama
qwen3.8:27b
LocallyCoding, research, and long-horizon agentic tasksAbout 28 GB of video memory
gemma4:12b
LocallyAgentic workflows and multimodal reasoningAbout 11 GB of video memory
qwen3.5:4b
LocallyCompact multimodal reasoning and tool useAbout 6 GB of video memory
glm-5.3-flash:cloud
Ollama’s cloudFast reasoning for coding and agentic workloads with 1M context and native image understandingUsage credits or a paid plan
kimi-k3.1-pro:cloud
Ollama’s cloudLong-horizon agentic reasoning with multimodal tool useUsage credits or a paid plan
deepseek-v4.1-flash:cloud
Ollama’s cloudFast coding and agentic tool use with 1M context and native image understandingUsage credits or a paid plan

The list moves. Ollama’s January 2026 post introducing the launcher recommended glm-4.7-flash, qwen3-coder and gpt-oss:20b for local use, and gave one useful measure of the cost of a big window: “~23 GB VRAM required with 64000 tokens context length” for the first. Whatever you pick, the test is the one OpenCode applies: Ollama has to list it with tools, which is the filter Ollama’s OpenCode page links to. Whether a machine with that much memory is worth buying for an agent is weighed in local AI box vs cloud.

Ollama on another machine

A common setup is OpenCode on a laptop or a server and Ollama on a machine with a GPU. Ollama listens only on 127.0.0.1:11434 by default, and its authentication page says “The local API at http://localhost:11434 does not require authentication.” Anyone who can reach the port can use your models, so open it only on a private network. On a Linux GPU machine, the same systemctl edit ollama.service override takes the bind address:

[Service]
Environment="OLLAMA_HOST=0.0.0.0:11434"
Environment="OLLAMA_CONTEXT_LENGTH=64000"
Copied

0.0.0.0 means every interface, a public one included, so on a machine with a public address keep 11434 closed to the internet and reach it over something like Tailscale or your LAN. Then point OpenCode at it, with /v1 on the end; OpenCode works out Ollama’s /api/tags and /api/show from that address:

{
  "$schema": "https://opencode.ai/config.json",
  "providers": {
    "ollama": {
      "settings": { "baseURL": "http://gpu-box:11434/v1" }
    }
  }
}
Copied

Check from the machine that runs OpenCode with curl http://gpu-box:11434/api/tags, then opencode reload. OpenCode 2 takes the address from this setting, not from an OLLAMA_HOST variable in your shell. If the Ollama machine sits behind a proxy that wants a bearer token, add "apiKey": "{env:OLLAMA_API_KEY}" beside baseURL; OpenCode sends it to discovery and to every model request. Since 2.3.0, discovery waits up to 15 seconds per request, so slower remote and tunnelled endpoints list their models.

An SSH tunnel avoids opening the port at all. Leave Ollama on 127.0.0.1, run this on the OpenCode machine, and OpenCode’s default probe finds the remote models with no config:

ssh -N -L 11434:127.0.0.1:11434 you@gpu-box
Copied

That needs nothing of your own listening on port 11434 locally; if a local Ollama is, forward a different port and put it in baseURL. How the forward works is in opening a localhost app on a remote server. If OpenCode runs in a container or a VM, remember that 127.0.0.1 there is the container itself, not the machine Ollama runs on.

Ollama’s cloud models

Model names ending in :cloud run on Ollama’s service, not your hardware, after you sign in with ollama signin. They are “set to their maximum context length by default,” so the window problem above doesn’t apply, and ollama launch passes their limits to OpenCode for you. Ollama’s launcher marks the three in its current picks as needing “usage credits or a paid plan”: a pay-as-you-go account pays per token, and Pro is $20 a month with $60 of usage included, on Ollama’s pricing page as of November 23, 2026. OpenCode can also reach them with no local Ollama: its catalogue has an Ollama Cloud provider, connected with /connect and an API key from ollama.com’s key settings. Either way the prompts leave your machine; Ollama says of cloud prompts, “We do not use them to train models.”

When it doesn’t work

What you seeWhyWhat to do
/models offers only OpenCode’s free models, though opencode models lists your ollama/ ones (OpenCode 2.4.0)
2.4.0 turned the terminal’s new designs on by default, and that dialog counts only providers connected with /connect. An Ollama found by discovery or defined in opencode.json has no such connection. Fixed on OpenCode’s development branch on November 23, 2026 (#65793); at 12:40 UTC that day, 2.4.0 was still the latest releasePut "experimental": { "new-designs": false } in ~/.config/opencode/cli.json, or turn off New designs in the Experiments dialog; or set the model with model in opencode.json
No ollama/ models anywhere
Ollama isn’t answering where OpenCode looks, or the model doesn’t report the completion capabilitycurl http://127.0.0.1:11434/api/tags from the machine running OpenCode. With a custom baseURL, look for “failed to discover Ollama models” in ~/.local/share/opencode/log/opencode.log
The model chats but never reads, edits or runs anything
Ollama doesn’t list tools for it, so OpenCode turns tools off; or the window is too smallCheck capabilities with /api/show; choose a model tagged tools; raise the window to 64K
Long sessions lose earlier work, or end in “Response still truncated after two output token limit continuations”
OpenCode budgets against the model’s maximum while Ollama serves less, so compaction comes too lateSet limit.context to the served window, as above
Model unavailable: ollama/<model>
Discovery no longer returns it: removed from Ollama, renamed, or a resumed session started before discovery finished (#62711)Check ollama list; send the message again once OpenCode is up. Listing the model under providers.ollama.models keeps it in the catalogue from the start
ollama launch opencode fails with “Unrecognized flag: --model in command opencode” or “could not read OpenCode model limits”
An Ollama older than 0.40.12 launching OpenCode 2Update Ollama (0.40.17 is the latest as of November 23, 2026)
The first reply takes minutes, or requests time out
The model is loading, or part of it is on the CPU; OpenCode waits five minutes for a response to start, then retries up to three timesCheck PROCESSOR in ollama ps; use a smaller model or window; raise settings.headerTimeout (milliseconds) on the ollama provider

Run OpenCode on an always-on developer pod.

A developer pod is a permanent cloud computer (VPS), set up and secured for you, with your pick of Claude Code, Codex, OpenCode, Pi, Hermes and OpenClaw installed. Run more agents at once than your laptop can hold, and keep them working when it’s closed. From $24 a month, built in about ten minutes.