如何使用 Claude Code Cowork 模式 (2026)
在同一个代码库上并行运行多个 Claude Code 代理
开始使用 Hypereal 构建
通过单个 API 访问 Kling、Flux、Sora、Veo 等。免费积分开始,扩展到数百万。
无需信用卡 • 10万+ 开发者 • 企业级服务
如何使用 Claude Code 协作(Cowork)模式 (2026)
Claude Code 的协作模式允许你在同一个项目上同时运行多个 Claude Code 实例。与其让一个智能体串行地完成所有工作,不如启动并行智能体来协调工作、攻克功能的各个部分或处理不同的关注点——所有这些都可以同步进行。
本指南涵盖了如何设置协作模式、实际工作流以及避免常见陷阱的技巧。
什么是协作模式?
协作模式是 Claude Code 内置的多智能体协作功能。当你启用协作功能启动 Claude Code 时,你可以:
- 在不同的终端面板中运行多个 Claude 实例
- 让每个实例同时处理不同的文件或任务
- 让实例通过共享的项目文件传递上下文
- 协调跨前端、后端和测试的复杂功能
把它想象成结对编程,只不过你有多个 AI 伙伴在并行工作,而由你进行编排。
前提条件
在使用协作模式之前,请确保你已经:
- 全局安装了 Claude Code:
npm install -g @anthropic-ai/claude-code - 拥有有效的 Anthropic API 密钥或 Claude Max 订阅
- 项目中有一个结构良好的
CLAUDE.md文件(这对于维持多智能体的一致性至关重要) - 一个终端复用器(tmux、iTerm2 分屏或 VS Code 终端)
设置协作模式
第一步:准备你的 CLAUDE.md
当多个智能体在同一个代码库上工作时,共享规范至关重要。你的 CLAUDE.md 应明确定义:
# CLAUDE.md
## 项目结构
- /src/app - Next.js 页面和布局
- /src/components - 共享 UI 组件
- /src/lib - 业务逻辑和工具函数
- /src/db - 数据库 Schema 和查询
## 编码规范
- 启用 TypeScript 严格模式
- 使用具名导出 (named exports),不使用默认导出 (default exports)
- 组件使用 interface Props,不使用行内类型
- 所有数据库查询均通过 Repository 模式
## 文件所有权(用于并行工作)
- 智能体 A: src/app/api/**, src/db/**
- 智能体 B: src/components/**, src/app/(marketing)/**
- 智能体 C: src/lib/**, tests/**
文件所有权部分是可选的,但有助于防止智能体同时工作时产生合并冲突。
第二步:启动多个实例
打开独立的终端面板,并在每个面板中启动 Claude Code:
终端 1 —— 后端智能体:
claude "You are working on the backend. Implement the new /api/orders
endpoint with CRUD operations. Use the existing repository pattern
in src/db/repositories/. Do not modify any frontend files."
终端 2 —— 前端智能体:
claude "You are working on the frontend. Build the orders management
page at src/app/(dashboard)/orders/page.tsx. Use the existing
DataTable component pattern. The API endpoint will be at /api/orders.
Do not modify any backend files."
终端 3 —— 测试智能体:
claude "You are writing tests. Watch the src/app/api/orders/ and
src/app/(dashboard)/orders/ directories. As files appear or change,
write comprehensive tests. Use the existing test patterns in tests/."
第三步:通过文件进行协调
智能体共享文件系统,这意味着它们可以通过实际代码进行协调。后端智能体创建 API 类型,前端智能体导入它们:
// src/types/orders.ts (由后端智能体创建)
export interface Order {
id: string;
customerId: string;
items: OrderItem[];
total: number;
status: "pending" | "confirmed" | "shipped" | "delivered";
createdAt: Date;
}
export interface CreateOrderInput {
customerId: string;
items: { productId: string; quantity: number }[];
}
前端智能体在读取项目结构时会发现这些类型并直接使用,从而确保全栈的类型安全。
实际协作工作流
工作流 1:功能开发(前端 + 后端 + 测试)
这是最常见的模式。三个智能体从不同角度处理同一个功能。
| 智能体 | 职责 | 范围 |
|---|---|---|
| 后端 | API 路由、数据库查询、业务逻辑 | src/app/api/, src/db/, src/lib/ |
| 前端 | UI 组件、页面、客户端状态 | src/components/, src/app/(routes)/ |
| 测试 | 单元测试、集成测试 | tests/, __tests__/ |
启动顺序很重要。先启动后端智能体,以便它创建类型和 API 合约。然后启动前端智能体。测试智能体可以最后运行或并发运行。
工作流 2:大规模重构
将大规模重构拆分为独立的块:
# 终端 1:重构身份验证
claude "Migrate all auth logic from next-auth v4 to v5. Only touch
files in src/lib/auth/ and src/app/api/auth/. Update the session
type definitions."
# 终端 2:重构数据库
claude "Migrate all database queries from raw SQL to Drizzle ORM.
Only touch files in src/db/ and src/lib/repositories/. Keep the
same function signatures."
# 终端 3:更新导入和类型
claude "Watch for type errors across the project. As the auth and
database refactors progress, update any broken imports or type
mismatches in the remaining files."
工作流 3:代码审查 + 修复
一个智能体审查,另一个智能体修复:
# 终端 1:审查者
claude "Review all files changed in the last 3 commits. Create a
file at .claude/review-findings.md with issues found, categorized
by severity (critical, warning, suggestion)."
# 终端 2:修复者(在审查者发现问题后启动)
claude "Read .claude/review-findings.md and fix all critical and
warning issues. Mark each one as resolved in the file."
最佳实践
1. 定义清晰的界限
最常见的失败模式是两个智能体同时编辑同一个文件。可以通过以下方式防止:
- 明确指定每个智能体拥有的目录
- 使用明确的指令,如“不要修改 src/components/ 中的文件”
- 将共享类型放在一个由单个智能体拥有的专用
/types目录中
2. 使用 Plan 模式进行协调
当智能体需要在共享接口上协同工作时,先以 plan 模式运行第一个智能体:
claude --permission-mode plan "Design the API contract for the orders
feature. Create type definitions and document the endpoints. Do not
implement anything yet."
审查并批准该计划,然后启动遵循既定合约的执行智能体。
3. 交错启动智能体
不要同时启动所有智能体。给基础智能体(通常是后端或类型定义智能体)2-3 分钟的领先时间:
- 第 0 分钟: 启动类型/合约智能体
- 第 2 分钟: 启动后端实现智能体
- 第 3 分钟: 启动前端实现智能体
- 第 4 分钟: 启动测试智能体
4. 使用 Git 监控
使用 git 跟踪每个智能体正在执行的操作:
# 实时观察文件变化
watch -n 5 'git status --short'
# 查看自智能体启动以来的更改
git diff --stat
# 创建检查点
git add -A && git commit -m "checkpoint: agents mid-progress"
5. 处理冲突
如果两个智能体修改了同一个文件,你会看到冲突。处理方式如下:
# 检查冲突
git diff --check
# 让 Claude 解决它们
claude "There are merge conflicts in src/types/orders.ts. Resolve
them by keeping both agents' contributions and ensuring type safety."
成本考量
并行运行多个智能体成倍增加你的 API 成本。以下是大概的成本明细:
| 配置 | 智能体数量 | 预计每小时成本 |
|---|---|---|
| 单体智能体 (Sonnet) | 1 | $2-5 |
| 协作模式 (Sonnet x3) | 3 | $6-15 |
| 协作模式 (Opus + Sonnet x2) | 3 | $15-30 |
要在协作模式下优化成本:
- 使用 Sonnet 作为实现智能体,并保留 Opus 用于规划/架构智能体
- 在完成子任务后,在每个智能体中运行
/compact - 设置
--max-turns以防止智能体失控 - 任务完成后立即关闭智能体
# 成本优化的协作设置
claude -m opus "Design the architecture for..." # 规划智能体
claude -m sonnet "Implement the backend..." # 实现
claude -m sonnet "Implement the frontend..." # 实现
claude -m haiku "Write tests for..." # 测试(简单任务)
常见问题及解决方案
智能体互相覆盖工作:
在指令和 CLAUDE.md 中设置明确的文件界限。使用 git commit 作为检查点。
上下文窗口填满:
定期在每个智能体中运行 /compact。长期运行的智能体会迅速积累上下文。
智能体重复劳动: 具体细化范围。“实现订单创建接口”比“处理订单相关工作”更好。
代码风格不一致:
确保你的 CLAUDE.md 包含详细的代码风格规范。所有智能体都会读取并遵循相同的规则。
结论
协作模式将 Claude Code 从一个单一助手转变为一支协调一致的开发团队。成功的关键在于清晰的界限、交错启动以及在 CLAUDE.md 中完备的项目文档。
在扩展到三或四个智能体之前,先从简单的双智能体模式(后端 + 前端)开始。当你对智能体如何协调有了直觉后,就可以处理日益复杂的并行工作流。
如果你正在构建需要媒体生成功能(图像、视频、语音或数字人)的 AI 应用,Hypereal AI 提供了一个可以访问最新模型的统一 API。它与 Claude Code 配合使用,是构建 AI 驱动产品的绝佳组合。
