OpenCode with local models: Ollama, LM Studio, llama.cpp, vLLM
Updated
OpenCode 2 discovers local models from Ollama, LM Studio and vLLM. Start one of those servers and open /models in OpenCode. A local installation needs no provider account. For llama.cpp or another OpenAI-compatible server, register the endpoint and model in your project’s opencode.jsonc. OpenCode providers
The config for each runtime#
| Runtime | baseURL |
Start it with | Where the context size is set |
|---|---|---|---|
| Ollama | http://localhost:11434/v1 |
Runs in the background after install | OLLAMA_CONTEXT_LENGTH, the app’s slider, or a Modelfile |
| LM Studio | http://127.0.0.1:1234/v1 |
lms server start, or Developer → Start server |
lms load <model> --context-length 65536 |
| llama.cpp | http://127.0.0.1:9931/v1 |
llama-server |
-c 65536 |
| vLLM | http://127.0.0.1:8000/v1 |
vllm serve with tool-call flags |
--max-model-len 65536 |
On the same machine, leave the addresses for Ollama, LM Studio and vLLM at their defaults. For a remote server, set providers.<id>.settings.baseURL to its reachable API address including /v1. Project settings go in opencode.jsonc or opencode.json. Configuration
Ollama#
Install Ollama and pull a model that fits your machine. This example uses the 30B Qwen3 Coder model. You can substitute a smaller tool-capable model and its exact tag.
ollama pull qwen3-coder:30b
ollama list
Start opencode in your project and open /models. Select the entry under Ollama that matches the downloaded tag. OpenCode checks Ollama’s native model inventory at 127.0.0.1:11434. The model must advertise the completion capability. Alternatively, ollama launch opencode lets you pick the model in Ollama’s launcher. Ollama integration
Why does OpenCode forget my request with Ollama?#
Check the context size and hardware fit first. Ollama’s default context depends on GPU memory:
| GPU memory | Ollama’s default context |
|---|---|
| Under 24 GB | 4k tokens |
| 24 to 48 GB | 32k tokens |
| 48 GB or more | 256k tokens |
The instructions, tool descriptions, source files and prior turns all take context. A small context window can leave too little space for the user’s request.
Ollama controls how much context its server allocates. Choose a size that fits your model and memory. Ollama recommends 64k or more for coding agents. Context length
-
The Ollama app: move the context length slider in Settings to 64k or more.
-
The server setting, for every model. On Windows, quit Ollama from the system tray, then in PowerShell:
setx OLLAMA_CONTEXT_LENGTH 65536Start Ollama again from the Start menu. On macOS or Linux:
OLLAMA_CONTEXT_LENGTH=65536 ollama serve -
A Modelfile, for one model. Create a file named
Modelfile:FROM qwen3-coder:30b PARAMETER num_ctx 65536Then create the model and select its new name under Ollama in OpenCode:
ollama create qwen3-coder-64k -f Modelfile
Then check it worked. Send one message in OpenCode and run:
ollama ps
CONTEXT should show the size you set, and PROCESSOR should say 100% GPU. If part of it runs on the CPU, the model plus its context doesn’t fit in GPU memory, and every turn gets slow. Use a smaller model, a shorter context, or a quantized KV cache (OLLAMA_KV_CACHE_TYPE=q8_0 halves its memory, per Ollama’s FAQ). The KV cache guide explains why context costs memory.
If you override the model’s limit.context in your OpenCode configuration, match it to the server setting. OpenCode uses that number to decide when to compact a long conversation.
LM Studio#
Load the model with a context size, then start the server:
lms load qwen/qwen3-coder-30b --context-length 65536
lms server start
Run lms ls to find the identifier for a model you downloaded. In the app you can set its context length when loading it and start the server under Developer. OpenCode discovers the running server on port 1234. Select the model using /models. For a server on another machine, set providers.lmstudio.settings.baseURL to its address ending in /v1. OpenCode local runtimes
llama.cpp#
llama-server can download a GGUF from Hugging Face and serve it. The -hf flag picks the Q4_K_M file by default:
llama-server -hf unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF -c 65536 --port 9931 --alias qwen3-coder
--alias gives the model a short name for the API. Tool calls work through the model’s chat template; llama.cpp’s Jinja template engine is on by default, so you don’t need extra flags for most models.
{
"providers": {
"llamacpp": {
"package": "@opencode/ai/providers/openai-compatible",
"settings": {
"baseURL": "http://127.0.0.1:9931/v1"
},
"models": {
"qwen3-coder": {
"capabilities": {
"tools": true
},
"limit": {
"context": 65536,
"output": 8192
}
}
}
}
},
"model": "llamacpp/qwen3-coder"
}
Use the port you passed to llama-server. The entry under models must match the alias it serves. Keep capabilities.tools enabled only for a model and chat template that support tool calling.
vLLM, on your own or a rented GPU#
OpenCode discovers a same-machine vLLM server on port 8000. It checks /health, then reads /v1/models. The discovery response does not identify tool support, so OpenCode initially disables tools for discovered vLLM models. For agent tasks, start a tool-capable model with its parser, then set the capability for that model in OpenCode. This example also sets a key for a shared server.
vllm serve Qwen/Qwen3-Coder-30B-A3B-Instruct \
--served-model-name qwen3-coder \
--enable-auto-tool-choice \
--tool-call-parser qwen3_xml \
--max-model-len 65536 \
--api-key YOUR_KEY
{
"providers": {
"vllm": {
"settings": {
"baseURL": "http://gpu-host:8000/v1",
"apiKey": "{env:VLLM_API_KEY}"
},
"models": {
"qwen3-coder": {
"capabilities": {
"tools": true
},
"limit": {
"context": 65536,
"output": 8192
}
}
}
}
}
}
Replace gpu-host with your server address. Set VLLM_API_KEY for the OpenCode server to the same value you supplied to vLLM. For a default local server without authentication, omit the settings object. Match limit.context to --max-model-len and the model entry to --served-model-name. vLLM setup and parser selection
Ollama Cloud, when the model doesn’t fit#
Ollama Cloud runs the selected model on Ollama’s servers. Sign in to Ollama, then use ollama launch opencode and choose a cloud model in the picker. Your prompts and any project context sent to the model then go to the hosted service. Ollama Cloud · Ollama’s OpenCode launcher
Does my local model support tool calls?#
OpenCode reads files, edits code and runs commands through tool calls. A model without them can chat but can’t do anything.
- Ollama: look for the tools tag on the model page, or filter the library to tools. Older coding models such as
deepseek-coderandcodellamadon’t have it. - LM Studio and llama.cpp: tool calls depend on the model’s chat template. LM Studio’s model page lists “Trained for tool use” under capabilities; for llama.cpp, check the model card.
- vLLM needs both the server’s tool-call parser and the OpenCode per-model capability shown above.
A quick test: in a new session, ask “list the files in this folder”. A working setup calls a tool and shows the result. A broken one prints a JSON blob, or a made-up file list.
OpenCode not working with Ollama: the fixes#
- It loses earlier instructions. Check the active
CONTEXTwithollama psand compare it with the size of the session. - “does not support tools”: the model has no tool support in Ollama. Switch to a model tagged tools.
- It prints tool calls as text instead of running them: the model’s tool-call output didn’t parse. This has been reported with Gemma 4 in OpenCode issue 20995; the reporter switched to
qwen3.5:9b. Update both Ollama and OpenCode first, then try another model. - “model not found”: the key under
modelsdoesn’t match the Ollama tag. Copy it fromollama list, including the part after the colon. - Connection refused: Ollama isn’t running, or
baseURLis missing/v1. Test it withcurl http://localhost:11434/v1/models. - An Ollama model is missing from
/models. Checkollama listandhttp://127.0.0.1:11434/api/tagson the machine running OpenCode. For a remote server, setproviders.ollama.settings.baseURLto its API address ending in/v1. Embedding-only models are excluded. - Every answer takes minutes:
ollama psshows a CPU share. The model and context don’t fit in GPU memory. - Ollama runs on another machine: it listens only on
127.0.0.1by default. SetOLLAMA_HOSTto0.0.0.0:11434on that machine, restart Ollama, and use its address inbaseURL. Anyone on that network can then use it.
Claude Code and Codex with Ollama#
The same Ollama server works for the other two big agents. The context rule is the same: 64k or more.
Claude Code talks to Ollama’s Anthropic-compatible API:
ollama launch claude --model qwen3-coder:30b
By hand, in PowerShell:
$env:ANTHROPIC_AUTH_TOKEN = "ollama"
$env:ANTHROPIC_API_KEY = ""
$env:ANTHROPIC_BASE_URL = "http://localhost:11434"
claude --model qwen3-coder:30b
On macOS or Linux, use export with the same three variables. The settings gotcha: a base URL or key in Claude Code’s settings files overrides your shell variables. If it still talks to Anthropic, run /status and fix the settings file. Anthropic doesn’t support Claude Code with non-Claude models, and Ollama’s Anthropic API lacks prompt caching, token counting and tool_choice, so some features won’t work.
Codex CLI uses Ollama’s Responses API:
ollama launch codex
Or codex --oss -m qwen3-coder:30b. --oss covers Ollama and LM Studio; set oss_provider = "ollama" in ~/.codex/config.toml so it stops asking which one. ollama launch codex --restore removes the profile the launcher created.
The full setup for all three agents is on the Ollama page. Which of them suits small local models best: best Ollama harness.
Contenox Harness#
In Contenox Harness, open Backends → Providers, choose Ollama / Ollama Cloud, keep http://127.0.0.1:11434 and select Connect. For vLLM, enter the server root, such as http://localhost:8000. Pick the discovered model in the chat’s Model chip. Ollama setup · vLLM setup
Related guides#
Best Ollama models for coding: which tag fits your GPUBest Ollama models for coding, by VRAMThe best Ollama model for coding at 8, 12, 16, 24 and 48+ GB VRAM and on Ollama Cloud: exact tags, tool support, context memory and the benchmarks behind them.
Best Ollama harness: OpenCode, Claude Code, Codex, Aider and others with small modelsBest Ollama harness for coding: Claude Code, OpenCode, CodexOllama defaults to 4K context even on a 16 GB GPU. Compare 9 coding harnesses and see which local models Contenox picks for 8 GB and 16 GB cards.
How to run local LLMs for coding: hardware and setup from scratchHow to run local LLMs for coding: hardware, setup, contextHow to run local LLMs for coding: which model fits your GPU, the Ollama setup, the context setting that breaks agents, and connecting Claude Code, Codex or OpenCode.
What is the KV cache?: why more context needs more memoryWhat is the KV cache? How much memory your context needsWhat is the KV cache, and why does a long context need so much GPU memory? The formula, worked examples for real coding models, a calculator, and how to shrink it.
vLLM: serve a model for a team or on a rented GPUSet up a vLLM coding agent on Windows, Mac or LinuxSet up a vLLM coding agent in Contenox. Start a server on Linux, Windows with WSL, or Apple Silicon, check its model and connect from the desktop.
Release notes with AIGenerate release notes with AI from git log, in one commandGenerate release notes with AI: pipe git log into Claude Code, Codex CLI or OpenCode, get grouped Markdown back, and run it in CI. Commands, prompt and the traps.
Coding agents on WindowsAI coding agents on Windows: native or WSL, agent by agentWhich AI coding agent runs natively on Windows and which needs WSL: install commands, shell, sandbox and config path for each, plus the Windows traps that bite.
Codex on WindowsCodex for Windows: install, desktop app and sandbox setupCodex for Windows, step by step: the desktop app, the CLI from PowerShell, npm or WSL, signing in, the Windows sandbox and its errors, and fixes for crashes.
Claude Code on WindowsClaude Code on Windows: install, setup and common fixesInstall Claude Code on Windows 11 from PowerShell, CMD or WinGet, pick Git Bash or PowerShell, decide on WSL, and fix PATH, proxy and duplicate-install errors.