Claude Code 是一个真正的 Agentic 编程环境,与问答式 Chatbot 完全不同——它能读取文件、运行命令、进行修改,并自主解决问题。这种全新的工作模式需要对应的最佳实践。本文整理了 Anthropic 内部团队和大量工程师验证的 8 个核心模式。
核心约束:上下文窗口是最重要的资源
大多数最佳实践都基于一个关键约束:Claude 的上下文窗口填满后性能会显著下降。
上下文窗口包含你的整个对话:每条消息、每个 Claude 读取的文件、每条命令输出。一个复杂的调试会话可能消耗数万 Token。当上下文接近满时,Claude 可能开始「遗忘」早期指令或产生更多错误。
建议:通过自定义状态栏持续监控上下文使用量。
模式 1:提供验证标准(最高杠杆)
这是单一最高杠杆的改进:让 Claude 能够验证自己的工作。
没有清晰的成功标准,Claude 可能产出看起来正确但实际不工作的结果,而你成为唯一的反馈循环。
| 策略 | 低效示例 | 高效示例 |
|---|---|---|
| 提供验证标准 | "实现一个验证邮箱的函数" | "写 validateEmail 函数。测试用例:user@example.com → true,invalid → false,user@.com → false。实现后运行测试" |
| UI 变更可视化验证 | "让仪表板更好看" | "[贴截图] 实现这个设计。截图对比结果和原图,列出差异并修复" |
| 根因而非症状 | "构建失败了" | "构建出错:[贴错误]。修复它并验证构建成功。解决根本原因,不要屏蔽错误" |
UI 变更可以使用 Chrome 扩展进行视觉验证——Claude 会打开新标签页测试 UI 并迭代直到代码正常工作。
模式 2:先探索再规划再编码
让 Claude 直接跳到编码,可能产出解决了错误问题的代码。使用 Plan Mode 将探索与执行分开。
四阶段工作流:
Phase 1 - 探索(Plan Mode):
read /src/auth and understand how we handle sessions and login.
also look at how we manage environment variables for secrets.Phase 2 - 规划(Plan Mode):
I want to add Google OAuth. What files need to change?
What's the session flow? Create a plan.按 Ctrl+G 在文本编辑器中打开计划并直接编辑,然后让 Claude 执行。
Phase 3 - 实现(Normal Mode):
implement the OAuth flow from your plan. write tests for the
callback handler, run the test suite and fix any failures.Phase 4 - 提交:
commit with a descriptive message and open a PR注意:Plan Mode 会增加开销,对于小改动(修错字、添加日志、重命名变量)直接让 Claude 执行更高效。计划最有价值的场景是:方法不确定时、修改多个文件时、不熟悉被修改的代码时。
模式 3:提供精确的上下文
越精确的指令,需要的修正越少。
| 策略 | 低效 | 高效 |
|---|---|---|
| 引用具体文件 | "update the login page" | "update pages/login.tsx. use the pattern from pages/signup.tsx" |
| 提及约束条件 | "add error handling" | "add error handling without changing the function signature" |
| 指向示例模式 | "write tests for the API" | "write tests for /api/users following the pattern in /api/products.test.ts" |
模式 4:管理上下文,不是忍受它
上下文窗口满了会影响性能,但有应对策略:
策略 1:压缩上下文
/compact或者在上下文用尽时,告诉 Claude 总结当前工作并开始新会话。
策略 2:及时结束会话
完成一个有界任务后立即提交并开始新会话,而不是继续同一个拉长的会话。
策略 3:使用 Subagent 隔离上下文
use the explore subagent to search the codebase for all authentication patternsSubagent 在独立上下文窗口运行,结果汇总返回主对话,有效保护主上下文。
模式 5:用 CLAUDE.md 设置工作规范
投入时间编写好的 CLAUDE.md,节省大量的重复解释:
## 开发规范
- 使用 pnpm,不用 npm
- TypeScript,不用 any
- 测试框架:Vitest
- API 错误统一使用 ApiError 类
## 关键命令
- 开发:`pnpm dev`
- 测试:`pnpm test`
- 构建:`pnpm build`
## 代码风格
- React 组件使用函数式
- 状态管理:Zustand
- 样式:Tailwind CSS模式 6:从 Git 角度思考任务
原子提交思维:把任务视为一系列有界的 git 提交,而不是一个无边界的对话流。
# 功能完成后立即提交
commit the OAuth implementation with a conventional commit message
# 为 Claude 工作创建草稿分支
git checkout -b draft/add-oauth
# 在草稿分支工作,满意后 squash 合并这种方式的好处:
- 上下文自然地被有界提交分割
- 容易回滚不满意的工作
- PR 历史更清晰
模式 7:善用非交互模式处理批量任务
# 对特定文件执行任务
claude -p "add error handling to all API routes in src/api/"
# 结合 shell 脚本批量处理
for file in src/components/*.tsx; do
claude -p "add PropTypes to the component in $file"
done
# 在 CI/CD 中自动化
claude --permission-mode plan -p "Review these changes for security issues" < git_diff.txt模式 8:监控并打断不满意的方向
主动观察:不要设置任务就离开。用 --no-auto-approve 或 Ask permissions 模式看 Claude 如何工作。
及时打断:如果 Claude 走错方向,立即打断(Ctrl+C 或点击停止按钮)并纠正:
# Claude 走错方向时直接���断并重定向
[Ctrl+C]
stop - that approach won't work because we need to maintain backward compatibility.
try a different approach: use an adapter pattern instead of modifying the interface检查点确认:在复杂任务的关键节点要求 Claude 暂停并汇报:
before making any changes to the database schema, pause and explain your plan快速参考卡
| 场景 | 最佳实践 |
|---|---|
| 开始新功能 | Plan Mode 探索 → 规划 → 实现 → 提交 |
| 调试 Bug | 提供错误信息 + 复现步骤 + 期望行为 |
| 上下文快满 | /compact 或开新会话 |
| 重复性工作 | 用非交互模式批量处理 |
| 大范围修改 | Subagent 分工,保护主上下文 |
| 团队共享 | CLAUDE.md 写入项目规范 |
来源:Claude Code 官方文档 - Best Practices 原文作者:Anthropic Team