Skip to content
Aventail™ co-founder speaks to BBC Radio Wales on AI safety and AI agents.Read the story
Aventail™
All guides

OpenCode + LM Studio: run OpenCode on a local model

13 min read

Run OpenCode on a local model with LM Studio: automatic discovery in OpenCode 2, the opencode.json provider block, context length, models and known limits.

OpenCode can run against a model served by LM Studio's local server on your own machine. OpenCode 2 finds an LM Studio server at http://127.0.0.1:1234 by itself and lists its models under /models with the lmstudio provider ID, so there is no provider block to write and no API key to enter. On OpenCode 1 you add a short provider block to opencode.json.

You need LM Studio with its server running, a model trained for tool use, a context length of about 64K tokens, and 12 GB of memory or more depending on the model. This guide covers the server, the context setting, automatic discovery, the manual opencode.json block, how to check that requests stay local, the known limits, and how to roll the same setup out to a team with Aventail™ Connect.

What you need

Minimum

Comfortable

LM Studio

0.4.0 for OpenCode 2 discovery, which reads LM Studio's /api/v1/models endpoint

Latest release: 0.4.28 on 15 November 2026

OpenCode

OpenCode 1 with a manual provider block

OpenCode 2 for automatic discovery; 2.3.0 was released on 13 November 2026

Context length

More than about 25K tokens, the floor LM Studio gives for coding agents

64K tokens or more

Memory

12 GB for openai/gpt-oss-20b

16 to 20 GB for the 24B to 35B models, plus room for context

Disk

12 to 20 GB per model

Space for two or three models

Platforms

Apple silicon Mac on macOS 14 or newer, Windows x64 or ARM, Linux x64 or ARM64

16 GB of RAM or more, as LM Studio recommends

LM Studio is free for use at home and at work. OpenCode is open source under the MIT licence. LM Studio has no OpenCode page in its integration docs on 15 November 2026; the setup below comes from OpenCode's docs and LM Studio's server docs.

Step 1: install LM Studio and download a model

Download the installer from lmstudio.ai/download. On a server or a VM without a desktop, install the headless daemon instead:

curl -fsSL https://lmstudio.ai/install.sh | bash
lms daemon up

The lms command line tool ships with LM Studio. Run the app once before you use it.

OpenCode reads files, edits them and runs commands through tool calls. LM Studio says all models support some degree of tool use, but models with native support, marked with a hammer badge in the app, generally perform better. Every model below is marked "Trained for tool use" in the LM Studio catalogue on 15 November 2026.

Model

Download size

Context window

Minimum system memory

Capabilities

openai/gpt-oss-20b

12 GB

131K

12 GB

Tool use, reasoning

qwen/qwen3-coder-30b

15 GB

256K

15 GB

Tool use

mistralai/devstral-small-2-2512

16.3 GB

256K

16 GB

Tool use, vision

zai-org/glm-4.7-flash

16 GB

128K

16 GB

Tool use, reasoning

google/gemma-4-26b-a4b

15.6 GB

256K

16 GB

Tool use, vision, reasoning

qwen/qwen3.6-35b-a3b

20.4 GB

Not stated on the catalogue page

20 GB

Tool use, vision, reasoning

qwen/qwen3-coder-next

42 GB

256K

42 GB

Tool use

A larger context adds to the minimum memory figure, so check the estimate in step 2 first.

lms get qwen/qwen3-coder-30b

Step 2: set the context length

The context window in the table is the most the model supports. The length it runs with is a load setting in LM Studio. Preview the memory a model needs at a given context before you load it:

lms load --estimate-only qwen/qwen3-coder-30b --context-length 64000

Load the model with that context:

lms load qwen/qwen3-coder-30b --context-length 64000

To make it stick, set the model's default load settings: open My Models, click the gear icon next to the model and set the context size. LM Studio says these defaults are used when the model is loaded anywhere in the app, including through lms load.

LM Studio's guidance for coding agents is more than about 25K tokens. Ollama's docs put the figure for coding tools at 64K. Use 64K if it fits.

Step 3: start the local server

From the app, open the Developer tab and switch on Start server. From a terminal:

lms server start --port 1234

The server binds to 127.0.0.1 by default, so only this machine can reach it. Three server settings matter here:

  • Just in Time Model Loading is on by default: a request for a model that is not loaded makes LM Studio load it. Load the model yourself first, as in step 2, so the context length is the one you chose.

  • Auto-Evict is on by default. At most one on-demand model stays in memory, so switching models in OpenCode unloads the previous one.

  • Require Authentication is off by default. Turn it on, and create an API token, if you serve on the local network.

Step 4: install OpenCode

OpenCode's docs give these commands for version 2:

curl -fsSL https://opencode.ai/v2/install | bash

npm install -g @opencode/cli

On Windows, OpenCode says Windows package managers are not supported. Download the standalone binary or the desktop app from the OpenCode docs.

Step 5: let OpenCode 2 find LM Studio by itself

With the LM Studio server running, start OpenCode:

opencode

Open /models. OpenCode 2 discovers LM Studio's language models and lists them under the lmstudio provider. It refreshes the list in the background and reads context, vision and tool-use capabilities from LM Studio.

To make a local model the default for new sessions, put this in ~/.config/opencode/opencode.json or in opencode.json in the project:

{
"$schema": "https://opencode.ai/config.json",
"model": "lmstudio/qwen/qwen3-coder-30b"
}

The part after lmstudio/ is the LM Studio model key, as lms ls prints it.

If LM Studio runs on another host or port, or needs a token, set its OpenAI-compatible URL. Models are still discovered automatically:

{
"$schema": "https://opencode.ai/config.json",
"providers": {
"lmstudio": {
"settings": {
"baseURL": "http://127.0.0.1:5678/v1",
"apiKey": "{env:LMSTUDIO_API_KEY}"
}
}
}
}

Leave apiKey out when LM Studio authentication is off. To turn discovery off, add "plugins": ["-opencode.provider.lmstudio"].

Step 6: or write the provider block by hand

Use this on OpenCode 1. This is the block from the OpenCode 1 docs:

{
"$schema": "https://opencode.ai/config.json",
"provider": {
"lmstudio": {
"npm": "@ai-sdk/openai-compatible",
"name": "LM Studio (local)",
"options": {
"baseURL": "http://127.0.0.1:1234/v1"
},
"models": {
"google/gemma-3n-e4b": {
"name": "Gemma 3n-e4b (local)"
}
}
}
}
}

Replace the model key with the one you loaded, for example qwen/qwen3-coder-30b. Three things to know about this block:

  • It is the OpenCode 1 format. OpenCode 2 still reads it. In the native OpenCode 2 format provider becomes providers, npm becomes package, and options.baseURL becomes settings.baseURL.

  • The base URL ends in /v1. That is LM Studio's OpenAI-compatible API.

  • Each model key must match an LM Studio identifier. OpenCode 1 lists the models you put in this map.

On OpenCode 2 you can also state a model's limits yourself. The models map adds or overrides entries, and limit holds the context and output token limits:

{
"$schema": "https://opencode.ai/config.json",
"providers": {
"lmstudio": {
"models": {
"qwen/qwen3-coder-30b": {
"limit": {
"context": 64000,
"output": 8192
}
}
}
}
}
}

LM Studio reports two numbers for a model: the maximum context it supports and the context length of the loaded instance. OpenCode's docs do not say which one discovery uses. If OpenCode works from 256K while LM Studio loaded 64K, it compacts too late.

Step 7: check it is running locally

  1. Confirm the server lists your model:

    curl http://localhost:1234/v1/models

  2. Run lms ps while OpenCode is answering. lms log stream shows the prompts that reach the model.

  3. In OpenCode, open /models and confirm the active model starts with lmstudio/. If you never set a default, OpenCode falls back to the newest available supported model, which can be one of OpenCode's free hosted models.

  4. Make LM Studio the only provider OpenCode may use. OpenCode 2 policies are evaluated in order and the last match wins:

    {
    "$schema": "https://opencode.ai/config.json",
    "policies": [
    { "action": "provider.use", "resource": "*", "effect": "deny" },
    { "action": "provider.use", "resource": "lmstudio", "effect": "allow" }
    ]
    }

  5. Set "update": "disable" in the global config if you want OpenCode to skip update checks.

  6. Unplug the network and ask OpenCode to explain a file. If it answers, the model is local.

LM Studio states that running a local server does not need the internet. OpenCode's web search is a separate path and asks before the first search.

What works and what does not on a local model

Works: chat, reading and editing files, running commands, and the agent loop, with a model that handles tool calls. LM Studio's OpenAI-compatible API supports tools, streaming, token usage in the stream, and the tool_choice values auto, none and required (required on llama.cpp models only).

Known limits:

  • Tool calling depends on the model. Models with native tool support in LM Studio generally do better. For the rest, LM Studio falls back to a default tool format and says results will vary by model.

  • Context is set in LM Studio. The OpenAI-compatible endpoints cannot set the context length in a request; only LM Studio's native chat endpoint can. Set it at load time, as in step 2.

  • Compaction comes sooner. OpenCode summarises the conversation when it nears the context limit and keeps about 15,000 recent tokens. It is on by default and it is lossy.

  • No prompt-cache controls. OpenCode places cache breakpoints for providers that need them, such as Anthropic. LM Studio's OpenAI-compatible docs describe no cache settings. On a local model the cost of a long conversation is time.

  • Model swaps take time. With Auto-Evict on, choosing a different model in OpenCode unloads the first and loads the second. An on-demand model is also unloaded after 60 idle minutes by default.

  • Speed and timeouts. Neither vendor publishes tokens per second for this pairing. OpenCode waits up to five minutes for response headers and fifteen minutes between streamed chunks, then retries up to three times. If a large model takes longer than that to load and start answering, raise settings.headerTimeout on the provider.

Running the model on another machine

Point OpenCode at another LM Studio server with settings.baseURL, as in step 5, and turn on authentication on that server. Or use LM Studio's LM Link, which keeps the API at localhost:1234 on your laptop while the model runs on another device you own. Either way the request leaves the laptop. Aventail™'s equivalent, in beta, is peer-to-peer AI routing.

The same setup for a team with Aventail™ Connect

The steps above suit one developer who manages their own machine. They give an IT or security lead no record of which machine sent which request where, and no rule that a developer cannot edit back in opencode.json.

Aventail™ Connect configures supported apps to send their model requests through Aventail™ Link, the inference engine and gateway that runs on each machine. Routing rules are set in Aventail™ Control and evaluated on the device. OpenCode, Claude Code, Claude Desktop and Codex are supported. In this setup Link serves the model itself, in place of LM Studio.

  1. Sign in to Control at control.aventail.co.uk and choose Connect a device.

  2. Install Link on each machine: desktop installers for macOS (Apple silicon), Windows x64 and Linux x64, or unattended:

    curl -fsSL https://get.aventail.co.uk/install.sh | AVENTAIL_REGISTRATION_KEY='YOUR_REG_KEY' sh

  3. Serve a model. In Models, download a coding model and choose Start serving. Check that it supports coding tools and has enough context.

  4. Connect OpenCode. In Connected apps, choose Connect next to OpenCode. There is no provider entry, endpoint or key to write. OpenCode 2 picks the settings up while running; OpenCode 1 needs a restart. Review any provider you added to OpenCode by hand so it does not conflict.

  5. Check where it ran. Send a harmless prompt, then open Requests in Control.

  6. Write the routing rule. The first matching enabled rule wins. For a local-only team, permit only On device; if local serving is unavailable, the request fails with an error.

Disconnect restores the settings Link changed. The Free plan covers three connected devices, including business use. Starter is £50 a month excluding VAT and includes ten devices. Routing policies need Control for Enterprise.

Which path to take

OpenCode + LM Studio by hand

OpenCode with Aventail™ Connect

Who it suits

One developer on their own machine

A team whose code must stay on approved models

Setup

LM Studio server plus discovery or opencode.json

Connect button in Link, rules in Control

Who picks the model

The developer, in /models

The developer in Link, within the routing rule

Where requests go

Wherever the OpenCode config says

Where the routing rule says, evaluated on the device

Record of requests

Local LM Studio logs on that machine

Requests view in Control

If the local model is down

Developer picks another provider

Fails with an error, or falls back to an approved provider

Cost

Free

Free for three devices; £50 a month for ten

Frequently asked questions

Does OpenCode work with LM Studio?
Yes. OpenCode 2 discovers an LM Studio server at http://127.0.0.1:1234 and lists its models under the lmstudio provider. OpenCode 1 needs a provider block that points at http://127.0.0.1:1234/v1.

Do I need an API key for LM Studio in OpenCode?
No, unless you turned on Require Authentication in LM Studio. In that case create an API token in LM Studio and pass it as apiKey in the provider settings.

Why does OpenCode show no LM Studio models?
Check that the server is running with lms server status and that curl http://localhost:1234/v1/models returns your models. On OpenCode 1, check that each model key in opencode.json matches the LM Studio identifier. If LM Studio uses another port, set baseURL.

Which LM Studio model is best for OpenCode?
On 16 GB, openai/gpt-oss-20b. With more memory, qwen/qwen3-coder-30b or mistralai/devstral-small-2-2512. Our guide to the best local LLMs for coding compares them.

Why does OpenCode chat but not edit files with my LM Studio model?
The usual causes are a model without native tool support and a context length that is too small. Pick a model marked "Trained for tool use", load it with a larger context, and try again.

Should I use LM Studio or Ollama with OpenCode?
OpenCode 2 discovers both. Ollama adds a one-command launcher. The differences are covered in LM Studio vs Ollama and in our guide to OpenCode + Ollama.

Sources

  1. OpenCode, Providers: https://opencode.ai/docs/providers/

  2. OpenCode, Models: https://opencode.ai/docs/models/

  3. OpenCode, Intro and install: https://opencode.ai/docs/

  4. OpenCode, Config: https://opencode.ai/docs/config/

  5. OpenCode, Migrate from V1: https://opencode.ai/docs/migrate-v1/

  6. OpenCode, Policies: https://opencode.ai/docs/policies/

  7. OpenCode, Compaction: https://opencode.ai/docs/compaction/

  8. OpenCode, Web search: https://opencode.ai/docs/websearch/

  9. OpenCode 1 docs, Providers: https://opencode.ai/v1/docs/providers

  10. OpenCode changelog, v2.3.0: https://opencode.ai/changelog

  11. LM Studio, Integrations: https://lmstudio.ai/docs/integrations

  12. LM Studio, OpenAI compatibility endpoints: https://lmstudio.ai/docs/developer/openai-compat

  13. LM Studio, REST API overview and list models: https://lmstudio.ai/docs/developer/rest and https://lmstudio.ai/docs/developer/rest/list

  14. LM Studio, API changelog: https://lmstudio.ai/docs/developer/api-changelog

  15. LM Studio, Local server, server settings and authentication: https://lmstudio.ai/docs/developer/core/server, https://lmstudio.ai/docs/developer/core/server/settings and https://lmstudio.ai/docs/developer/core/authentication

  16. LM Studio, Headless mode and JIT loading: https://lmstudio.ai/docs/developer/core/headless

  17. LM Studio, Idle TTL and Auto-Evict: https://lmstudio.ai/docs/developer/core/ttl-and-auto-evict

  18. LM Studio, lms CLI, lms load, lms get, lms ps and lms server start: https://lmstudio.ai/docs/cli

  19. LM Studio, Per-model defaults: https://lmstudio.ai/docs/app/advanced/per-model

  20. LM Studio, Tool use: https://lmstudio.ai/docs/developer/openai-compat/tools

  21. LM Studio, Claude Code integration (context guidance): https://lmstudio.ai/docs/integrations/claude-code

  22. LM Studio, LM Link with integrations: https://lmstudio.ai/docs/integrations/lmlink

  23. LM Studio, System requirements and offline operation: https://lmstudio.ai/docs/app/system-requirements and https://lmstudio.ai/docs/app/offline

  24. LM Studio catalogue: gpt-oss, qwen3-coder, devstral-2, glm-4.7, gemma-4, qwen3.6, qwen3-coder-next at https://lmstudio.ai/models; download page (version 0.4.28): https://lmstudio.ai/download

  25. LM Studio blog, LM Studio is free for use at work (8 July 2025): https://lmstudio.ai/blog/free-for-work

  26. Ollama, Context length: https://docs.ollama.com/context-length

  27. Aventail™, Connect overview: https://docs.aventail.co.uk/docs/connect/overview

  28. Aventail™, Initial setup: https://docs.aventail.co.uk/docs/getting-started/initial-setup

  29. Aventail™, Routing: https://docs.aventail.co.uk/docs/fleet/routing

  30. Aventail™, Pricing: https://aventail.co.uk/#pricing

All sources were read on 15 November 2026.

We use optional cookies and tracking to identify visiting companies, load our contact form, measure Google Ads download clicks and understand page use with PostHog on our homepage, Connect and How it works pages, Ollama and LM Studio comparison guides and campaign pages. Cookie policy