OpenCode: OpenCode Configuration File

Last updated: 2026-08-31

OpenCode manages all settings through the opencode.json configuration file, including models, agents, tools, themes, and more.

1. What You Will Learn


2. Configuration File Locations

OpenCode loads configuration in this priority order (later overrides earlier):

Priority Path Scope
1 ~/.config/opencode/opencode.json Global
2 <project>/.opencode/opencode.json Project
3 Environment variables Runtime override

3. Complete Configuration Structure

JSON
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {},
  "model": {},
  "agent": {},
  "tools": {},
  "permission": {},
  "theme": {},
  "tui": {}
}

(1) provider

Configure custom model providers:

JSON
{
  "provider": {
    "my-provider": {
      "name": "My Custom Provider",
      "baseURL": "https://api.example.com/v1",
      "apiKey": "sk-xxx"
    }
  }
}

(2) model

Set the default model:

JSON
{
  "model": {
    "default": "anthropic/claude-sonnet-4-20250514"
  }
}

(3) agent

Configure custom agents (see Lesson 13 for details):

JSON
{
  "agent": {
    "build": {
      "mode": "primary",
      "model": "anthropic/claude-sonnet-4-20250514"
    }
  }
}

(4) tools

Global tool toggles:

JSON
{
  "tools": {
    "write": true,
    "edit": true,
    "bash": true
  }
}

(5) permission

Fine-grained permission control:

JSON
{
  "permission": {
    "edit": "ask",
    "bash": {
      "*": "ask",
      "git status *": "allow",
      "grep *": "allow"
    }
  }
}

Permission values:

Value Description
allow Allow directly
ask Require user confirmation
deny Deny directly

(6) tui

Terminal UI settings:

JSON
{
  "tui": {
    "scroll_speed": 3,
    "scroll_acceleration": {
      "enabled": true
    }
  }
}

4. AGENTS.md (Project Rules)

AGENTS.md is the project-level rules file placed in the project root directory, defining AI behavior constraints.

(1) Creation

In TUI enter:

TEXT 📖 Display only
/init

Or manually create an AGENTS.md file.

(2) Content Example

MARKDOWN
# Project Rules

- Always use TypeScript for new files
- Follow ESLint configuration
- Write unit tests for all new functions
- Use conventional commits format

5. Environment Variables

Variable Type Purpose
OPENCODE_AUTO_SHARE boolean Auto-share sessions
OPENCODE_CONFIG string Custom config path
OPENCODE_TUI_CONFIG string TUI config path
OPENCODE_DISABLE_AUTOUPDATE boolean Disable auto-update
OPENCODE_SERVER_PASSWORD string API service auth password
OPENCODE_MODELS_URL string Custom model source

❓ FAQ

Q Which takes priority—global or project config?
A Project config. OpenCode loads by priority, later overrides earlier, ensuring project-level settings take precedence.
Q What's the difference between AGENTS.md and opencode.json?
A AGENTS.md describes rules in natural language that AI reads and follows; opencode.json defines structured config that the OpenCode program reads and executes. They complement each other.
Q Alice's team has 10 projects with different configs—how to manage?
A Set common defaults in global config, then project-specific settings in each project's .opencode/opencode.json. This way you maintain one global config + per-project differences.

📖 Summary


📝 Exercises

  1. Basic: Create a global opencode.json with your preferred model provider.

  2. Intermediate: Create a project-level .opencode/opencode.json with project-specific tool permissions.

  3. Advanced: Write an AGENTS.md defining coding standards and AI behavior constraints, then have OpenCode follow it.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏