How to Use LM Studio with OpenCode

April 15, 2026 by @anthonynsimon

OpenCode is an open-source, terminal-based coding agent similar to Claude Code or Cursor, but with support for local models. By pairing it with LM Studio, you can run a fully private coding assistant on your own hardware with no API keys or cloud dependencies.

This guide walks you through setting up LM Studio's local API server and connecting it to OpenCode.

What you'll need

  • LM Studio installed and running with a downloaded model. If you haven't set this up yet, follow the LM Studio guide first.
  • Node.js 18 or later (for installing OpenCode via npm), or use Homebrew.
  • A model with strong instruction-following and code generation abilities. Recommended:
RAM Model Why
16GB qwen3.5-9b Good all-rounder for code tasks
24GB devstral-2 Mistral's dedicated coding agent model, strong at tool use and multi-step edits
24GB+ qwen3-coder-next Qwen's dedicated coding model, optimized for code generation and editing
32GB+ qwen3.5-27b Excellent code generation and reasoning at 27B parameters
32GB+ gemma-4-31b Strong general-purpose model with large context window

Starting the LM Studio API server

LM Studio includes a built-in API server that is compatible with the OpenAI API format. OpenCode uses this to communicate with your local model.

  1. Open LM Studio.
  2. Load the model you want to use (click the Chat icon, then select a model from the dropdown).
  3. Click the Developer tab on the left sidebar.
  4. Toggle the server On. By default, it listens on http://localhost:1234.
  5. Note the model identifier shown in the server panel (e.g., qwen3.5-9b). You'll need this for the OpenCode configuration.

You can verify the server is running:

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

You should see a JSON response listing the loaded model.

Installing OpenCode

OpenCode can be installed via npm or Homebrew:

npm:

npm install -g opencode

Homebrew:

brew install opencode

Verify the installation:

opencode --version

Configuring OpenCode to use LM Studio

OpenCode needs to know where your local API server is and which model to use. Create or edit the opencode.json configuration file in your project root:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "lmstudio": {
      "api": "openai",
      "baseURL": "http://localhost:1234/v1",
      "models": {
        "qwen3.5-9b": {
          "max_tokens": 8192,
          "context_length": 32768
        }
      }
    }
  },
  "model": {
    "provider": "lmstudio",
    "model": "qwen3.5-9b"
  }
}

Replace qwen3.5-9b with the model identifier shown in your LM Studio server panel if you're using a different model.

Configuration notes

  • baseURL: Points to LM Studio's local server. Change the port if you've configured a different one.
  • max_tokens: Maximum number of tokens per response. Increase this for longer code generation, but keep it within your model's limits.
  • context_length: The context window size. Check your model's specs in LM Studio. Larger values let OpenCode send more file content but use more memory.

Using OpenCode

Navigate to your project directory and start OpenCode:

cd /path/to/your/project
opencode

OpenCode will launch an interactive terminal session. You can ask it to read, write, and modify files in your project, much like other AI coding assistants.

Tips for local models

Local models have less capacity than large cloud models, so a few adjustments help:

  • Be specific. Instead of "fix the bug," point to the file and describe the issue.
  • Work in small steps. Ask for one change at a time rather than large refactors across multiple files.
  • Use capable models. For complex code tasks, the 26B+ parameter models perform significantly better than smaller ones.
  • Keep context focused. Smaller context windows mean OpenCode may struggle with very large files. Break big files into smaller modules when possible.

Switching between local and cloud models

One of OpenCode's strengths is supporting multiple providers. You can configure both a local model and a cloud provider in the same opencode.json and switch between them:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "lmstudio": {
      "api": "openai",
      "baseURL": "http://localhost:1234/v1",
      "models": {
        "qwen3.5-9b": {
          "max_tokens": 8192,
          "context_length": 32768
        }
      }
    },
    "anthropic": {
      "models": {
        "claude-sonnet-4-6": {
          "max_tokens": 16384,
          "context_length": 200000
        }
      }
    }
  },
  "model": {
    "provider": "lmstudio",
    "model": "qwen3.5-9b"
  }
}

Switch to the cloud model by changing the model section, or use the model picker inside OpenCode's UI. This gives you a practical workflow: iterate locally for free during development, then switch to a more powerful cloud model when you need it.

Next steps

  • Try different models. Swap between smaller and larger models in LM Studio to find the best speed-vs-quality tradeoff for your workflow.
  • Use the local API in other tools. LM Studio's OpenAI-compatible server works with any tool that supports custom OpenAI endpoints, not just OpenCode.
  • Explore OpenCode's features. Check the OpenCode documentation for features like file watching, custom system prompts, and tool use.
  • Compare cloud GPU options. When you need more power than your local hardware can provide, check out cloud GPU prices and LLM API prices to find the best option.