Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCode Configuration File

OpenCode Configuration File

OpenCode uses JSON format for configuration.

We can first look at the global config file ~/.config/opencode/opencode.json:


touch ~/.config/opencode/opencode.json

It contains model providers and configuration information:

{
  // JSON Schema (for editor validation and autocomplete)
  "$schema": "https://opencode.ai/config.json",

  "provider": {
    // Custom Provider name (use: deepseek/xxx)
    "deepseek": {

      // Using OpenAI compatible adapter (suitable for compatible interfaces like DeepSeek)
      "npm": "@ai-sdk/openai-compatible",

      "options": {
        // API base URL (must be OpenAI compatible format)
        "baseURL": "https://api.deepseek.com/v1",

        // API Key
        "apiKey": "sk-xxxx",

        // Whether to force set cache key (improve cache hit rate, reduce cost)
        "setCacheKey": true
      },

      "models": {
        // Local model alias (Used in CLI/TUI: deepseek/Deepseek-v4)
        "Deepseek-v4": {
          // Actual model name called (API layer)
          "name": "deepseek-v4-pro"
        }
      }
    }
  }
}

A minimal working configuration example

{
  "$schema": "https://opencode.ai/config.json",
  "model": "anthropic/claude-sonnet-4-5",
  "autoupdate": true,
  "theme": "default"
}

Common global config items:

Configuration Item Type Description
model string Default main model
small_model string Lightweight task model (e.g., title generation)
autoupdate boolean / "notify" Auto-update strategy
theme string UI Theme
provider object Model provider configuration

Provider example:

{
  "provider": {
    "anthropic": {
      "options": {
        "timeout": 600000,
        "setCacheKey": true
      }
    }
  }
}

Understanding the config "merge mechanism"

OpenCode's config doesn't overwrite, but ratherhierarchically merges。

Load order (from low to high)

Order Source Description
1 Remote .well-known/opencode Organization default config
2 Global configuration ~/.config/opencode/opencode.json
3 Custom path OPENCODE_CONFIG
4 Project configuration In the project opencode.json
5 .opencode/ directory extension capabilities
6 Inline config OPENCODE_CONFIG_CONTENT

Merge rules

Example

Global config:

{
  "autoupdate": true
}

Project config:

{
  "model": "anthropic/claude-sonnet-4-5"
}

Final result:

{
  "autoupdate": true,
  "model": "anthropic/claude-sonnet-4-5"
}

Project configuration

Create in the project root:

opencode.json

Function:

Example: project-level config

{
  "$schema": "https://opencode.ai/config.json",
  "model": "anthropic/claude-sonnet-4-5",
  "tools": {
    "write": true,
    "bash": true
  }
}

Core config modules explained

{
  "model": "anthropic/claude-sonnet-4-5",
  "small_model": "anthropic/claude-haiku-4-5"
}

Models and Provider

{
  "model": "anthropic/claude-sonnet-4-5",
  "small_model": "anthropic/claude-haiku-4-5"
}
Field Description
model Main task model
small_model Low-cost task model

Tool control

{
  "tools": {
    "write": false,
    "bash": false
  }
}
Tool Purpose
write Write to a file
bash Execute the command

This isThe first layer of security control。

Permission Control

{
  "permission": {
    "edit": "ask",
    "bash": "ask"
  }
}
Value Meaning
allow Automatic execution
ask Confirm every time
deny Forbid

Agent (core capability)

{
  "agent": {
    "code-reviewer": {
      "description": "代码审查",
      "model": "anthropic/claude-sonnet-4-5",
      "prompt": "You are a code reviewer",
      "tools": {
        "write": false
      }
    }
  }
}

Essence:Define roles + capability scope for the AI

Command (automated prompt)

{
  "command": {
    "test": {
      "template": "Run tests and fix failures",
      "description": "运行测试"
    }
  }
}

Essence:Prompt template system

TUI configuration

{
  "tui": {
    "scroll_speed": 3,
    "scroll_acceleration": {
      "enabled": true
    },
    "diff_style": "auto"
  }
}

Server config (API / Web)

{
  "server": {
    "port": 4096,
    "hostname": "0.0.0.0",
    "mdns": true
  }
}

Context compression (Token optimization)

{
  "compaction": {
    "auto": true,
    "prune": true,
    "reserved": 10000
  }
}

Plugin system

{
  "plugin": [
    "opencode-helicone-session",
    "@my-org/custom-plugin"
  ]
}

MCP (extension ecosystem)

{
  "mcp": {}
}

For integrating external tools (e.g., Jira, databases, etc.)

Instruction system (AI rules)

{
  "instructions": [
    "CONTRIBUTING.md",
    "docs/*.md"
  ]
}

Provider control

{
  "enabled_providers": ["anthropic"],
  "disabled_providers": ["openai"]
}

Priority:

disabled > enabled

Variable system

Environment variables:

{
  "model": "{env:OPENCODE_MODEL}"
}

File reference

{
  "apiKey": "{file:~/.secrets/key}"
}

Directory extension mechanism

OpenCode is not just JSON config; it also supports directory extensions:

.opencode/
├── agents/
├── commands/
├── plugins/
├── tools/
├── themes/

Function:


Recommended starter config

{
  "$schema": "https://opencode.ai/config.json",
  "model": "anthropic/claude-sonnet-4-5",
  "small_model": "anthropic/claude-haiku-4-5",
  "autoupdate": "notify",
  "tools": {
    "write": true,
    "bash": true
  },
  "permission": {
    "bash": "ask"
  },
  "compaction": {
    "auto": true,
    "prune": true
  }
}

Summary (architecture perspective)

You can understand OpenCode config as a four-layer structure:

配置层(多级 merge)
    ↓
能力层(agent / tools / mcp)
    ↓
执行层(model / provider)
    ↓
交互层(tui / server / web)

Essentially, it's not a simple config file, but a:

configuration center for a programmable AI Agent operating system