
简介
OpenCode 是一个 100% 开源的 AI 编程智能体,可以在终端、桌面应用或 IDE 里使用。
与 Claude Code 的区别:
- 完全开源:代码全在 GitHub 上,可自由修改
- 不绑定模型:支持 Claude、GPT、Gemini、DeepSeek 甚至本地模型,兼容 75+ 个 LLM provider
- 内置 LSP 支持:自动识别项目语言并加载对应 LSP
- TUI 体验:基于 Neovim 用户设计,对终端交互有深度优化
- 客户端/服务端架构:支持在电脑运行 OpenCode,通过手机远程控制
核心功能与特点:
- 🔌 LSP 自动加载:根据项目自动配置语言服务器
- 🧵 多会话并行:同一项目可同时开启多个 Agent
- 🔗 分享会话链接:遇到问题可分享给同事 debug
- 🔐 Claude Pro 直连:支持登录 Anthropic 直接使用订阅
- 🌐 全平台支持:终端、桌面应用、IDE 插件均有
安装
官方推荐一键脚本:
curl -fsSL https://opencode.ai/install | bash
各平台包管理器支持:
brew install opencode
choco install opencode
scoop bucket add extras
scoop install extras/opencode
npm install -g opencode-ai
docker run -it --rm ghcr.io/anomalyco/opencode
paru -S opencode-bin
桌面应用 Beta 版支持 macOS(Intel/Apple Silicon)、Windows 和 Linux(deb/rpm):
下载地址:https://opencode.ai/download

brew install --cask opencode-desktop
也可在 IDE 中直接安装插件。

快速开始
进入项目目录,运行 opencode 启动:
cd /path/to/project
opencode
首次使用建议先运行 /init 命令,让 OpenCode 分析项目,自动生成 AGENTS.md 文件:
/init
该文件帮助 OpenCode 理解项目结构和编码规范,建议 commit 到 Git。
两种内置 Agent
OpenCode 内置两种 Agent,用 Tab 键切换:
- Build 模式(默认):全权限,可读写文件、执行命令
- Plan 模式:只读模式,仅分析和规划,不修改代码
Plan 模式适合:
- 探索不熟悉代码库
- 先规划再动手的工作流程
- 让 AI 出方案但不执行
还有 @general 子智能体,可处理复杂搜索和多步骤任务。
配置模型
OpenCode 支持多种模型提供商,包括:
- Anthropic(Claude)
- OpenAI(GPT)
- DeepSeek
- Google Vertex AI
- Ollama(本地模型)
- LM Studio
- llama.cpp
- Groq
- Together AI
- Fireworks AI
- OpenRouter
配置 API Key 直接在界面运行:
/connect
选择 provider 输入 API Key。Key 保存在 ~/.local/share/opencode/auth.json。
CLI 命令详解
非交互模式:脚本自动化
命令行直接提问:
opencode run "Explain the use of context in Go"
指定模型、附加文件:
opencode run --model anthropic/claude-3.5-sonnet --file ./src/main.py "帮我优化这段代码"
远程控制:服务器 + TUI 分离
在服务器上启动 headless 服务:
opencode serve --port 4096 --hostname 0.0.0.0
在另一台机器远程连接:
opencode attach http://10.20.30.40:4096
Web 界面
opencode web
自动打开浏览器操作。
会话管理
opencode session list
opencode export <sessionID>
opencode import session.json
opencode import https://opencode.ai/s/abc123
统计消耗
opencode stats
opencode stats --days 7 --models
GitHub 集成
在仓库里安装:
opencode github install
设置 GitHub Actions 工作流,自动处理 issue、PR 等任务。
MCP Server 集成
MCP(Model Context Protocol)用于给 AI 智能体扩展工具能力。OpenCode 原生支持 MCP。
添加本地 MCP Server
在 opencode.json 配置文件里加:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-local-mcp": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-everything"],
"enabled": true
}
}
}
添加远程 MCP Server
接入 Sentry 查 bug:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"sentry": {
"type": "remote",
"url": "https://mcp.sentry.dev/mcp",
"oauth": {}
}
}
}
配置后认证:
opencode mcp auth sentry
对话中使用:
Show me the latest unresolved issues in my project. use sentry
实用 MCP 示例
Context7 - 搜索技术文档:
{"mcp":{"context7":{"type":"remote","url":"https://mcp.context7.com/mcp"}}}
使用:Configure a Cloudflare Worker script to cache JSON API responses for five minutes. use context7
Grep by Vercel - 搜索 GitHub 代码:
{"mcp":{"gh_grep":{"type":"remote","url":"https://mcp.grep.app"}}}
MCP 管理命令
opencode mcp add
opencode mcp list
opencode mcp debug my-oauth-server
opencode mcp logout my-oauth-server
Agent Skills:自定义技能
定义可复用的技能,让 Agent 在特定场景下自动加载。
创建一个 Skill
在项目里创建 .opencode/skill/<name>/SKILL.md,例如 .opencode/skill/git-release/SKILL.md:
---
name: git-release
description: Create consistent releases and changelogs
license: MIT
compatibility: opencode
metadata:
audience: maintainers
workflow: github
---
## What I do
- Draft release notes from merged PRs
- Propose a version bump
- Provide a copy-pasteable `gh release create` command
## When to use me
Use this when you are preparing a tagged release. Ask clarifying questions if the target versioning scheme is unclear.
Skill 存放位置
- 项目级:
.opencode/skill/<name>/SKILL.md
- 全局级:
~/.config/opencode/skill/<name>/SKILL.md
- Claude 兼容:
.claude/skills/<name>/SKILL.md
权限控制
在 opencode.json 里控制哪些 Skill 可用:
{
"permission": {
"skill": {
"pr-review": "allow",
"internal-*": "deny",
"experimental-*": "ask",
"*": "allow"
}
}
}
OpenCode Zen:付费服务
官方提供 OpenCode Zen 服务,精选针对编程智能体优化过的模型。充值按用量付费,无需自行申请 API。
特点:
- Zero markup(无加价),价格透明
- 余额到 5 刀自动充值 20 刀
- 设置每月消费限额
若已有各家的 API Key,可直接使用,Zen 非必须。
命令速查表
| 命令 | 作用 |
|---|
Tab | 切换 Build/Plan 模式 |
/init | 初始化项目 |
/connect | 配置 API Key |
/models | 查看可用模型 |
/share | 分享当前会话 |
/undo | 撤销上一步修改 |
/redo | 重做 |
@ | 模糊搜索项目文件 |
@general | 调用通用子智能体 |
CLI 命令:
| 命令 | 作用 |
|---|
opencode run "prompt" | 非交互模式运行 |
opencode serve | 启动 headless 服务 |
opencode attach <url> | 连接远程服务 |
opencode web | 启动 Web 界面 |
opencode stats | 查看用量统计 |
opencode session list | 列出会话 |
opencode export <id> | 导出会话 |
opencode mcp add | 添加 MCP 服务器 |
opencode mcp list | 列出 MCP 服务器 |
opencode github install | 安装 GitHub Agent |
opencode upgrade | 升级版本 |
和 Claude Code 对比
| 特性 | OpenCode | Claude Code |
|---|
| 开源 | ✅ 100% MIT | ❌ 闭源 |
| 模型 | 任意(75+ providers) | 仅 Claude |
| 价格 | 按 API 实际消耗 | Claude 订阅费 |
| LSP | ✅ 开箱即用 | ❌ |
| MCP 支持 | ✅ 完整支持 | ✅ |
| Skills | ✅ 支持 | ✅ |
| TUI | ✅ 极致体验 | ✅ |
| Web 界面 | ✅ | ❌ |
| 桌面应用 | ✅ Beta | ❌ |
| 远程控制 | ✅ | ❌ |
| GitHub Agent | ✅ | ❌ |
总结
OpenCode 是一款功能全面的开源编程智能体:
- CLI 非交互模式方便写脚本
- 服务器/客户端分离支持远程控制
- 完整的 MCP 生态可无限扩展能力
- Skills 让团队协作更顺畅
- GitHub Agent 替代部分 DevOps 工作
优点:
- 完全开源,不绑定任何厂商
- 支持模型众多
- TUI 交互体验佳
- MCP、Skills 等高级功能齐全
- 客户端/服务端架构灵活
不足:
- 桌面应用为 Beta 版,可能有 bug
- 没有 Claude Code 那种原生集成的流畅度
- 入门需自行配置 API Key
项目地址:https://github.com/anomalyco/opencode
官网:https://opencode.ai
文档:https://opencode.ai/docs