Contenox

OpenCode with local models: Ollama, LM Studio, llama.cpp, vLLM

Updated

OpenCode with local models

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

  1. The Ollama app: move the context length slider in Settings to 64k or more.

  2. The server setting, for every model. On Windows, quit Ollama from the system tray, then in PowerShell:

    setx OLLAMA_CONTEXT_LENGTH 65536
    

    Start Ollama again from the Start menu. On macOS or Linux:

    OLLAMA_CONTEXT_LENGTH=65536 ollama serve
    
  3. A Modelfile, for one model. Create a file named Modelfile:

    FROM qwen3-coder:30b
    PARAMETER num_ctx 65536
    

    Then 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-coder and codellama don’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 CONTEXT with ollama ps and 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 models doesn’t match the Ollama tag. Copy it from ollama list, including the part after the colon.
  • Connection refused: Ollama isn’t running, or baseURL is missing /v1. Test it with curl http://localhost:11434/v1/models.
  • An Ollama model is missing from /models. Check ollama list and http://127.0.0.1:11434/api/tags on the machine running OpenCode. For a remote server, set providers.ollama.settings.baseURL to its API address ending in /v1. Embedding-only models are excluded.
  • Every answer takes minutes: ollama ps shows 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.1 by default. Set OLLAMA_HOST to 0.0.0.0:11434 on that machine, restart Ollama, and use its address in baseURL. 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