Claude Code 是一个 Agentic 编码环境:它可以读取文件、运行命令、做出修改,自主解决问题。这改变了你的工作方式——你描述想要什么,Claude 负责构建它。本文整理了 Anthropic 内部团队验证有效的最佳实践。
核心约束:Context 窗口是最重要的资源
所有最佳实践都基于一个约束:Context 窗口填满得很快,填满后性能会下降。
Context 窗口包含:所有对话消息 + Claude 读取的每个文件 + 每次命令输出。一次调试会话或代码库探索可能消耗数万 Token。Context 接近上限时,Claude 可能开始「遗忘」早期指令或犯更多错误。
实践建议:
- 用自定义 Status Line 持续追踪 Context 用量
- 使用
/compact压缩历史,释放 Context 空间 - 长会话主动拆分:完成一个功能后
/clear开新会话
最高杠杆:给 Claude 自我验证的方式
这是单个最高价值的投入。Claude 能自我验证时(运行测试、对比截图、验证输出),质量会显著提升。
| 策略 | 低效做法 ❌ | 高效做法 ✅ |
|---|---|---|
| 提供验证标准 | 「实现一个验证邮箱的函数」 | 「写一个 validateEmail 函数。测试用例:valid@example.com → true, invalid → false, test@.com → false。实现后立即运行测试」 |
| UI 改动可视化验证 | 「让 Dashboard 好看一些」 | 「[粘贴截图] 实现这个设计,截图结果并与原图对比,列出差异并修复」 |
| 解决根本原因 | 「构建失败了」 | 「构建失败错误:[粘贴错误]。修复它并验证构建成功。解决根本原因,不要抑制错误」 |
验证手段:测试套件、Lint 检查、Bash 验证命令、Chrome 扩展(UI 自动测试)。
「先探索,再规划,后编码」四阶段工作法
让 Claude 直接跳到写代码往往会解决错误的问题。推荐四阶段工作流:
阶段一:探索(Plan Mode)
[进入 Plan Mode: Shift+Tab]
> read /src/auth and understand how we handle sessions and login.
> also look at how we manage environment variables for secrets.
Claude 只读文件、回答问题,不做任何修改。
阶段二:规划
> I want to add Google OAuth. What files need to change?
> What's the session flow? Create a plan.
按 Ctrl+G 在文本编辑器中打开计划,直接编辑后让 Claude 继续。
阶段三:实现(切回 Normal Mode)
[Shift+Tab 切回 Normal Mode]
> implement the OAuth flow from your plan.
> write tests for the callback handler,
> run the test suite and fix any failures.
阶段四:提交
> commit with a descriptive message and open a PR
何时跳过规划:任务范围清晰且改动小(修错别字、加日志、改变量名)时直接做。规划在你不确定方案、改动跨多文件、或不熟悉被修改的代码时最有价值。
提示词技巧
精确指定上下文:
❌ "add tests for foo.py"
✅ "write a test for foo.py covering the edge case where the user is logged out. avoid mocks."
❌ "why does ExecutionFactory have such a weird api?"
✅ "look through ExecutionFactory's git history and summarize how its api came to be"
❌ "fix the login bug"
✅ "users report login fails after session timeout.
check auth flow in src/auth/, especially token refresh.
write a failing test that reproduces the issue, then fix it"
提供丰富内容:
- 用
@filename引用文件(Claude 会先读文件再回答) - 直接粘贴截图(
Ctrl+V) - 提供 URL,Claude 会抓取文档
- 管道传入数据:
cat error.log | claude
有效沟通
让 Claude 反问你(需求不清晰时):
> I need to refactor the authentication system. Before you start,
> ask me clarifying questions about requirements, constraints, and preferences.
常见代码库问题提示词:
> how is the error handling pattern used across this codebase?
> what testing patterns do we use, and how do tests communicate intent?
> what are some examples of well-written modules in this codebase I can follow?
> explain the main data flow from API request to database and back
会话管理
及时纠偏:Claude 跑偏时立即纠正,不要等它走太远:
> stop - that approach won't work because [reason].
> instead, let's [alternative approach]
主动管理 Context:
- 觉得 Claude 开始重复或遗忘时,立即
/compact - 新任务开始前
/clear,避免旧 Context 干扰 - 用
/context可视化当前 Context 用量
用 Subagent 调查复杂问题:
> I need to understand why the payment processing is slow.
> use a subagent to investigate the database queries in the payment module
> without making any changes. report findings.
用 Checkpoints 回退:
/rewind # 回到任意历史节点
/fork approach-b # 从当前节点创建新分支探索另一种方案
自动化与扩展
非交互模式(适合 CI/CD)
# 基本用法
claude -p "analyze this PR for security issues" \
--output-format json \
--max-turns 10 \
--max-budget-usd 1.00
# 管道集成
git diff | claude -p "review for security vulnerabilities"
cat test-results.json | claude -p "summarize failures and suggest fixes"并行多会话(Git Worktree)
# 终端 1:开发新功能
claude -w feature-oauth
# 终端 2:同时修复 Bug
claude -w bugfix-payment
# 两个 Claude 实例互不干扰,各在独立 worktree 工作跨文件扇出(Fan Out)
> I need to add input validation to all API endpoints in src/api/.
> first, list all the endpoint files.
> then use subagents to add validation to each file in parallel.
> finally, run the test suite to verify everything works.
安全自主模式
# 严格沙箱:只允许读文件和运行测试,不允许其他网络或文件系统操作
claude --permission-mode plan \
--allowedTools "Read,Glob,Grep,Bash(npm test),Bash(npm run build)"常见失败模式(避免)
| 失败模式 | 改进方法 |
|---|---|
| 没有验证标准,只说「实现 X」 | 提供测试用例或截图,让 Claude 可以自我校验 |
| Context 耗尽才意识到问题 | 安�� Status Line,在 Context 满 70% 时就 /compact |
| 任务太大,一次性给 Claude | 拆分为阶段性任务,每阶段验证后再继续 |
| 让 Claude 自由探索而不给边界 | 给具体的文件范围、禁止修改的区域、期望的输出格式 |
| 发现方向错了但不及时纠正 | 「Stop - 这个方向不对,因为 X,改为 Y」立即纠偏 |
原文:Best Practices for Claude Code | 来源:Anthropic 官方文档