Getting OpenCode to work with a local Ollama model by fixing the 4,096-token context
- Hardware
- Windows 11, 16 GB RTX 5060 Ti
- Models & engines
- OpenCode 1.19.5 talking to Ollama 0.40.14 through the OpenAI-compatible /v1 endpoint
gemma4:12b, qwen3.5:9b and qwen3.5:4b - Key gotcha
- Before the fix, gemma4:12b planned a 'Remember Me' checkbox for a login form that doesn't exist and called its todo tool 65 times; qwen3.5:9b said it couldn't access files
What they built
Davidrs pointed the OpenCode coding agent at local Ollama models on a Windows 11 box with a 16 GB RTX 5060 Ti (OpenCode 1.19.5, Ollama 0.40.14). The agent said it had no access to files, started building features nobody asked for, or looped on one tool until it timed out, with no error anywhere.
The cause was a mismatch: every OpenCode request carries the system prompt and every tool schema, about 23,000 tokens in their runs, while Ollama picks a 4,096-token default on GPUs under 24 GiB and trims the request without rejecting it. The post shows how to confirm this in the server log and how to fix it on both sides.
Setup
- Windows 11, 16 GB RTX 5060 Ti
- OpenCode 1.19.5 talking to Ollama 0.40.14 through the OpenAI-compatible /v1 endpoint
- gemma4:12b, qwen3.5:9b and qwen3.5:4b
How it works
- With a 4k context the log showed an input limit of about 2,050 tokens, since roughly half is reserved for output, so the model saw under a tenth of the request
- Search server.log for 'truncating input prompt' (66 times in one test run) and check n_ctx_slot for the real loaded context
- The /v1 endpoint can't set context per request, so set OLLAMA_CONTEXT_LENGTH on the server process, as a user environment variable for the tray app, or bake num_ctx into a Modelfile variant
- Tell OpenCode the same number with limit.context in opencode.json, and set small_model to the local model
Results & numbers
- With 32,768 tokens nothing was truncated, the agent used its tools, and gemma4:12b wrote the requested file in 27 seconds
- gemma4:12b at 32k sat around 8.4 GB of VRAM; generation speed was barely affected (86, 55 and 80 tokens/s for qwen3.5:4b, qwen3.5:9b and gemma4:12b)
- The first 22,207-token request took gemma4:12b 12.7 s to read; the next one reused the cache and processed 58 new tokens in 0.37 s
Gotchas
- Before the fix, gemma4:12b planned a 'Remember Me' checkbox for a login form that doesn't exist and called its todo tool 65 times; qwen3.5:9b said it couldn't access files
- Kill the tray app first, or the already-running Ollama answers and never sees the variable
- The log still prints default_num_ctx=4096 even with the variable set; trust n_ctx_slot and ollama ps
- Declaring more context in OpenCode than Ollama really has makes it compact too late, and Ollama truncates silently again
- A cloud small_model in the global config was merged into the project and opened an HTTPS connection to that provider while the 'local' agent worked
- Most of the request is your config: an empty global config gave 7,440 tokens, a daily-driver one with AGENTS.md, skills and a browser MCP server 23,569
“The model wasn't the problem, and neither was OpenCode. The model never saw the request.”— Davidrs
Do it yourself
- Stop every Ollama process, including the tray app
- Start Ollama with OLLAMA_CONTEXT_LENGTH=32768, or create a model variant with PARAMETER num_ctx 32768
- Run ollama ps and check CONTEXT reads 32768 and PROCESSOR says 100% GPU
- In opencode.json, set limit.context to the same number with limit.input below it, and point small_model at the local model
- Confirm there are zero 'truncating input prompt' lines in the log since startup
$env:OLLAMA_CONTEXT_LENGTH = '32768'
ollama serve 2> C:\logs\ollama-serve.log
# then check
ollama psSimilar setups
OpenCode on a CPU-only Intel i7 laptop talking to local Ollama coder models
Idada Osaigbovo James wires OpenCode on a Dell Latitude 5430 (i7-1265U, 32 GB RAM, Iris Xe) to Ollama’s local OpenAI-compatible API and runs Qwen2.5-Coder 1.5B/7B with no cloud subscription.
PatchPal TinyAgent: a coding agent on a 4B model and a 6GB laptop GPU
WiseProbe adapted its PatchPal coding agent to run Gemma 4 E4B or Qwen3.5 4B through Ollama on a 6GB RTX A1000, using handoff-note compaction and capped file reads to work inside a 16K or 32K window.
Local AI: a one-click Windows app that picks a model for your PC and sets up Ollama for friends
Tired of setting up local LLMs for friends, a developer built a free MIT-licensed Windows app that detects your GPU and RAM, installs Ollama without admin rights, pulls a fitting model and opens a private chat.
A €200 mini PC as an offline AI box: a week with Gemma and Qwen
Diogo Soares ran Gemma 4 E4B and Qwen2.5-Coder 7B on a cheap NiPoGi P1 mini PC firewalled off the internet, used it from his Mac for a week, and measured what was fast and what wasn't.