You point OpenCode at a local Ollama model, ask it to read a file, and it tells you it has no access to files. Or it starts working on a feature nobody asked for. Or it loops on the same tool until it times out. No error anywhere.
We hit all three on a Windows 11 box with a 16 GB RTX 5060 Ti (OpenCode 1.19.5, Ollama 0.40.14). The model wasn't the problem, and neither was OpenCode. The model never saw the request.
The mismatch
Every OpenCode request carries a large preamble: the system prompt, the schema of every tool (read, glob, write, bash...), plus whatever your config adds, such as AGENTS.md, skills and MCP servers. In our runs a single request weighed about 23,000 tokens before the user message was even relevant.
Ollama picks a default context based on VRAM. Per its context length docs:
| VRAM | Default context |
|---|---|
| < 24 GiB | 4k |
| 24-48 GiB | 32k |
| >= 48 GiB | 256k |
Almost every consumer card lands in the first row. So you're sending ~23k tokens into a 4,096-token window, and Ollama doesn't reject the request. It trims it and carries on.
It's worse than 4,096, too. With a 4k context the log showed an input limit of 2,050-2,051 tokens, because roughly half is reserved for the output. The model got about 2k tokens out of 22k, less than a tenth of what OpenCode meant to send.
What that looked like in practice:
-
gemma4:12bstarted planning a "Remember Me" checkbox for a login form that doesn't exist, called its todo tool 65 times and ran into our 400-second timeout. -
qwen3.5:9banswered that it couldn't access files.
Confirm it in the log
Don't guess. The evidence is in the server log. With the tray app it lives at %LOCALAPPDATA%\Ollama\server.log. With ollama serve it goes to stderr.
At startup, look for the default Ollama chose:
msg="vram-based default context" total_vram="15.9 GiB" default_num_ctx=4096
And during the OpenCode session, look for this:
level=WARN source=llama_server.go:344 msg="truncating input prompt" limit=2051 prompt=22207 keep=5 new=2051
That warning showed up 66 times in a single test run. To pull it out in PowerShell:
$log = "$env:LOCALAPPDATA\Ollama\server.log"
(Select-String -Path $log -Pattern 'truncating input prompt').Count
Select-String -Path $log -Pattern 'n_ctx_slot = \d+' | Select-Object -Last 1
Select-String -Path $log -Pattern 'task\.n_tokens = \d+' | ForEach-Object { $_.Matches[0].Value }
The first line counts truncations (you want 0). The second shows the context the model was actually loaded with. The third lists the size of each request OpenCode sent.
Fix it on the Ollama side
The catch is that OpenCode talks to Ollama through the OpenAI-compatible endpoint (/v1), and Ollama's docs say that endpoint doesn't let you set the context per request. The num_ctx option you'd pass to /api/generate doesn't apply there. You have three ways to set it, and all of them take effect when the model loads, not mid-session.
1. Environment variable on the server process (the one we tested):
Get-Process -Name 'ollama', 'ollama app' -ErrorAction SilentlyContinue | Stop-Process -Force
$env:OLLAMA_CONTEXT_LENGTH = '32768'
ollama serve 2> C:\logs\ollama-serve.log
Killing the tray app first matters. If an Ollama is already running, it answers your requests and never sees the variable.
2. User environment variable, if you prefer the tray app. On Windows it inherits user variables:
[Environment]::SetEnvironmentVariable('OLLAMA_CONTEXT_LENGTH', '32768', 'User')
Quit Ollama from the tray and start it again from the Start menu.
3. Bake num_ctx into a model variant with a Modelfile. This is what Ollama's docs suggest for /v1 clients:
FROM gemma4:12b
PARAMETER num_ctx 32768
ollama create gemma4-12b-32k -f .\Modelfile
Then reference ollama/gemma4-12b-32k in OpenCode. You'll need one variant per model and size. We didn't run this path ourselves, so check ollama ps after you switch.
However you set it, verify with the model loaded:
ollama ps
The CONTEXT column should read 32768 and PROCESSOR should say 100% GPU.
One thing that confused us: with the variable set, the log still prints default_num_ctx=4096. That's the value Ollama would have picked on its own. The variable takes precedence. Trust n_ctx_slot and ollama ps, not that line.
Tell OpenCode the same number
Ollama needs the context, and OpenCode needs to know about it. Here is the minimal project-level opencode.json we used:
{
"$schema": "https://opencode.ai/config.json",
"model": "ollama/gemma4:12b",
"small_model": "ollama/gemma4:12b",
"enabled_providers": ["ollama"],
"provider": {
"ollama": {
"npm": "@ai-sdk/openai-compatible",
"name": "Ollama (local)",
"options": { "baseURL": "http://127.0.0.1:11434/v1" },
"models": {
"gemma4:12b": {
"name": "gemma4 12B local",
"tool_call": true,
"limit": { "context": 32768, "input": 28672, "output": 4096 }
}
}
}
}
}
Two details:
-
limit.contextdoesn't change anything in Ollama. It tells OpenCode when to compact the conversation. If you declare more than Ollama really has, OpenCode compacts too late and Ollama truncates silently again. If you're unsure, declare less. - Set
small_modelexplicitly. OpenCode uses it for things like session titles, and asmall_modelin your global config gets merged into the project. In one run, a cloudsmall_modelfrom our global config opened an HTTPS connection to that provider while the "local" agent was working.
How much context do you actually need?
The numbers we measured:
- With 32,768, nothing was truncated, the agent used its tools, and
gemma4:12bwrote the requested file in 27 seconds. - Requests grow with every turn. Across four requests for a single small task,
gemma4:12bwent from 22,207 to 22,919 tokens.qwen3.5:9bsent 23,004 and then 23,399 (each tokenizer counts differently). A real multi-file task grows much faster. - Most of the weight is your config. With an empty global config, the first request was 7,440 tokens. With a daily-driver config (AGENTS.md, skills, a browser MCP server), it was 23,569. Even the clean version doesn't fit in 4,096.
Ollama's docs recommend at least 64k for agents and coding tools, and the Ollama + OpenCode integration page asks for 64k or more. OpenCode's own docs suggest starting at 16k-32k if tools don't work. We only validated 32,768. Treat it as a floor that works, and go up if your VRAM allows.
What it costs
-
VRAM:
gemma4:12bat 32,768 sat around 8.4 GB while working with OpenCode. If you push the context past what fits, Ollama spills to system RAM and speed collapses. -
Generation speed: barely affected. On a long prompt at 32k we measured 86, 55 and 80 tokens/s for
qwen3.5:4b,qwen3.5:9bandgemma4:12b. -
First-request latency: reading OpenCode's first 22,207-token request took
gemma4:12b12.7 s. The next request reused the cache, so Ollama processed only 58 new tokens in 0.37 s. If you switch models, restart Ollama or let the model unload, you pay the 12.7 s again.
Checklist
- Kill every Ollama process, start it with
OLLAMA_CONTEXT_LENGTH=32768(or use anum_ctxModelfile). -
ollama psshows 32768 inCONTEXT. -
opencode.jsondeclares the same number inlimit.context, withlimit.inputbelow it, andsmall_modelpoints to the local model. - Zero
truncating input promptlines in the log since startup.
If all four pass and the agent still gets things wrong, the context isn't the problem anymore. A 9-12B model with a working context will use its tools, but it won't necessarily get the answer right. That's a separate problem.
Full measurements and configs: https://beautythings.net/en/guides/opencode-ollama-not-working/?s=devto
I wrote the whole setup up as a short book: https://beautythings.net/go/opencode-en?s=devto
Top comments (0)