配置 Config
使用 OpenCode JSON 配置。
你可以使用 JSON 配置文件配置 OpenCode。
格式 Format
OpenCode 支持 JSON 和 JSONC(带注释的 JSON)两种格式。
{ "$schema": "https://opencode.ai/config.json", // Theme configuration "theme": "opencode", "model": "anthropic/claude-sonnet-4-5", "autoupdate": true,}位置 Locations
你可以将配置放在几个不同的位置,它们具有不同的优先顺序。
配置文件合并在一起,而不是替换。来自以下配置位置的设置将合并。较后的配置仅覆盖冲突键的较早配置。所有配置中的非冲突设置都将保留。
例如,如果你的全局配置设置 theme: "opencode" 和 autoupdate: true,而你的项目配置设置 model: "anthropic/claude-sonnet-4-5",最终配置将包括所有这三个设置。
优先级顺序
配置源按此顺序加载(较后的源覆盖较早的源):
- 远程配置 (from
.well-known/opencode) - 组织默认值 - 全局配置 (
~/.config/opencode/opencode.json) - 用户偏好 - 自定义配置 (
OPENCODE_CONFIG环境变量) - 自定义覆盖 - 项目配置 (
opencode.json在项目中) - 项目特定设置 .opencode目录 - 智能体, 命令, 插件- 内联配置 (
OPENCODE_CONFIG_CONTENT环境变量) - 运行时覆盖
这意味着项目配置可以覆盖全局默认值,而全局配置可以覆盖远程组织默认值。
远程 Remote
组织可以通过 .well-known/opencode 端点提供默认配置。当你使用支持它的提供商进行身份验证时,会自动获取此配置。
远程配置首先加载,作为基础层。所有其他配置源(全局、项目)都可以覆盖这些默认值。
例如,如果你的组织提供了默认禁用的 MCP 服务器:
{ "mcp": { "jira": { "type": "remote", "url": "https://jira.example.com/mcp", "enabled": false } }}你可以在本地配置中启用特定的服务器:
{ "mcp": { "jira": { "type": "remote", "url": "https://jira.example.com/mcp", "enabled": true } }}全局 Global
将全局 OpenCode 配置放在 ~/.config/opencode/opencode.json 中。将全局配置用于用户范围的偏好设置,如主题、提供商或键绑定。
全局配置覆盖远程组织默认值。
项目级 Per project
在项目根目录中添加 opencode.json。项目配置在标准配置文件中具有最高优先级 - 它覆盖了全局和远程配置。
当 OpenCode 启动时,它会在当前目录中查找配置文件,或向上遍历到最近的 Git 目录。
这也可以安全地提交到 Git,并使用与全局配置相同的架构。
自定义路径 Custom path
使用 OPENCODE_CONFIG 环境变量指定自定义配置文件路径。
export OPENCODE_CONFIG=/path/to/my/custom-config.jsonopencode run "Hello world"自定义配置在优先级顺序中位于全局配置和项目配置之间加载。
自定义目录 Custom directory
使用 OPENCODE_CONFIG_DIR 环境变量指定自定义配置目录。
将像标准 .opencode 目录一样搜索此目录中的智能体、命令、模式和插件,并且应遵循相同的结构。
export OPENCODE_CONFIG_DIR=/path/to/my/config-directoryopencode run "Hello world"自定义目录在全局配置和 .opencode 目录之后加载,因此它 可以覆盖 它们的设置。
架构 Schema
配置文件有一个定义在 opencode.ai/config.json 中的架构。
你的编辑器应该能够基于该架构进行验证和自动完成。
TUI
你可以通过 tui 选项配置 TUI 特定的设置。
{ "$schema": "https://opencode.ai/config.json", "tui": { "scroll_speed": 3, "scroll_acceleration": { "enabled": true }, "diff_style": "auto" }}可用选项:
scroll_acceleration.enabled- 启用 macOS 风格的滚动加速。优先于scroll_speed。scroll_speed- 自定义滚动速度倍增器(默认:3,最小:1)。如果scroll_acceleration.enabled为true,则忽略此项。diff_style- 控制 diff 渲染。"auto"适应终端宽度,"stacked"始终显示单列。
服务器 Server
你可以通过 server 选项为 opencode serve 和 opencode web 命令配置服务器设置。
{ "$schema": "https://opencode.ai/config.json", "server": { "port": 4096, "hostname": "0.0.0.0", "mdns": true, "cors": ["http://localhost:5173"] }}可用选项:
port- 监听端口。hostname- 监听主机名。启用mdns且未设置主机名时,默认为0.0.0.0。mdns- 启用 mDNS 服务发现。这允许网络上的其他设备发现你的 OpenCode 服务器。cors- 使用基于浏览器的客户端时允许 CORS 的其他来源。值必须是完整来源(协议 + 主机 + 可选端口),例如https://app.example.com。
工具 Tools
你可以通过 tools 选项管理 LLM 可以使用的工具。
{ "$schema": "https://opencode.ai/config.json", "tools": { "write": false, "bash": false }}模型 Models
你可以通过 provider、model 和 small_model 选项配置要在 OpenCode 配置中使用的提供商和模型。
{ "$schema": "https://opencode.ai/config.json", "provider": {}, "model": "anthropic/claude-sonnet-4-5", "small_model": "anthropic/claude-haiku-4-5"}small_model 选项配置用于轻量级任务(如标题生成)的单独模型。默认情况下,如果你的提供商提供更便宜的模型,OpenCode 会尝试使用它,否则会回退到你的主模型。
提供商选项可以包括 timeout 和 setCacheKey:
{ "$schema": "https://opencode.ai/config.json", "provider": { "anthropic": { "options": { "timeout": 600000, "setCacheKey": true } } }}timeout- 请求超时时间(毫秒)(默认:300000)。设置为false以禁用。setCacheKey- 确保始终为指定提供商设置缓存键。
提供商特定选项 Provider-Specific Options
除通用的 timeout 和 apiKey 设置外,某些提供商支持其他配置选项。
Amazon Bedrock
Amazon Bedrock 支持 AWS 特定配置:
{ "$schema": "https://opencode.ai/config.json", "provider": { "amazon-bedrock": { "options": { "region": "us-east-1", "profile": "my-aws-profile", "endpoint": "https://bedrock-runtime.us-east-1.vpce-xxxxx.amazonaws.com" } } }}region- Bedrock 的 AWS 区域(默认为AWS_REGION环境变量或us-east-1)profile- 来自~/.aws/credentials的 AWS 命名配置文件(默认为AWS_PROFILE环境变量)endpoint- VPC 端点的自定义端点 URL。这是通用baseURL选项使用 AWS 特定术语的别名。如果两者都指定,endpoint优先。
主题 Themes
你可以通过 theme 选项在 OpenCode 配置中配置你想要使用的主题。
{ "$schema": "https://opencode.ai/config.json", "theme": ""}智能体 Agents
你可以通过 agent 选项为特定任务配置专门的智能体。
{ "$schema": "https://opencode.ai/config.json", "agent": { "code-reviewer": { "description": "Reviews code for best practices and potential issues", "model": "anthropic/claude-sonnet-4-5", "prompt": "You are a code reviewer. Focus on security, performance, and maintainability.", "tools": { // Disable file modification tools for review-only agent "write": false, "edit": false, "bash": false }, }, },}你也可以在 ~/.config/opencode/agents/ 或 .opencode/agents/ 中使用 markdown 文件定义智能体。在此处了解更多。
默认智能体 Default agent
你可以使用 default_agent 选项设置默认智能体。这决定了在未显式指定时使用哪个智能体。
{ "$schema": "https://opencode.ai/config.json", "default_agent": "plan"}默认智能体必须是主智能体(不是子智能体)。这可以是内置智能体如 "build" 或 "plan",或者是你定义的 自定义智能体。如果指定的智能体不存在或是子智能体,OpenCode 将回退到 "build" 并发出警告。
此设置适用于所有界面:TUI、CLI(opencode run)、桌面应用和 GitHub Action。
分享 Sharing
你可以通过 share 选项配置 分享 功能。
{ "$schema": "https://opencode.ai/config.json", "share": "manual"}这接受:
"manual"- 允许通过命令手动分享(默认)"auto"- 自动分享新对话"disabled"- 完全禁用分享
默认情况下,分享设置为手动模式,你需要使用 /share 命令显式分享对话。
命令 Commands
你可以通过 command 选项为重复性任务配置自定义命令。
{ "$schema": "https://opencode.ai/config.json", "command": { "test": { "template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes.", "description": "Run tests with coverage", "agent": "build", "model": "anthropic/claude-haiku-4-5", }, "component": { "template": "Create a new React component named $ARGUMENTS with TypeScript support.\nInclude proper typing and basic structure.", "description": "Create a new component", }, },}你也可以在 ~/.config/opencode/commands/ 或 .opencode/commands/ 中使用 markdown 文件定义命令。在此处了解更多。
快捷键 Keybinds
你可以通过 keybinds 选项自定义快捷键。
{ "$schema": "https://opencode.ai/config.json", "keybinds": {}}自动更新 Autoupdate
OpenCode 在启动时会自动下载任何新更新。你可以通过 autoupdate 选项禁用此功能。
{ "$schema": "https://opencode.ai/config.json", "autoupdate": false}如果你不想要更新,但希望在有新版本可用时收到通知,请将 autoupdate 设置为 "notify"。
注意,这仅在未使用包管理器(如 Homebrew)安装时有效。
格式化器 Formatters
你可以通过 formatter 选项配置代码格式化工具。
{ "$schema": "https://opencode.ai/config.json", "formatter": { "prettier": { "disabled": true }, "custom-prettier": { "command": ["npx", "prettier", "--write", "$FILE"], "environment": { "NODE_ENV": "development" }, "extensions": [".js", ".ts", ".jsx", ".tsx"] } }}权限 Permissions
默认情况下,opencode 允许所有操作 而无需明确批准。你可以使用 permission 选项更改此设置。
例如,确保 edit 和 bash 工具需要用户批准:
{ "$schema": "https://opencode.ai/config.json", "permission": { "edit": "ask", "bash": "ask" }}压缩 Compaction
你可以通过 compaction 选项控制上下文压缩行为。
{ "$schema": "https://opencode.ai/config.json", "compaction": { "auto": true, "prune": true }}auto- 当上下文已满时自动压缩会话(默认:true)。prune- 移除旧的工具输出以节省令牌(默认:true)。
观察器 Watcher
你可以通过 watcher 选项配置文件观察器忽略模式。
{ "$schema": "https://opencode.ai/config.json", "watcher": { "ignore": ["node_modules/**", "dist/**", ".git/**"] }}模式遵循 glob 语法。使用此功能从文件观察中排除嘈杂的目录。
MCP 服务器
你可以通过 mcp 选项配置你想要使用的 MCP 服务器。
{ "$schema": "https://opencode.ai/config.json", "mcp": {}}插件 Plugins
插件 使用自定义工具、钩子和集成扩展 OpenCode。
将插件文件放在 .opencode/plugins/ 或 ~/.config/opencode/plugins/ 中。你也可以通过 plugin 选项从 npm 加载插件。
{ "$schema": "https://opencode.ai/config.json", "plugin": ["opencode-helicone-session", "@my-org/custom-plugin"]}指令 Instructions
你可以通过 instructions 选项配置你正在使用的模型的指令。
{ "$schema": "https://opencode.ai/config.json", "instructions": ["CONTRIBUTING.md", "docs/guidelines.md", ".cursor/rules/*.md"]}这接受指令文件的路径和 glob 模式数组。在此处了解更多关于规则的信息。
禁用提供商 Disabled providers
你可以通过 disabled_providers 选项禁用自动加载的提供商。当你想要阻止某些提供商被加载(即使它们的凭据可用)时,这很有用。
{ "$schema": "https://opencode.ai/config.json", "disabled_providers": ["openai", "gemini"]}disabled_providers 选项接受提供商 ID 数组。当禁用提供商时:
- 即使设置了环境变量,它也不会加载。
- 即使通过
/connect命令配置了 API 密钥,它也不会加载。 - 提供商的模型不会出现在模型选择列表中。
启用提供商 Enabled providers
你可以通过 enabled_providers 选项指定提供商的允许列表。设置后,只有指定的提供商将被启用,所有其他提供商将被忽略。
{ "$schema": "https://opencode.ai/config.json", "enabled_providers": ["anthropic", "openai"]}当你想要限制 OpenCode 仅使用特定提供商,而不是逐个禁用它们时,这很有用。
如果提供商同时出现在 enabled_providers 和 disabled_providers 中,为了向后兼容,disabled_providers 将优先。
实验性功能 Experimental
experimental 键包含正在积极开发中的选项。
{ "$schema": "https://opencode.ai/config.json", "experimental": {}}变量 Variables
你可以在配置文件中使用变量替换来引用环境变量和文件内容。
环境变量 Env vars
使用 {env:VARIABLE_NAME} 替换环境变量:
{ "$schema": "https://opencode.ai/config.json", "model": "{env:OPENCODE_MODEL}", "provider": { "anthropic": { "models": {}, "options": { "apiKey": "{env:ANTHROPIC_API_KEY}" } } }}如果未设置环境变量,它将被替换为空字符串。
文件 Files
使用 {file:path/to/file} 替换文件内容:
{ "$schema": "https://opencode.ai/config.json", "instructions": ["./custom-instructions.md"], "provider": { "openai": { "options": { "apiKey": "{file:~/.secrets/openai-key}" } } }}文件路径可以是:
- 相对于配置文件目录
- 或以
/或~开头的绝对路径
这些对于以下情况很有用:
- 将敏感数据(如 API 密钥)保存在单独的文件中。
- 包含大型指令文件而不弄乱你的配置。
- 跨多个配置文件共享通用配置片段。