
大家好,我是 Ai 学习的老章
今天给大家介绍一个我最近在用的 AI 编程神器 —— OpenCode。4.6万 Star,500+ 贡献者,每月被 65 万开发者使用。这货基本上就是开源版的 Claude Code,而且完全不绑定任何一家模型提供商!

简介
OpenCode 是一个 100% 开源的 AI 编程智能体,可以在终端、桌面应用或 IDE 里使用。
和 Claude Code 有什么区别?官方自己也说了:
- 完全开源:代码全在 GitHub 上,想怎么改就怎么改
- 不绑定模型:Claude、GPT、Gemini、DeepSeek 甚至本地模型都能用,75+ 个 LLM provider 随便切
- 内置 LSP 支持:能自动识别你项目用的语言,加载对应的 LSP
- TUI 极致体验:这帮人是 Neovim 用户,也是 terminal.shop 的创造者,对终端的执念可想而知
- 客户端/服务端架构:你可以在电脑上跑 OpenCode,然后用手机远程控制它,这个设计太骚了
核心功能与特点:
- 🔌 LSP 自动加载:根据项目自动配置语言服务器
- 🧵 多会话并行:同一个项目可以同时开多个 Agent
- 🔗 分享会话链接:遇到问题可以把会话分享给同事 debug
- 🔐 Claude Pro 直连:可以登录 Anthropic 直接用你的 Claude Pro/Max 订阅
- 🌐 全平台支持:终端、桌面应用、IDE 插件都有
安装
安装方式超级多,官方推荐一键脚本:
curl -fsSL https://opencode.ai/install | bash
各平台包管理器也都支持:
# macOS / Linux
brew install opencode
# Windows (Chocolatey)
choco install opencode
# Windows (Scoop)
scoop bucket add extras
scoop install extras/opencode
# npm/pnpm/yarn
npm install -g opencode-ai
# Docker(也能用)
docker run -it --rm ghcr.io/anomalyco/opencode
# Arch Linux
paru -S opencode-bin
桌面应用也有 Beta 版了,支持 macOS(Intel/Apple Silicon)、Windows 和 Linux(deb/rpm):
下载地址:https://opencode.ai/download

# macOS 通过 Homebrew 安装桌面版
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 支持的模型提供商多到离谱,文档里列了 30 多个:
- 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 的 CLI 功能非常强大,远不止一个 opencode 命令。来看看有哪些骚操作:
非交互模式:脚本自动化利器
不想打开 TUI?直接命令行问问题:
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
这意味着什么?你可以让一台高性能服务器跑 OpenCode 后端,自己用轻薄本甚至 iPad 远程操控。对于需要在服务器上编译/测试的场景,简直不要太爽。
Web 界面
不喜欢终端?还有 Web 界面:
opencode web
会自动打开浏览器,通过 Web 界面操作 OpenCode。
会话管理
# 列出所有会话
opencode session list
# 导出会话(方便分享或备份)
opencode export <sessionID>
# 导入会话(支持 JSON 文件或分享链接)
opencode import session.json
opencode import https://opncd.ai/s/abc123
统计消耗
想知道自己用了多少 token、花了多少钱?
opencode stats
opencode stats --days 7 --models # 按模型统计最近7天
GitHub 集成
OpenCode 还能直接当 GitHub Agent 用!在仓库里安装:
opencode github install
这会设置 GitHub Actions 工作流,让 OpenCode 自动处理 issue、PR 等任务。
MCP Server 集成
MCP(Model Context Protocol) 是 Anthropic 搞的协议,用来给 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"
}
}
}
不确定怎么实现某个功能?让它帮你在 GitHub 上搜代码示例!
MCP 管理命令
# 添加 MCP 服务器(交互式)
opencode mcp add
# 列出所有已配置的 MCP
opencode mcp list
# 调试 OAuth 问题
opencode mcp debug my-oauth-server
# 登出
opencode mcp logout my-oauth-server
Agent Skills:自定义技能
这是 OpenCode 的另一个杀手级功能: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:
- 项目级:
.opencode/skill/<name>/SKILL.md - 全局级:
~/.config/opencode/skill/<name>/SKILL.md - Claude 兼容:
.claude/skills/<name>/SKILL.md(方便从 Claude Code 迁移)
权限控制
可以在 opencode.json 里控制哪些 Skill 可用:
{
"permission": {
"skill": {
"pr-review": "allow",
"internal-*": "deny",
"experimental-*": "ask",
"*": "allow"
}
}
}
支持通配符,比如 internal-* 可以匹配所有 internal- 开头的 Skill。
这个功能太适合团队协作了!你可以把常用的代码规范、发布流程、Code Review 检查清单都封装成 Skill,团队成员直接复用。
OpenCode Zen:懒人福音
如果你懒得折腾各种模型 API,官方还搞了个 OpenCode Zen 服务。
Zen 是什么?就是官方帮你精选了一批专门针对编程智能体优化过的模型,测试过了,好用的才放进来。你只需要充值 20 刀,按用量付费,不用自己去各个平台申请 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 那种原生 Claude 集成的流畅度
- 入门需要自己配置 API Key(其实也不算缺点)
4.6 万 Star 的项目,500 多个贡献者,这势头很猛。喜欢折腾的朋友,强烈建议试试。
项目地址:https://github.com/anomalyco/opencode
官网:https://opencode.ai
文档:https://opencode.ai/docs
240

被折叠的 条评论
为什么被折叠?



