# 最佳实践

> ## 文档 索引
> 获取 the 完成 文档 索引 at: https://代码.claude.com/docs/llms.txt
> 使用 this 文件 to disc结束 所有 可用 页s 之前探索 further.

# Claude 代码 最佳实践

> 提示 and 模式s for getting the 最多 out of Claude 代码, from configuring your 环境 to scaling across 平行 会话s.

Claude 代码 is an 代理ic coding 环境. 不像 a chat机器人 that 答案s 问题s and 等待s, Claude 代码 can 读取 your 文件, 运行 命令, make 更改s, and 自治ly 工作 th粗糙 问题s while you观看, 重定向, or步骤 a方式 整个ly.

This 更改s how you 工作. Instead of写作 代码 yourself and询问 Claude to re视图 it, you describe what you 想要 and Claude 图s out how to 构建 it. Claude探索s, 计划s, and 实现s.

But this autonomy 静止 comes with a learning curve. Claude 工作s 在...内 确定 常量雨ts you 需要 to understand.

This 指南 c结束s 模式s that have 证实 有效 across Anthropic's 内部 团队 and for 工程师s using Claude 代码 across various 代码基础s, languages, and 环境s. For how the 代理ic loop 工作s under the hood,看见 [How Claude 代码 工作s](/en/how-claude-代码-工作s).

***

最多 最佳实践 are 基础d on one 常量雨t: Claude's 上下文 风ow fills 上 快, and 性能 de年级s as it fills.

Claude's 上下文 风ow h旧s your 整个 对话,包括 每个 消息, 每个 文件 Claude 读取s, and 每个 命令 输出. However, this can fill 上 快. A single 调试ging 会话 or 代码基础 探索 might gene速率 and con总和e tens of thou沙s of 令牌s.

This 事情s since LLM 性能 de年级s as 上下文 fills. When the 上下文 风ow is getting 满, Claude may 启动 "forgetting" 更早 说明 or制造 更多 薄雾akes. The 上下文 风ow is the 最多 重要 资源 to manage. To看见 how a 会话 fills 上 in 实践, [观看 an 交互走th粗糙](/en/上下文-风ow) of what 加载s at 启动上 and what 每个 文件 读取 成本s. 跟踪 上下文 使用方法 continuously with a [习俗 状态 行](/en/状态行), and看见 [Reduce 令牌 使用方法](/en/成本s#reduce-令牌-使用方法) for st速率gies on reducing 令牌 使用方法.

***

## Give Claude a 方式 to 验证 its 工作

  Include 测试s, 截图s, or 预期 输出s so Claude can 检查 itself. This is the single 高est-leve狂怒 薄g you can do.

Claude per形式s dramatic所有y 更好 when it can 验证 its own 工作, 像 运行 测试s, 比较 截图s, and 验证 输出s.

Without 清楚 成功 标准, it might produce 一些薄g that看s 正确 but 实际ly doesn't 工作. You become the only 反馈 loop, and 每个 薄雾ake requ怒s your 注意.

| 战略                              | 之前                                                  | 之后                                                                                                                                                                                                   |
| ------------------------------------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Provide 验证 标准**     | *"实现 a 功能 that 验证s email 添加resses"* | *"写入 a 验证Email 功能. 示例 测试 案例s: [用户@示例.com](mailto:用户@示例.com) is 真实, 无效 is 虚假, [用户@.com](mailto:用户@.com) is 虚假. 运行 the 测试s 之后实施"* |
| **验证 UI 更改s visu所有y**        | *"make the dash董事会看 更好"*                      | *"\[粘贴 截图] 实现 this 设计. take a 截图 of the 结果 and 比较 it to the 原始. 列表 差异s and 修复 them"*                                                            |
| **添加ress 根 原因s, not 症状s** | *"the 构建 is failing"*                                | *"the 构建 fails with this 错误: \[粘贴 错误]. 修复 it and 验证 the 构建 succeeds. 添加ress the 根 原因, don't s上press the 错误"*                                                             |

UI 更改s can be 验证 using the [Claude in Chrome 扩展](/en/chrome). It 打开s 新 tabs in your 浏览器, 测试s the UI, and ite速率s until the 代码 工作s.

Your 验证 can also be a 测试 suite, a linter, or a Bash 命令 that 检查s 输出. Invest in制造 your 验证 rock-固体.

***

##探索 第一个, then 计划, then 代码

  单独研究 and 规划 from 实施 to a空白 solving the 错误 问题.

Letting Claude跳 直 to coding can produce 代码 that solves the 错误 问题. 使用 [计划 模式](/en/常见-工作流s#使用-计划-模式-for-安全-代码-分析) to 单独 探索 from 执行.

The 推荐 工作流 has four 阶段s:

  
    进入 计划 模式. Claude 读取s 文件 and 答案s 问题s without制造 更改s.

    ```txt claude (计划 模式) 主题={空}
    读取 /src/auth and understand how we handle 会话s and 日志in.
    also看 at how we manage 环境变量 for 秘密s.
    ```
  

  
    Ask Claude to 创建 a 详情ed 实施 计划.

    ```txt claude (计划 模式) 主题={空}
    I 想要 to 添加 前往ogle OAuth. What 文件 需要 to 更改?
    What's the 会话 f低? 创建 a 计划.
    ```

    Press `Ctrl+G` to 打开 the 计划 in your 文本 编辑or for 直接 编辑ing 之前 Claude proceeds.
  

  
    Switch 返回 to 正常 模式 and let Claude 代码,验证 a收益st its 计划.

    ```txt claude (正常 模式) 主题={空}
    实现 the OAuth f低 from your 计划. 写入 测试s for the
    回调 处理器, 运行 the 测试 suite and 修复 任何 失败s.
    ```
  

  
    Ask Claude to 提交 with a de脚本ive 消息 and 创建 a PR.

    ```txt claude (正常 模式) 主题={空}
    提交 with a de脚本ive 消息 and 打开 a PR
    ```
  

  计划 模式 is 有用, but also 添加s 结束负责人.

  For 任务s where the 范围 is 清楚 and the 修复 is 小 (像固定 a typo,添加 a 日志 行, or renaming a 可变) ask Claude to do it 直接ly.

  规划 is 最多 有用 when you're 不确定 about the 方法, when the 更改 modifies mul提示le 文件, or when you're unfamiliar with the 代码 being modified. If you could describe the diff in one 句子,跳 the 计划.

***

## Provide 特定 上下文 in your 及时s

  The 更多 精确 your 说明, the 几个er 正确ions you'll 需要.

Claude can infer 意图, but it can't 读取 your mind. 参考 特定 文件, mention 常量雨ts, and point to 示例 模式s.

| 战略                                                                                         | 之前                                               | 之后                                                                                                                                                                                                                                                                                                                                                            |
| ------------------------------------------------------------------------------------------------ | ---------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **范围 the 任务.** 规格ify which 文件, what scenario, and 测试 偏好设置.                  | *"添加 测试s for foo.py"*                             | *"写入 a 测试 for foo.py覆盖 the 边 案例 where the 用户 is 日志ged out. a空白 模拟s."*                                                                                                                                                                                                                                                                    |
| **Point to 来源s.** 直接 Claude to the 来源 that can 答案 a 问题.                    | *"why does 执行事实ory have such a weird api?"* | *"看 th粗糙 执行事实ory's Git history and 总和marize how its api came to be"*                                                                                                                                                                                                                                                                             |
| **参考 现有 模式s.** Point Claude to 模式s in your 代码基础.                      | *"添加 a cal结束ar widget"*                            | *"看 at how 现有 widgets are 实现ed on the home 页 to understand the 模式s. 热DogWidget.pH值p is a 好 示例. fol低 the 模式 to 实现 a 新 cal结束ar widget that lets the 用户 选择 a month and paginate 前进s/落后s to pick a year. 构建 from scratch without libraries other than the ones al就绪 使用d in the 代码基础."* |
| **Describe the 症状.** Provide the 症状, the 可能 location, and what "固定"看s 像. | *"修复 the 日志in 缺陷"*                                | *"用户s 报告 that 日志in fails 之后 会话 超时. 检查 the auth f低 in src/auth/, e特殊ly 令牌 刷新. 写入 a failing 测试 that reproduces the 问题, then 修复 it"*                                                                                                                                                                                 |

模糊 及时s can be 有用 when you're探索 and can afford to course-正确. A 及时 像 `"what would you improve in this 文件?"` can 表面 薄gs you wouldn't have 想法 to ask about.

### Provide rich 满意

  使用 `@` to 参考 文件, 粘贴 截图s/图像s, or 管道 数据 直接ly.

You can provide rich 数据 to Claude in 几个 方式s:

* **参考 文件 with `@`** instead of描述 where 代码 lives. Claude 读取s the 文件 之前 responding.
* **粘贴 图像s 直接ly**. 复制/粘贴 or drag and 下降 图像s into the 及时.
* **Give URLs** for 文档 and API 参考s. 使用 `/权限` to 允许列表 频繁ly-使用d do主s.
* **管道 in 数据** by 运行中 `cat 错误.日志 | claude` to 发送 文件 满意s 直接ly.
* **Let Claude 获取 what it 需要s**. Tell Claude to 拉取 上下文 itself using Bash 命令, MCP 工具, or by 读取ing 文件.

***

## Con图 your 环境

A 几个 设置步骤s make Claude 代码 重要ly 更多 有效 across 所有 your 会话s. For a 满 概述 of 扩展 features and when to 使用 每个 one,看见 [Ext结束 Claude 代码](/en/features-概述).

### 写入 an 有效 CLAUDE.md

  运行 `/init` to gene速率 a 启动er CLAUDE.md 文件 基础d on your 当前 项目 结构, then re好 结束 时间.

CLAUDE.md is a 特殊 文件 that Claude 读取s at the 启动 of 每个 对话. Include Bash 命令, 代码 style, and 工作流 规则. This gives Claude 持续 上下文 it can't infer from 代码 alone.

The `/init` 命令分析s your 代码基础 to detect 构建 系统s, 测试 框架s, and 代码 模式s,给 you a 固体 基础 to re好.

There's no 必需 格式 for CLAUDE.md 文件, but keep it 短 and 人类-读取能够. For 示例:

```mark下 CLAUDE.md 主题={空}
# 代码 style
- 使用 ES 模块s (进口/出口) syn税, not 常见JS (requ怒)
- De结构 进口s when 可能 (eg. 进口 { foo } from 'bar')

# 工作流
- Be 确定 to 类型检查 when you're 完成制造 a series of 代码 更改s
- Prefer 运行中 single 测试s, and not the 整体 测试 suite, for 性能
```

CLAUDE.md is 加载 每个 会话, so only include 薄gs that 应用ly broadly. For do主 知识 or 工作流s that are only 相关 一些时间s, 使用 [技能](/en/技能) instead. Claude 加载s them on 需求 without膨胀 每个 对话.

Keep it concise. For 每个 行, ask: *"Would removing this 原因 Claude to make 薄雾akes?"* If not, 剪切 it. Bloated CLAUDE.md 文件 原因 Claude to ignore your 实际 说明!

| ✅ Include                                            | ❌ Exclude                                          |
| ---------------------------------------------------- | -------------------------------------------------- |
| Bash 命令 Claude can't guess                     | 任何薄g Claude can 图 out by 读取ing 代码     |
| 代码 style 规则 that differ from 默认s           | 标准 language 惯例 Claude al就绪 knows |
| 测试 说明 and preferred 测试 运行ners      | 详情ed API 文档 (链接 to docs instead)  |
| 仓库 礼仪 (分支 naming, PR 惯例) | 信息 that 更改s 频繁ly                |
| 架构师ural 决定s 特定 to your 项目     | 长 解释s or 教程s                     |
| 开发者 环境 quirks (必需 env vars)     | 文件-by-文件 描述s of the 代码基础          |
| 常见 前往tchas or non-明显 behaviors              | Self-明显 实践 像 "写入 干净 代码"     |

If Claude keeps做 一些薄g you don't 想要 despite having a 规则 a收益st it, the 文件 is probably too 长 and the 规则 is getting 失败. If Claude asks you 问题s that are 答案ed in CLAUDE.md, the pH值rasing might be 模糊. Treat CLAUDE.md 像 代码: re视图 it when 薄gs 前往 错误, p运行e it 常规ly, and 测试 更改s by观察 whether Claude's behavior 实际ly shifts.

You can tune 说明 by添加 empH值asis (e.g., "重要" or "YOU MUST") to improve adherence. 检查 CLAUDE.md into Git so your 团队 can contribute. The 文件 compounds in 值 结束 时间.

CLAUDE.md 文件 can 进口 添加itional 文件 using `@路径/to/进口` syn税:

```mark下 CLAUDE.md 主题={空}
看见 @读取ME.md for 项目 概述 and @包.json for 可用 npm 命令.

# 添加itional 说明
- Git 工作流: @docs/Git-说明.md
- 个人 结束rides: @~/.claude/my-项目-说明.md
```

You can place CLAUDE.md 文件 in 几个 locations:

* **Home 文件夹 (`~/.claude/CLAUDE.md`)**: 应用lies to 所有 Claude 会话s
* **项目 根 (`./CLAUDE.md`)**: 检查 into Git to share with your 团队
* **项目 根 (`./CLAUDE.本地.md`)**: 个人 项目-特定 注意s; 添加 this 文件 to your `.Gitignore` so it isn't 共享 with your 团队
* **Parent 总监ies**: 有用 for monorepos where 机器人h `根/CLAUDE.md` and `根/foo/CLAUDE.md` are 拉取ed in 自动所有y
* **Child 总监ies**: Claude 拉取s in child CLAUDE.md 文件 on 需求 when工作 with 文件 in those 总监ies

### Con图 权限

  使用 [auto 模式](/en/许可-模式s#eliminate-及时s-with-auto-模式) to let a 阶级ifier handle 批准s, `/权限` to 允许列表 特定 命令, or `/沙box` for OS-级别 isolation. 每个 reduces 中断ions while保持 you in 控制.

By 默认, Claude 代码 请求s 许可 for 行动s that might 修改 your 系统: 文件 写入s, Bash 命令, MCP 工具, etc. This is 安全 but 乏味. 之后 the tenth 批准 you're not 真实ly回顾 任何更多, you're 公正 clicking th粗糙. There are three 方式s to reduce these 中断ions:

* **Auto 模式**: a 单独 阶级ifier 模型 re视图s 命令 and 块s only what看s 风险y: 范围 escalation, unknown 基础设施, or hos瓦-满意-driven 行动s. 最佳 when you 信任 the 一般 指导 of a 任务 but don't 想要 to click th粗糙 每个步骤
* **许可 允许列表s**: permit 特定 工具 you know are 安全, 像 `npm 运行 lint` or `Git 提交`
* **沙boxing**: 启用 OS-级别 isolation that re严格s 文件系统 and net工作 access,允许 Claude to 工作 更多 freely 在...内 定义 boundaries

读取 更多 about [许可 模式s](/en/许可-模式s), [许可 规则](/en/权限), and [沙boxing](/en/沙boxing).

### 使用 CLI 工具

  Tell Claude 代码 to 使用 CLI 工具 像 `gh`, `aws`, `g云`, and `s条目-cli` when interacting with 外部 服务s.

CLI 工具 are the 最多 上下文-高效 方式 to interact with 外部 服务s. If you 使用 GitHub, 安装 the `gh` CLI. Claude knows how to 使用 it for创建 问题s, 打开ing 拉取请求s, and 读取ing comments. Without `gh`, Claude can 静止 使用 the GitHub API, but 未认证 请求s often hit 速率 限制s.

Claude is also 有效 at learning CLI 工具 it doesn't al就绪 know. 尝试 及时s 像 `使用 'foo-cli-工具 --帮助' to learn about foo 工具, then 使用 it to solve A, B, C.`

### Connect MCP 服务器

  运行 `claude mcp 添加` to connect 外部 工具 像 概念, Figma, or your 数据库.

With [MCP 服务器](/en/mcp), you can ask Claude to 实现 features from 问题 跟踪ers, 查询 数据库s,分析监控 数据, integ速率 设计s from Figma, and automate 工作流s.

### 设置 上 钩子

  使用 钩子 for 行动s that must h应用en 每个 时间 with zero 异常.

[钩子](/en/钩子-指南) 运行 脚本s 自动所有y at 特定 points in Claude's 工作流. 不像 CLAUDE.md 说明 which are advisory, 钩子 are 确定性 and guarantee the 行动 h应用ens.

Claude can 写入 钩子 for you. 尝试 及时s 像 *"写入 a hook that 运行s eslint 之后 每个 文件 编辑"* or *"写入 a hook that 块s 写入s to the mig比率ns 文件夹."* 编辑 `.claude/设置.json` 直接ly to con图 钩子 by hand, and 运行 `/钩子` to 浏览 what's con图d.

### 创建技能

  创建 `技能.md` 文件 in `.claude/技能/` to give Claude do主 知识 and reus能够 工作流s.

[技能](/en/技能) ext结束 Claude's 知识 with 信息 特定 to your 项目, 团队, or do主. Claude 应用lies them 自动所有y when 相关, or you can invoke them 直接ly with `/技能-名称`.

创建 a 技能 by添加 a 目录 with a `技能.md` to `.claude/技能/`:

```mark下 .claude/技能/api-惯例/技能.md 主题={空}
---
名称: api-惯例
描述: REST API 设计 惯例 for our 服务s
---
# API 惯例
- 使用 kebab-案例 for URL 路径s
- 使用 camel案例 for JSON 适当ties
- Al方式s include pagination for 列表 结束points
- 版本 APIs in the URL 路径 (/v1/, /v2/)
```

技能 can also de好 repea表 工作流s you invoke 直接ly:

```mark下 .claude/技能/修复-问题/技能.md 主题={空}
---
名称: 修复-问题
描述: 修复 a GitHub 问题
禁用-模型-invocation: 真实
---
分析 and 修复 the GitHub 问题: $参数.

1. 使用 `gh 问题视图` to get the 问题 详情s
2. Understand the 问题 described in the 问题
3. 搜索 the 代码基础 for 相关 文件
4. 实现 the 必要 更改s to 修复 the 问题
5. 写入 and 运行 测试s to 验证 the 修复
6. En确定 代码 passes linting and 类型检查
7. 创建 a de脚本ive 提交 消息
8. 推送 and 创建 a PR
```

运行 `/修复-问题 1234` to invoke it. 使用 `禁用-模型-invocation: 真实` for 工作流s with side 效果s that you 想要 to trigger 手册ly.

### 创建 习俗 子代理

  De好 专业 assistants in `.claude/代理s/` that Claude can delegate to for iso晚d 任务s.

[子代理](/en/sub-代理s) 运行 in their own 上下文 with their own 设置 of 允许 工具. They're 有用 for 任务s that 读取 许多 文件 or 需要 专业 焦点 without cluttering your 主 对话.

```mark下 .claude/代理s/安全-审查员.md 主题={空}
---
名称: 安全-审查员
描述: Re视图s 代码 for 安全 vulner能力
工具: 读取, Grep, Glob, Bash
模型: opus
---
You are a 高级 安全 工程师. Re视图 代码 for:
- Injection vulner能力 (SQL, XSS, 命令 injection)
- 认证 and 授权 flaws
- 秘密s or credentials in 代码
- In安全 数据处理

Provide 特定 行 参考文献 and 建议 修复es.
```

Tell Claude to 使用 子代理 明确ly: *"使用 a sub代理 to re视图 this 代码 for 安全 问题s."*

### 安装 插件s

  运行 `/插件` to 浏览 the 市场place. 插件s 添加 技能, 工具, and 集成s without 配置.

[插件s](/en/插件s) 捆绑 技能, 钩子, 子代理, and MCP 服务器 into a single 安装能够 单位 from the 社区 and Anthropic. If you 工作 with a 类型化 language, 安装 a [代码 intelligence 插件](/en/disc结束-插件s#代码-intelligence) to give Claude 精确 符号 navigation and 自动 错误 检测 之后 编辑s.

For 指导 on选择 between 技能, 子代理, 钩子, and MCP,看见 [Ext结束 Claude 代码](/en/features-概述#匹配-features-to-your-目标).

***

## Communicate 有效ly

The 方式 you communicate with Claude 代码 重要ly 影响s the quality of 结果s.

### Ask 代码基础 问题s

  Ask Claude 问题s you'd ask a 高级 工程师.

When on董事会ing to a 新 代码基础, 使用 Claude 代码 for learning and 探索. You can ask Claude the 相同 类别s of 问题s you would ask another 工程师:

* How does日志 工作?
* How do I make a 新 API 结束point?
* What does `异步 移动 { ... }` do on 行 134 of `foo.rs`?
* What 边 案例s does `习俗erOn董事会ingF低Impl` handle?
* Why does this 代码 c所有 `foo()` instead of `bar()` on 行 333?

Using Claude 代码 this 方式 is an 有效 on董事会ing 工作流,改善 ramp-上 时间 and reducing 加载 on other 工程师s. No 特殊 及时ing 必需: ask 问题s 直接ly.

### Let Claude inter视图 you

  For larger features, have Claude inter视图 you 第一个. 启动 with a 最小 及时 and ask Claude to inter视图 you using the `Ask用户问题` 工具.

Claude asks about 薄gs you might not have considered yet,包括 技术 实施, UI/UX, 边 案例s, and 贸易offs.

```文本  主题={空}
I 想要 to 构建 [简短 描述]. Inter视图 me in 详情 using the Ask用户问题 工具.

Ask about 技术 实施, UI/UX, 边 案例s, 关心s, and 贸易offs. Don't ask 明显 问题s, dig into the 硬 部分s I might not have considered.

Keep inter视图ing until we've c结束ed 每个薄g, then 写入 a 完成 规格 to 规格.md.
```

Once the 规格 is 完成, 启动 a 新鲜 会话 to 执行 it. The 新 会话 has 干净 上下文 焦点ed 整个ly on 实施, and you have a written 规格 to 参考.

***

## Manage your 会话

对话s are 持续 and reversible. 使用 this to your 优势!

### Course-正确 早 and often

  正确 Claude as soon as you 通知 it 前往ing off 跟踪.

The 最佳 结果s come from 紧 反馈 loops. Though Claude 偶尔ly solves 问题s 完美ly on the 第一个 attempt, 正确ing it 快ly 一般ly produces 更好 解决方案 快er.

* **`Esc`**: 停止 Claude mid-行动 with the `Esc` 键. 上下文 is 保存, so you can 重定向.
* **`Esc + Esc` or `/re风`**: press `Esc` twice or 运行 `/re风` to 打开 the re风 menu and 恢复 之前 对话 and 代码 州, or 总和marize from a 选择ed 消息.
* **`"撤销 that"`**: have Claude 撤销 its 更改s.
* **`/清楚`**: 重置 上下文 between unr兴高采烈 任务s. 长 会话s with 不相关 上下文 can reduce 性能.

If you've 正确ed Claude 更多 than twice on the 相同 问题 in one 会话, the 上下文 is cluttered with 失败 方法es. 运行 `/清楚` and 启动 新鲜 with a 更多 特定 及时 that incorpo速率s what you 学习. A 干净 会话 with a 更好 及时 al最多 al方式s outper形式s a 长 会话 with 积累 正确ions.

### Manage 上下文 aggressively

  运行 `/清楚` between unr兴高采烈 任务s to 重置 上下文.

Claude 代码 自动所有y 紧凑s 对话 history when you 方法 上下文 限制s, which preserves 重要 代码 and 决定s while freeing s步伐.

期间 长 会话s, Claude's 上下文 风ow can fill with 不相关 对话, 文件 满意s, and 命令. This can reduce 性能 and 一些时间s distract Claude.

* 使用 `/清楚` 频繁ly between 任务s to 重置 the 上下文 风ow 整个ly
* When auto 紧凑ion triggers, Claude 总和marizes what 事情s 最多,包括 代码 模式s, 文件 州s, and 键 决定s
* For 更多 控制, 运行 `/紧凑 `, 像 `/紧凑 焦点 on the API 更改s`
* To 紧凑 only 部分 of the 对话, 使用 `Esc + Esc` or `/re风`, 选择 a 消息 检查point, and choose **总和marize from here**. This con密集s 消息s from that point 前进 while保持 更早 上下文 完整.
* 习俗ize 紧凑ion behavior in CLAUDE.md with 说明 像 `"When 紧凑ing, al方式s preserve the 满 列表 of modified 文件 and 任何 测试 命令"` to en确定 关键 上下文 survives 总和marization
* For 快 问题s that don't 需要 to stay in 上下文, 使用 [`/btw`](/en/交互-模式#side-问题s-with-btw). The 答案 应用ears in a dismissible 结束lay and never 进入s 对话 history, so you can 检查 a 详情 without生长 上下文.

### 使用 子代理 for 调查

  Delegate研究 with `"使用 子代理 to调查 X"`. They探索 in a 单独 上下文,保持 your 主 对话 干净 for 实施.

Since 上下文 is your 资金a心理 常量雨t, 子代理 are one of the 最多 强大 工具 可用. When Claude研究es a 代码基础 it 读取s 很多s of 文件, 所有 of which con总和e your 上下文. 子代理 运行 in 单独 上下文 风ows and 报告 返回 总和maries:

```文本  主题={空}
使用 子代理 to调查 how our 认证 系统 handles 令牌
刷新, and whether we have 任何 现有 OAuth utilities I should re使用.
```

The sub代理探索s the 代码基础, 读取s 相关 文件, and 报告s 返回 with发现s, 所有 without cluttering your 主 对话.

You can also 使用 子代理 for 验证 之后 Claude 实现s 一些薄g:

```文本  主题={空}
使用 a sub代理 to re视图 this 代码 for 边 案例s
```

### Re风 with 检查points

  每个 行动 Claude makes 创建s a 检查point. You can 恢复 对话, 代码, or 机器人h to 任何 之前 检查point.

Claude 自动所有y 检查points 之前 更改s. 双精度-tap `Escape` or 运行 `/re风` to 打开 the re风 menu. You can 恢复 对话 only, 恢复 代码 only, 恢复 机器人h, or 总和marize from a 选择ed 消息.看见 [检查pointing](/en/检查pointing) for 详情s.

Instead of 小心ly 规划 每个 移动, you can tell Claude to 尝试 一些薄g 风险y. If it doesn't 工作, re风 and 尝试 a 不同 方法. 检查points persist across 会话s, so you can 关闭 your 终端 and 静止 re风 更晚.

  检查points only 跟踪 更改s made *by Claude*, not 外部 流程es. This isn't a 替换ment for Git.

### 恢复 对话s

  运行 `claude --继续` to pick 上 where you 左 off, or `--恢复` to choose from 最近 会话s.

Claude 代码 保存s 对话s 本地ly. When a 任务 跨度s mul提示le 会话s, you don't have to re-ex简单 the 上下文:

```bash  主题={空}
claude --继续    # 恢复 the 最多 最近 对话
claude --恢复      # 选择 from 最近 对话s
```

使用 `/重命名` to give 会话s de脚本ive 名称s 像 `"oauth-mig比率n"` or `"调试ging-记忆-leak"` so you can 查找 them 更晚. Treat 会话s 像 分支es: 不同 工作流s can have 单独, 持续 上下文s.

***

## Automate and scale

Once you're 有效 with one Claude, mul提示ly your 输出 with 平行 会话s, non-交互 模式, and fan-out 模式s.

每个薄g so far as总和es one 人类, one Claude, and one 对话. But Claude 代码 scales 水平ly. The 技术 in this 节 显示 how you can get 更多 完成.

### 运行 non-交互 模式

  使用 `claude -p "及时"` in CI, pre-提交 钩子, or 脚本s. 添加 `--输出-格式 流-json` for 流ing JSON 输出.

With `claude -p "your 及时"`, you can 运行 Claude non-交互ly, without a 会话. Non-交互 模式 is how you integ速率 Claude into CI 管道s, pre-提交 钩子, or 任何 automated 工作流. The 输出 格式s let you parse 结果s 计划matic所有y: 简单 文本, JSON, or 流ing JSON.

```bash  主题={空}
# One-off queries
claude -p "Ex简单 what this 项目 does"

# 结构化 输出 for 脚本s
claude -p "列表 所有 API 结束points" --输出-格式 json

# 流ing for 真实-时间 处理中
claude -p "分析 this 日志 文件" --输出-格式 流-json
```

### 运行 mul提示le Claude 会话s

  运行 mul提示le Claude 会话s in 平行 to 速度 上 开发, 运行 iso晚d 实验s, or 启动 复杂 工作流s.

There are three 主 方式s to 运行 平行 会话s:

* [Claude 代码 桌面 应用](/en/桌面#工作-in-平行-with-会话s): Manage mul提示le 本地 会话s visu所有y. 每个 会话 gets its own iso晚d 工作树.
* [Claude 代码 on the 网页](/en/claude-代码-on-the-网页): 运行 on Anthropic's 安全 云 基础设施 in iso晚d VMs.
* [代理 团队](/en/代理-团队): Automated 协调 of mul提示le 会话s with 共享 任务s, 混乱aging, and a 团队 领导.

超出 平行izing 工作, mul提示le 会话s 启用 quality-焦点ed 工作流s. A 新鲜 上下文 improves 代码 re视图 since Claude 获胜't be 有偏见 toward 代码 it 公正 wrote.

For 示例, 使用 a 写入器/审查员 模式:

| 会话 A (写入器)                                                      | 会话 B (审查员)                                                                                                                                                     |
| ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `实现 a 速率 限制er for our API 结束points`                        |                                                                                                                                                                          |
|                                                                         | `Re视图 the 速率 限制er 实施 in @src/中间件/速率限制er.ts.寻找 边 案例s, 种族 条件, and 一致 with our 现有 中间件 模式s.` |
| `Here's the re视图 反馈: [会话 B 输出]. 添加ress these 问题s.` |                                                                                                                                                                          |

You can do 一些薄g 相似 with 测试s: have one Claude 写入 测试s, then another 写入 代码 to pass them.

### Fan out across 文件

  Loop th粗糙 任务s c所有ing `claude -p` for 每个. 使用 `--允许工具` to 范围 权限 for batch 运营.

For large mig比率ns or analyses, you can distribute 工作 across 许多 平行 Claude invocations:

  
    Have Claude 列表 所有 文件 that 需要 mig评级 (e.g., `列表 所有 2,000 Python 文件 that 需要 mig评级`)
  

  
    ```bash  主题={空}
    for 文件 in $(cat 文件.txt); do
      claude -p "Mig速率 $文件 from React to Vue. 回报 OK or FAIL." \
        --允许工具 "编辑,Bash(Git 提交 *)"
    完成
    ```
  

  
    Re好 your 及时 基础d on what 前往es 错误 with the 第一个 2-3 文件, then 运行 on the 满 设置. The `--允许工具` flag re严格s what Claude can do, which 事情s when you're 运行中 unatt结束.
  

You can also integ速率 Claude into 现有 数据/处理中 管道s:

```bash  主题={空}
claude -p "" --输出-格式 json | your_命令
```

使用 `--详细` for 调试ging 期间 开发, and turn it off in 生产.

### 运行 自治ly with auto 模式

For un中断ed 执行 with 背景 安全 检查s, 使用 [auto 模式](/en/许可-模式s#eliminate-及时s-with-auto-模式). A 阶级ifier 模型 re视图s 命令 之前 they 运行, 阻塞 范围 escalation, unknown 基础设施, and hos瓦-满意-driven 行动s while让 常规 工作 proceed without 及时s.

```bash  主题={空}
claude --许可-模式 auto -p "修复 所有 lint 错误"
```

For non-交互 运行s with the `-p` flag, auto 模式 中止s if the 阶级ifier repeatedly 块s 行动s, since there is no 用户 to 下降 返回 to.看见 [when auto 模式 下降s 返回](/en/许可-模式s#when-auto-模式-下降s-返回) for 阈值s.

***

## A空白 常见 失败 模式s

These are 常见 薄雾akes.认可 them 早 保存s 时间:

* **The kitchen sink 会话.** You 启动 with one 任务, then ask Claude 一些薄g unr兴高采烈, then 前往 返回 to the 第一个 任务. 上下文 is 满 of 不相关 信息.
  > **修复**: `/清楚` between unr兴高采烈 任务s.
* **正确ing 结束 and 结束.** Claude does 一些薄g 错误, you 正确 it, it's 静止 错误, you 正确 a收益. 上下文 is 污染 with 失败 方法es.
  > **修复**: 之后 two 失败 正确ions, `/清楚` and 写入 a 更好 initial 及时合并 what you 学习.
* **The 结束-指定 CLAUDE.md.** If your CLAUDE.md is too 长, Claude ignores half of it be原因 重要 规则 get 失败 in the 噪音.
  > **修复**: 无情ly p运行e. If Claude al就绪 does 一些薄g 正确ly without the instruction, 删除 it or 转换 it to a hook.
* **The 信任-then-验证 gap.** Claude produces a plausible-看 实施 that doesn't handle 边 案例s.
  > **修复**: Al方式s provide 验证 (测试s, 脚本s, 截图s). If you can't 验证 it, don't ship it.
* **The infinite 探索.** You ask Claude to "调查" 一些薄g without scoping it. Claude 读取s hundreds of 文件, filling the 上下文.
  > **修复**: 范围 调查s nar行ly or 使用 子代理 so the 探索 doesn't con总和e your 主 上下文.

***

## Develop your 直觉

The 模式s in this 指南 aren't 设置 in 石头. They're 启动ing points that 工作 well in 一般, but might not be 最优 for 每个 situation.

一些时间s you *should* let 上下文 accumu晚 be原因 you're 深 in one 复杂 问题 and the history is 有价值. 一些时间s you should跳 规划 and let Claude 图 it out be原因 the 任务 is 探索. 一些时间s a 模糊 及时 is 精确ly 正确 be原因 you 想要 to看见 how Claude 解释s the 问题 之前 常量雨ing it.

Pay 注意 to what 工作s. When Claude produces great 输出, 通知 what you did: the 及时 结构, the 上下文 you provided, the 模式 you were in. When Claude struggles, ask why. Was the 上下文 too noisy? The 及时 too 模糊? The 任务 too big for one pass?

结束 时间, you'll develop 直觉 that no 指南 can capture. You'll know when to be 特定 and when to be 打开-结束, when to 计划 and when to探索, when to 清楚 上下文 and when to let it accumu晚.

## R兴高采烈 资源

* [How Claude 代码 工作s](/en/how-claude-代码-工作s): the 代理ic loop, 工具, and 上下文 管理
* [Ext结束 Claude 代码](/en/features-概述): 技能, 钩子, MCP, 子代理, and 插件s
* [通用工作流](/en/常见-工作流s):步骤-by-步骤 recipes for 调试ging, 测试, PRs, and 更多
* [CLAUDE.md](/en/记忆): store 项目 惯例 and 持续 上下文