← 返回文章列表
2025-04-18

Claude Code 最佳实践

Best practices for Claude Code

Best practices for Claude Code

On this page

本页内容

  • Give Claude a way to verify its work
  • Explore first, then plan, then code
  • Provide specific context in your promptsProvide rich content
  • Provide rich content
  • Configure your environmentWrite an effective CLAUDE.mdConfigure permissionsUse CLI toolsConnect MCP serversSet up hooksCreate skillsCreate custom subagentsInstall plugins
  • Write an effective CLAUDE.md
  • Configure permissions
  • Use CLI tools
  • Connect MCP servers
  • Set up hooks
  • Create skills
  • Create custom subagents
  • Install plugins
  • Communicate effectivelyAsk codebase questionsLet Claude interview you
  • Ask codebase questions
  • Let Claude interview you
  • Manage your sessionCourse-correct early and oftenManage context aggressivelyUse subagents for investigationRewind with checkpointsResume conversations
  • Course-correct early and often
  • Manage context aggressively
  • Use subagents for investigation
  • Rewind with checkpoints
  • Resume conversations
  • Automate and scaleRun non-interactive modeRun multiple Claude sessionsFan out across filesRun autonomously with auto modeAdd an adversarial review step
  • Run non-interactive mode
  • Run multiple Claude sessions
  • Fan out across files
  • Run autonomously with auto mode
  • Add an adversarial review step
  • Avoid common failure patterns
  • Develop your intuition
  • Related resources
  • 让 Claude 能够验证自己的工作
  • 先探索,再规划,然后编码
  • 在提示中提供具体上下文提供丰富内容
  • 提供丰富内容
  • 配置你的环境编写有效的 CLAUDE.md配置权限使用 CLI 工具连接 MCP 服务器设置钩子创建技能创建自定义子智能体安装插件
  • 编写有效的 CLAUDE.md
  • 配置权限
  • 使用 CLI 工具
  • 连接 MCP 服务器
  • 设置钩子
  • 创建技能
  • 创建自定义子智能体
  • 安装插件
  • 有效沟通提出代码库相关问题让 Claude 访谈你
  • 提出代码库相关问题
  • 让 Claude 访谈你
  • 管理你的会话尽早并频繁地修正方向积极管理上下文使用子智能体进行调查通过检查点回退恢复会话
  • 尽早并频繁地修正方向
  • 积极管理上下文
  • 使用子智能体进行调查
  • 通过检查点回退
  • 恢复会话
  • 自动化与规模化运行非交互模式运行多个 Claude 会话跨文件扇出执行使用自动模式自主运行添加对抗性审查步骤
  • 运行非交互模式
  • 运行多个 Claude 会话
  • 跨文件扇出执行
  • 使用自动模式自主运行
  • 添加对抗性审查步骤
  • 避免常见的失败模式
  • 培养你的直觉
  • 相关资源

Best practices for Claude Code

Claude Code 最佳实践

Tips and patterns for getting the most out of Claude Code, from configuring your environment to scaling across parallel sessions.

充分利用 Claude Code 的技巧和模式,从配置环境到跨并行会话进行规模化。

​Give Claude a way to verify its work

让 Claude 能够验证自己的工作

  • In one prompt: ask Claude to run the check and iterate in the same message, as in the table above.
  • Across a session: set the check as a/goalcondition. A separate evaluator re-checks it after every turn and Claude keeps working until it holds.
  • 在单个提示中:要求 Claude 在同一条消息中运行检查并进行迭代,如上表所示。
  • 跨会话:将检查设置为 /goal 条件。一个独立的评估器会在每轮对话后重新检查,Claude 会持续工作直到条件满足。
  • 作为确定性门控:一个 Stop 钩子(Hook)将你的检查作为脚本运行,并阻止当前轮次结束直到检查通过。Claude Code 在连续 8 次阻止后会覆盖该钩子并结束轮次。
  • 通过第二意见:一个验证子智能体(Subagent)或动态工作流来检查自身的发现,让一个全新的模型尝试反驳结果,这样执行工作的智能体不是评分的那个。

/goal

先探索,再规划,然后编码

  • As a deterministic gate: aStop hookruns your check as a script and blocks the turn from ending until it passes. Claude Code overrides the hook and ends the turn after 8 consecutive blocks.
  • By a second opinion: averification subagentor adynamic workflowthat checks its own findings has a fresh model try to refute the result, so the agent doing the work isn’t the one grading it.

探索

/goal

read /src/auth and understand how we handle sessions and login.also look at how we manage environment variables for secrets.

​Explore first, then plan, then code

规划

Explore

I want to add Google OAuth. What files need to change?What's the session flow? Create a plan.
read /src/auth and understand how we handle sessions and login.also look at how we manage environment variables for secrets.

read /src/auth and understand how we handle sessions and login.also look at how we manage environment variables for secrets. Plan

implement the OAuth flow from your plan. write tests for thecallback handler, run the test suite and fix any failures.
I want to add Google OAuth. What files need to change?What's the session flow? Create a plan.

I want to add Google OAuth. What files need to change?What's the session flow? Create a plan. Ctrl+G Implement

commit with a descriptive message and open a PR
implement the OAuth flow from your plan. write tests for thecallback handler, run the test suite and fix any failures.

implement the OAuth flow from your plan. write tests for thecallback handler, run the test suite and fix any failures. Commit

"你会对这个文件做哪些改进?"

commit with a descriptive message and open a PR

commit with a descriptive message and open a PR

  • 使用 @ 引用文件,而不是描述代码的位置。Claude 会在响应之前读取该文件。
  • 直接粘贴图片。将图片复制/粘贴或拖放到提示中。
  • 提供文档和 API 参考的 URL。使用 /permissions 将常用域名加入允许列表。
  • 通过运行 cat error.log | claude 管道传入数据,直接发送文件内容。
  • 让 Claude 获取所需内容。告诉 Claude 使用 Bash 命令、MCP 工具或读取文件来自行拉取上下文。

​Provide specific context in your prompts

配置你的环境

"what would you improve in this file?"

编写有效的 CLAUDE.md

​Provide rich content

/init

@

# Code style-Use ES modules (import/export) syntax, not CommonJS (require)-Destructure imports when possible (eg. import { foo } from 'bar')# Workflow-Be sure to typecheck when you're done making a series of code changes-Prefer running single tests, and not the whole test suite, for performance
  • Reference files with@instead of describing where code lives. Claude reads the file before responding.

@path/to/import

@

See @README.md for project overview and @package.json for available npm commands.# Additional Instructions-Git workflow: @docs/git-instructions.md-Personal overrides: @~/.claude/my-project-instructions.md
  • Paste images directly. Copy/paste or drag and drop images into the prompt.
  • Give URLsfor documentation and API references. Use/permissionsto allowlist frequently-used domains.
  • 主文件夹(~/.claude/CLAUDE.md):适用于所有 Claude 会话
  • 项目根目录(./CLAUDE.md):提交到 git 以便与团队共享
  • 项目根目录(./CLAUDE.local.md):个人的项目专用笔记;将此文件添加到 .gitignore 中,避免与团队共享
  • 父目录:适用于 monorepo,root/CLAUDE.md 和 root/foo/CLAUDE.md 都会被自动加载
  • 子目录:当 Claude 读取子目录中的文件时,会按需加载子目录的 CLAUDE.md 文件

/permissions

配置权限

  • Pipe in databy runningcat error.log | claudeto send file contents directly.

/permissions

cat error.log | claude

  • 自动模式:一个独立的分类器模型审查命令,仅阻止看起来有风险的操作:权限范围升级、未知基础设施或恶意内容驱动的操作。当你信任任务的大致方向但不想逐步点击确认时,这是最佳选择
  • 权限允许列表:允许你已知安全的特定工具,如 npm run lint 或 git commit
  • 沙箱化:启用操作系统级别的隔离来限制文件系统和网络访问,允许 Claude 在定义的边界内更自由地工作
  • Let Claude fetch what it needs. Tell Claude to pull context itself using Bash commands, MCP tools, or by reading files.

使用 CLI 工具

​Configure your environment

gh、aws、gcloud、sentry-cli 等工具都经过了优化。

​Write an effective CLAUDE.md

使用 'foo-cli-tool --help' 来了解 foo 工具,然后用它来解决 A、B、C 问题。

/init /init

连接 MCP 服务器

# Code style-Use ES modules (import/export) syntax, not CommonJS (require)-Destructure imports when possible (eg. import { foo } from 'bar')# Workflow-Be sure to typecheck when you're done making a series of code changes-Prefer running single tests, and not the whole test suite, for performance

Code style-Use ES modules (import/export) syntax, not CommonJS (require)-Destructure imports when possible (eg. import { foo } from 'bar')# Workflow-Be sure to typecheck when you're done making a series of code changes-Prefer running single tests, and not the whole test suite, for performance

设置钩子

@path/to/import

在 .claude/settings.json 中配置 /hooks。

See @README.md for project overview and @package.json for available npm commands.# Additional Instructions-Git workflow: @docs/git-instructions.md-Personal overrides: @~/.claude/my-project-instructions.md

See @README.md for project overview and @package.json for available npm commands.# Additional Instructions-Git workflow: @docs/git-instructions.md-Personal overrides: @~/.claude/my-project-instructions.md

技能(Skill)以 SKILL.md 文件的形式存在于 .claude/skills/ 目录中,可以通过 /skill-name 来调用。

  • Home folder (~/.claude/CLAUDE.md): applies to all Claude sessions
---name:api-conventionsdescription:REST API design conventions for our services---# API Conventions-Use kebab-case for URL paths-Use camelCase for JSON properties-Always include pagination for list endpoints-Version APIs in the URL path (/v1/, /v2/)

~/.claude/CLAUDE.md

---name:fix-issuedescription:Fix a GitHub issuedisable-model-invocation:true---Analyze and fix the GitHub issue: $ARGUMENTS.1.Use`gh issue view`to get the issue details2.Understand the problem described in the issue3.Search the codebase for relevant files4.Implement the necessary changes to fix the issue5.Write and run tests to verify the fix6.Ensure code passes linting and type checking7.Create a descriptive commit message8.Push and create a PR
  • Project root (./CLAUDE.md): check into git to share with your team

你可以使用 /fix-issue 1234 来调用此技能。设置 disable-model-invocation: true 可以跳过模型调用,直接执行技能中的步骤。

./CLAUDE.md

创建自定义子智能体

  • Project root (./CLAUDE.local.md): personal project-specific notes; add this file to your.gitignoreso it isn’t shared with your team

子智能体(Subagent)定义在 .claude/agents/ 目录中。

./CLAUDE.local.md .gitignore

---name:security-reviewerdescription:Reviews code for security vulnerabilitiestools:Read, Grep, Glob, Bashmodel:opus---You are a senior security engineer. Review code for:-Injection vulnerabilities (SQL, XSS, command injection)-Authentication and authorization flaws-Secrets or credentials in code-Insecure data handlingProvide specific line references and suggested fixes.
  • Parent directories: useful for monorepos where bothroot/CLAUDE.mdandroot/foo/CLAUDE.mdare pulled in automatically

安装插件

root/CLAUDE.md root/foo/CLAUDE.md

使用 /plugin 来安装和管理插件。

  • Child directories: Claude pulls in child CLAUDE.md files on demand when it reads a file in those directories

有效沟通

​Configure permissions

提出代码库相关问题

/permissions /sandbox

  • 日志系统是如何工作的?
  • 如何创建一个新的 API 端点?
  • foo.rs 第 134 行的 async move { ... } 是做什么的?
  • CustomerOnboardingFlowImpl 处理了哪些边缘情况?
  • 为什么这段代码在第 333 行调用 foo() 而不是 bar()?
  • Auto mode: a separate classifier model reviews commands and blocks only what looks risky: scope escalation, unknown infrastructure, or hostile-content-driven actions. Best when you trust the general direction of a task but don’t want to click through every step
  • Permission allowlists: permit specific tools you know are safe, likenpm run lintorgit commit

让 Claude 访谈你

npm run lint git commit

使用 AskUserQuestion 工具来让 Claude 对你进行深入访谈。

  • Sandboxing: enable OS-level isolation that restricts filesystem and network access, allowing Claude to work more freely within defined boundaries
I want to build [brief description]. Interview me in detail using the AskUserQuestion tool.Ask about technical implementation, UI/UX, edge cases, concerns, and tradeoffs. Don't ask obvious questions, dig into the hard parts I might not have considered.Keep interviewing until we've covered everything, then write a complete spec to SPEC.md.

​Use CLI tools

管理你的会话

gh aws gcloud sentry-cli gh gh Use 'foo-cli-tool --help' to learn about foo tool, then use it to solve A, B, C.

尽早并频繁地修正方向

​Connect MCP servers

  • Esc:在 Claude 执行过程中按 Esc 键停止。上下文会被保留,因此你可以重新引导方向。
  • Esc + Esc 或 /rewind:按两次 Esc 或运行 /rewind 来打开回退菜单,恢复之前的对话和代码状态,或从选定消息开始总结。
  • "撤销那个操作":让 Claude 回退其更改。
  • /clear:在不相关的任务之间重置上下文。包含不相关上下文的长会话可能会降低性能。

claude mcp add

积极管理上下文

​Set up hooks

  • 在任务之间频繁使用 /clear 来完全重置上下文窗口
  • 当自动压缩触发时,Claude 会总结最重要的内容,包括代码模式、文件状态和关键决策
  • 如需更多控制,运行 /compact <instructions>,例如 /compact Focus on the API changes
  • 要仅压缩对话的一部分,使用 Esc + Esc 或 /rewind,选择一个消息检查点,然后选择"从此处总结"或"总结到此处"。前者压缩从该点之后的消息同时保留早期上下文不变;后者压缩早期消息同时完整保留最近的消息。请参阅"恢复与总结"。
  • 在 CLAUDE.md 中自定义压缩行为,例如添加指令"压缩时,始终保留完整的已修改文件列表和所有测试命令",以确保关键上下文在总结过程中得以保留
  • 对于不需要保留在上下文中的快速问题,使用 /btw。答案会显示在可关闭的叠加层中,并且永远不会进入对话历史,因此你可以在不增加上下文的情况下查看细节。

.claude/settings.json /hooks

使用子智能体进行调查

​Create skills

使用子智能体(Subagent)来调查问题。

SKILL.md .claude/skills/ /skill-name SKILL.md .claude/skills/

Use subagents to investigate how our authentication system handles tokenrefresh, and whether we have any existing OAuth utilities I should reuse.
---name:api-conventionsdescription:REST API design conventions for our services---# API Conventions-Use kebab-case for URL paths-Use camelCase for JSON properties-Always include pagination for list endpoints-Version APIs in the URL path (/v1/, /v2/)

---name:api-conventionsdescription:REST API design conventions for our services---# API Conventions-Use kebab-case for URL paths-Use camelCase for JSON properties-Always include pagination for list endpoints-Version APIs in the URL path (/v1/, /v2/)

通过检查点回退

---name:fix-issuedescription:Fix a GitHub issuedisable-model-invocation:true---Analyze and fix the GitHub issue: $ARGUMENTS.1.Use`gh issue view`to get the issue details2.Understand the problem described in the issue3.Search the codebase for relevant files4.Implement the necessary changes to fix the issue5.Write and run tests to verify the fix6.Ensure code passes linting and type checking7.Create a descriptive commit message8.Push and create a PR

---name:fix-issuedescription:Fix a GitHub issuedisable-model-invocation:true---Analyze and fix the GitHub issue: $ARGUMENTS.1.Usegh issue viewto get the issue details2.Understand the problem described in the issue3.Search the codebase for relevant files4.Implement the necessary changes to fix the issue5.Write and run tests to verify the fix6.Ensure code passes linting and type checking7.Create a descriptive commit message8.Push and create a PR /fix-issue 1234 disable-model-invocation: true

恢复会话

​Create custom subagents

使用 /rename 为会话命名,使用 claude --continue 继续最近的会话,使用 claude --resume 恢复特定会话(例如 claude --resume oauth-migration)。

.claude/agents/

自动化与规模化

---name:security-reviewerdescription:Reviews code for security vulnerabilitiestools:Read, Grep, Glob, Bashmodel:opus---You are a senior security engineer. Review code for:-Injection vulnerabilities (SQL, XSS, command injection)-Authentication and authorization flaws-Secrets or credentials in code-Insecure data handlingProvide specific line references and suggested fixes.

---name:security-reviewerdescription:Reviews code for security vulnerabilitiestools:Read, Grep, Glob, Bashmodel:opus---You are a senior security engineer. Review code for:-Injection vulnerabilities (SQL, XSS, command injection)-Authentication and authorization flaws-Secrets or credentials in code-Insecure data handlingProvide specific line references and suggested fixes.

使用 claude -p "prompt" 来运行非交互模式。可以配合 --output-format stream-json --verbose 使用。

​Install plugins

# One-off queriesclaude-p"Explain what this project does"# Structured output for scriptsclaude-p"List all API endpoints"--output-formatjson# Streaming for real-time processingclaude-p"Analyze this log file"--output-formatstream-json--verbose

/plugin

运行多个 Claude 会话

​Communicate effectively

  • 工作树(Worktree):在隔离的 git 检出中运行独立的 CLI 会话,避免编辑冲突
  • 桌面应用:可视化管理多个本地会话,每个会话在自己的工作树中
  • Web 版 Claude Code:在 Anthropic 管理的云基础设施上的隔离虚拟机中运行会话
  • 智能体团队(Agent Team):多个会话的自动化协调,具有共享任务、消息传递和团队负责人

​Ask codebase questions

实现流程示例:

  • How does logging work?
  • How do I make a new API endpoint?
  • What doesasync move { ... }do on line 134 offoo.rs?
  1. 智能体 A:为我们的 API 端点实现一个速率限制器
  2. 智能体 B:审查 @src/middleware/rateLimiter.ts 中的速率限制器实现。查找边缘情况、竞态条件以及与现有中间件模式的一致性
  3. 智能体 A:以下是审查反馈:[会话 B 输出]。解决这些问题

async move { ... } foo.rs

跨文件扇出执行

  • What edge cases doesCustomerOnboardingFlowImplhandle?

使用 claude -p 配合 --allowedTools 来跨多个文件扇出执行任务。

CustomerOnboardingFlowImpl

步骤:

  • Why does this code callfoo()instead ofbar()on line 333?
  1. 生成任务列表:列出所有需要迁移的 2,000 个 Python 文件
  2. 编写脚本来循环处理列表

foo() bar()

forfilein$(catfiles.txt);doclaude-p"Migrate$filefrom React to Vue. Return OK or FAIL."\--allowedTools"Edit,Bash(git commit *)"done

​Let Claude interview you

先在几个文件上测试,然后大规模运行。使用 --allowedTools 来限制可用工具。

AskUserQuestion

claude-p"<your prompt>"--output-formatjson|your_command
I want to build [brief description]. Interview me in detail using the AskUserQuestion tool.Ask about technical implementation, UI/UX, edge cases, concerns, and tradeoffs. Don't ask obvious questions, dig into the hard parts I might not have considered.Keep interviewing until we've covered everything, then write a complete spec to SPEC.md.

I want to build [brief description]. Interview me in detail using the AskUserQuestion tool.Ask about technical implementation, UI/UX, edge cases, concerns, and tradeoffs. Don't ask obvious questions, dig into the hard parts I might not have considered.Keep interviewing until we've covered everything, then write a complete spec to SPEC.md.

使用自动模式自主运行

​Manage your session

claude--permission-modeauto-p"fix all lint errors"

​Course-correct early and often

使用 -p 标志来指定提示。

  • Esc: stop Claude mid-action with theEsckey. Context is preserved, so you can redirect.

添加对抗性审查步骤

Esc Esc

使用 /code-review 或子智能体来添加对抗性审查。

  • Esc + Escor/rewind: pressEsctwice or run/rewindto open the rewind menu and restore previous conversation and code state, or summarize from a selected message.
Use a subagent to review the rate limiter diff against PLAN.md. Check thatevery requirement is implemented, the listed edge cases have tests, andnothing outside the task's scope changed. Report gaps, not style preferences.

Esc + Esc /rewind Esc /rewind

避免常见的失败模式

  • "Undo that": have Claude revert its changes.
  • 大杂烩会话。你从一个任务开始,然后问 Claude 一些不相关的事情,再回到第一个任务。上下文中充满了不相关的信息。

"Undo that"

修复方法:在不相关的任务之间使用 /clear。

  • /clear: reset context between unrelated tasks. Long sessions with irrelevant context can reduce performance.
  • 反复纠正。Claude 做错了某事,你纠正它,它仍然做错,你再次纠正。上下文被失败的方法污染了。

/clear /clear

修复方法:在两次失败的纠正后,使用 /clear 并根据你学到的经验编写一个更好的初始提示。

​Manage context aggressively

  • 过度指定的 CLAUDE.md。如果你的 CLAUDE.md 太长,Claude 会忽略其中一半,因为重要的规则在噪音中丢失了。

/clear

修复方法:果断精简。如果 Claude 在没有指令的情况下已经正确执行了某项操作,就删除它或将其转换为钩子。

  • Use/clearfrequently between tasks to reset the context window entirely
  • 信任后验证的差距。Claude 生成了一个看似合理的实现,但没有处理边缘情况。

/clear

修复方法:始终提供验证(测试、脚本、截图)。如果你无法验证它,就不要发布它。

  • When auto compaction triggers, Claude summarizes what matters most, including code patterns, file states, and key decisions
  • For more control, run/compact <instructions>, like/compact Focus on the API changes
  • 无限探索。你让 Claude "调查"某事但没有限定范围。Claude 读取了数百个文件,填满了上下文。

/compact <instructions> /compact Focus on the API changes

修复方法:将调查范围缩小或使用子智能体,这样探索不会消耗你的主上下文。

  • To compact only part of the conversation, useEsc + Escor/rewind, select a message checkpoint, and chooseSummarize from hereorSummarize up to here. The first condenses messages from that point forward while keeping earlier context intact; the second condenses earlier messages while keeping recent ones in full. SeeRestore vs. summarize.

培养你的直觉

Esc + Esc /rewind

相关资源

  • Customize compaction behavior in CLAUDE.md with instructions like"When compacting, always preserve the full list of modified files and any test commands"to ensure critical context survives summarization
  • Claude Code 的工作原理:智能体循环、工具和上下文管理
  • 扩展 Claude Code:技能、钩子、MCP、子智能体和插件
  • 常见工作流:调试、测试、PR 等的分步指南
  • CLAUDE.md:存储项目约定和持久化上下文

"When compacting, always preserve the full list of modified files and any test commands"

本页是否对你有帮助?

  • For quick questions that don’t need to stay in context, use/btw. The answer appears in a dismissible overlay and never enters conversation history, so you can check a detail without growing context.

/btw

​Use subagents for investigation

"use subagents to investigate X"

Use subagents to investigate how our authentication system handles tokenrefresh, and whether we have any existing OAuth utilities I should reuse.

Use subagents to investigate how our authentication system handles tokenrefresh, and whether we have any existing OAuth utilities I should reuse.

use a subagent to review this code for edge cases

use a subagent to review this code for edge cases

​Rewind with checkpoints

Escape /rewind

​Resume conversations

/rename claude --continue claude --resume oauth-migration

​Automate and scale

​Run non-interactive mode

claude -p "prompt" --output-format stream-json --verbose claude -p "your prompt"

# One-off queriesclaude-p"Explain what this project does"# Structured output for scriptsclaude-p"List all API endpoints"--output-formatjson# Streaming for real-time processingclaude-p"Analyze this log file"--output-formatstream-json--verbose

One-off queriesclaude-p"Explain what this project does"# Structured output for scriptsclaude-p"List all API endpoints"--output-formatjson# Streaming for real-time processingclaude-p"Analyze this log file"--output-formatstream-json--verbose

​Run multiple Claude sessions

  • Worktrees: run separate CLI sessions in isolated git checkouts so edits don’t collide
  • Desktop app: manage multiple local sessions visually, each in its own worktree
  • Claude Code on the web: run sessions on Anthropic-managed cloud infrastructure in isolated VMs
  • Agent teams: automated coordination of multiple sessions with shared tasks, messaging, and a team lead

Implement a rate limiter for our API endpoints Review the rate limiter implementation in @src/middleware/rateLimiter.ts. Look for edge cases, race conditions, and consistency with our existing middleware patterns. Here's the review feedback: [Session B output]. Address these issues.

​Fan out across files

claude -p --allowedTools Generate a task list list all 2,000 Python files that need migrating Write a script to loop through the list

forfilein$(catfiles.txt);doclaude-p"Migrate$filefrom React to Vue. Return OK or FAIL."\--allowedTools"Edit,Bash(git commit *)"done

forfilein$(catfiles.txt);doclaude-p"Migrate$filefrom React to Vue. Return OK or FAIL."\--allowedTools"Edit,Bash(git commit *)"done Test on a few files, then run at scale --allowedTools

claude-p"<your prompt>"--output-formatjson|your_command

claude-p"<your prompt>"--output-formatjson|your_command --verbose

​Run autonomously with auto mode

claude--permission-modeauto-p"fix all lint errors"

claude--permission-modeauto-p"fix all lint errors" -p

​Add an adversarial review step

/code-review

Use a subagent to review the rate limiter diff against PLAN.md. Check thatevery requirement is implemented, the listed edge cases have tests, andnothing outside the task's scope changed. Report gaps, not style preferences.

Use a subagent to review the rate limiter diff against PLAN.md. Check thatevery requirement is implemented, the listed edge cases have tests, andnothing outside the task's scope changed. Report gaps, not style preferences.

​Avoid common failure patterns

  • The kitchen sink session.You start with one task, then ask Claude something unrelated, then go back to the first task. Context is full of irrelevant information.Fix:/clearbetween unrelated tasks.

Fix:/clearbetween unrelated tasks.

/clear

  • Correcting over and over.Claude does something wrong, you correct it, it’s still wrong, you correct again. Context is polluted with failed approaches.Fix: After two failed corrections,/clearand write a better initial prompt incorporating what you learned.

Fix: After two failed corrections,/clearand write a better initial prompt incorporating what you learned.

/clear

  • The over-specified CLAUDE.md.If your CLAUDE.md is too long, Claude ignores half of it because important rules get lost in the noise.Fix: Ruthlessly prune. If Claude already does something correctly without the instruction, delete it or convert it to a hook.

Fix: Ruthlessly prune. If Claude already does something correctly without the instruction, delete it or convert it to a hook.

  • The trust-then-verify gap.Claude produces a plausible-looking implementation that doesn’t handle edge cases.Fix: Always provide verification (tests, scripts, screenshots). If you can’t verify it, don’t ship it.

Fix: Always provide verification (tests, scripts, screenshots). If you can’t verify it, don’t ship it.

  • The infinite exploration.You ask Claude to “investigate” something without scoping it. Claude reads hundreds of files, filling the context.Fix: Scope investigations narrowly or use subagents so the exploration doesn’t consume your main context.

Fix: Scope investigations narrowly or use subagents so the exploration doesn’t consume your main context.

​Develop your intuition

​Related resources

  • How Claude Code works: the agentic loop, tools, and context management
  • Extend Claude Code: skills, hooks, MCP, subagents, and plugins
  • Common workflows: step-by-step recipes for debugging, testing, PRs, and more
  • CLAUDE.md: store project conventions and persistent context

Was this page helpful?

本页内容

  • 让 Claude 能够验证自己的工作
  • 先探索,再规划,然后编码
  • 在提示中提供具体上下文提供丰富内容
  • 提供丰富内容
  • 配置你的环境编写有效的 CLAUDE.md配置权限使用 CLI 工具连接 MCP 服务器设置钩子创建技能创建自定义子智能体安装插件
  • 编写有效的 CLAUDE.md
  • 配置权限
  • 使用 CLI 工具
  • 连接 MCP 服务器
  • 设置钩子
  • 创建技能
  • 创建自定义子智能体
  • 安装插件
  • 有效沟通提出代码库相关问题让 Claude 访谈你
  • 提出代码库相关问题
  • 让 Claude 访谈你
  • 管理你的会话尽早并频繁地修正方向积极管理上下文使用子智能体进行调查通过检查点回退恢复会话
  • 尽早并频繁地修正方向
  • 积极管理上下文
  • 使用子智能体进行调查
  • 通过检查点回退
  • 恢复会话
  • 自动化与规模化运行非交互模式运行多个 Claude 会话跨文件扇出执行使用自动模式自主运行添加对抗性审查步骤
  • 运行非交互模式
  • 运行多个 Claude 会话
  • 跨文件扇出执行
  • 使用自动模式自主运行
  • 添加对抗性审查步骤
  • 避免常见的失败模式
  • 培养你的直觉
  • 相关资源

Claude Code 最佳实践

充分利用 Claude Code 的技巧和模式,从配置环境到跨并行会话进行规模化。

让 Claude 能够验证自己的工作

  • 在单个提示中:要求 Claude 在同一条消息中运行检查并进行迭代,如上表所示。
  • 跨会话:将检查设置为 /goal 条件。一个独立的评估器会在每轮对话后重新检查,Claude 会持续工作直到条件满足。
  • 作为确定性门控:一个 Stop 钩子(Hook)将你的检查作为脚本运行,并阻止当前轮次结束直到检查通过。Claude Code 在连续 8 次阻止后会覆盖该钩子并结束轮次。
  • 通过第二意见:一个验证子智能体(Subagent)或动态工作流来检查自身的发现,让一个全新的模型尝试反驳结果,这样执行工作的智能体不是评分的那个。

先探索,再规划,然后编码

探索

read /src/auth and understand how we handle sessions and login.also look at how we manage environment variables for secrets.

规划

I want to add Google OAuth. What files need to change?What's the session flow? Create a plan.

Ctrl+G 实现

implement the OAuth flow from your plan. write tests for thecallback handler, run the test suite and fix any failures.

提交

commit with a descriptive message and open a PR

在提示中提供具体上下文

"你会对这个文件做哪些改进?"

提供丰富内容

  • 使用 @ 引用文件,而不是描述代码的位置。Claude 会在响应之前读取该文件。
  • 直接粘贴图片。将图片复制/粘贴或拖放到提示中。
  • 提供文档和 API 参考的 URL。使用 /permissions 将常用域名加入允许列表。
  • 通过运行 cat error.log | claude 管道传入数据,直接发送文件内容。
  • 让 Claude 获取所需内容。告诉 Claude 使用 Bash 命令、MCP 工具或读取文件来自行拉取上下文。

配置你的环境

编写有效的 CLAUDE.md

/init

# Code style-Use ES modules (import/export) syntax, not CommonJS (require)-Destructure imports when possible (eg. import { foo } from 'bar')# Workflow-Be sure to typecheck when you're done making a series of code changes-Prefer running single tests, and not the whole test suite, for performance

@path/to/import

See @README.md for project overview and @package.json for available npm commands.# Additional Instructions-Git workflow: @docs/git-instructions.md-Personal overrides: @~/.claude/my-project-instructions.md
  • 主文件夹(~/.claude/CLAUDE.md):适用于所有 Claude 会话
  • 项目根目录(./CLAUDE.md):提交到 git 以便与团队共享
  • 项目根目录(./CLAUDE.local.md):个人的项目专用笔记;将此文件添加到 .gitignore 中,避免与团队共享
  • 父目录:适用于 monorepo,root/CLAUDE.md 和 root/foo/CLAUDE.md 都会被自动加载
  • 子目录:当 Claude 读取子目录中的文件时,会按需加载子目录的 CLAUDE.md 文件

配置权限

/permissions

  • 自动模式:一个独立的分类器模型审查命令,仅阻止看起来有风险的操作:权限范围升级、未知基础设施或恶意内容驱动的操作。当你信任任务的大致方向但不想逐步点击确认时,这是最佳选择
  • 权限允许列表:允许你已知安全的特定工具,如 npm run lint 或 git commit
  • 沙箱化:启用操作系统级别的隔离来限制文件系统和网络访问,允许 Claude 在定义的边界内更自由地工作

使用 CLI 工具

gh、aws、gcloud、sentry-cli 等工具都经过了优化。

使用 'foo-cli-tool --help' 来了解 foo 工具,然后用它来解决 A、B、C 问题。

连接 MCP 服务器

使用 claude mcp add 来连接 MCP 服务器。

设置钩子

在 .claude/settings.json 中配置 /hooks。

创建技能

技能(Skill)以 SKILL.md 文件的形式存在于 .claude/skills/ 目录中,可以通过 /skill-name 来调用。

---name:api-conventionsdescription:REST API design conventions for our services---# API Conventions-Use kebab-case for URL paths-Use camelCase for JSON properties-Always include pagination for list endpoints-Version APIs in the URL path (/v1/, /v2/)
---name:fix-issuedescription:Fix a GitHub issuedisable-model-invocation:true---Analyze and fix the GitHub issue: $ARGUMENTS.1.Use`gh issue view`to get the issue details2.Understand the problem described in the issue3.Search the codebase for relevant files4.Implement the necessary changes to fix the issue5.Write and run tests to verify the fix6.Ensure code passes linting and type checking7.Create a descriptive commit message8.Push and create a PR

你可以使用 /fix-issue 1234 来调用此技能。设置 disable-model-invocation: true 可以跳过模型调用,直接执行技能中的步骤。

创建自定义子智能体

子智能体(Subagent)定义在 .claude/agents/ 目录中。

---name:security-reviewerdescription:Reviews code for security vulnerabilitiestools:Read, Grep, Glob, Bashmodel:opus---You are a senior security engineer. Review code for:-Injection vulnerabilities (SQL, XSS, command injection)-Authentication and authorization flaws-Secrets or credentials in code-Insecure data handlingProvide specific line references and suggested fixes.

安装插件

使用 /plugin 来安装和管理插件。

有效沟通

提出代码库相关问题

  • 日志系统是如何工作的?
  • 如何创建一个新的 API 端点?
  • foo.rs 第 134 行的 async move { ... } 是做什么的?
  • CustomerOnboardingFlowImpl 处理了哪些边缘情况?
  • 为什么这段代码在第 333 行调用 foo() 而不是 bar()?

让 Claude 访谈你

使用 AskUserQuestion 工具来让 Claude 对你进行深入访谈。

I want to build [brief description]. Interview me in detail using the AskUserQuestion tool.Ask about technical implementation, UI/UX, edge cases, concerns, and tradeoffs. Don't ask obvious questions, dig into the hard parts I might not have considered.Keep interviewing until we've covered everything, then write a complete spec to SPEC.md.

管理你的会话

尽早并频繁地修正方向

  • Esc:在 Claude 执行过程中按 Esc 键停止。上下文会被保留,因此你可以重新引导方向。
  • Esc + Esc 或 /rewind:按两次 Esc 或运行 /rewind 来打开回退菜单,恢复之前的对话和代码状态,或从选定消息开始总结。
  • "撤销那个操作":让 Claude 回退其更改。
  • /clear:在不相关的任务之间重置上下文。包含不相关上下文的长会话可能会降低性能。

积极管理上下文

  • 在任务之间频繁使用 /clear 来完全重置上下文窗口
  • 当自动压缩触发时,Claude 会总结最重要的内容,包括代码模式、文件状态和关键决策
  • 如需更多控制,运行 /compact <instructions>,例如 /compact Focus on the API changes
  • 要仅压缩对话的一部分,使用 Esc + Esc 或 /rewind,选择一个消息检查点,然后选择"从此处总结"或"总结到此处"。前者压缩从该点之后的消息同时保留早期上下文不变;后者压缩早期消息同时完整保留最近的消息。请参阅"恢复与总结"。
  • 在 CLAUDE.md 中自定义压缩行为,例如添加指令"压缩时,始终保留完整的已修改文件列表和所有测试命令",以确保关键上下文在总结过程中得以保留
  • 对于不需要保留在上下文中的快速问题,使用 /btw。答案会显示在可关闭的叠加层中,并且永远不会进入对话历史,因此你可以在不增加上下文的情况下查看细节。

使用子智能体进行调查

使用子智能体(Subagent)来调查问题。

Use subagents to investigate how our authentication system handles tokenrefresh, and whether we have any existing OAuth utilities I should reuse.
use a subagent to review this code for edge cases

通过检查点回退

按 Escape 键或使用 /rewind 命令来回退到之前的检查点。

恢复会话

使用 /rename 为会话命名,使用 claude --continue 继续最近的会话,使用 claude --resume 恢复特定会话(例如 claude --resume oauth-migration)。

自动化与规模化

运行非交互模式

使用 claude -p "prompt" 来运行非交互模式。可以配合 --output-format stream-json --verbose 使用。

# One-off queriesclaude-p"Explain what this project does"# Structured output for scriptsclaude-p"List all API endpoints"--output-formatjson# Streaming for real-time processingclaude-p"Analyze this log file"--output-formatstream-json--verbose

运行多个 Claude 会话

  • 工作树(Worktree):在隔离的 git 检出中运行独立的 CLI 会话,避免编辑冲突
  • 桌面应用:可视化管理多个本地会话,每个会话在自己的工作树中
  • Web 版 Claude Code:在 Anthropic 管理的云基础设施上的隔离虚拟机中运行会话
  • 智能体团队(Agent Team):多个会话的自动化协调,具有共享任务、消息传递和团队负责人

实现流程示例:

  1. 智能体 A:为我们的 API 端点实现一个速率限制器
  2. 智能体 B:审查 @src/middleware/rateLimiter.ts 中的速率限制器实现。查找边缘情况、竞态条件以及与现有中间件模式的一致性
  3. 智能体 A:以下是审查反馈:[会话 B 输出]。解决这些问题

跨文件扇出执行

使用 claude -p 配合 --allowedTools 来跨多个文件扇出执行任务。

步骤:

  1. 生成任务列表:列出所有需要迁移的 2,000 个 Python 文件
  2. 编写脚本来循环处理列表
forfilein$(catfiles.txt);doclaude-p"Migrate$filefrom React to Vue. Return OK or FAIL."\--allowedTools"Edit,Bash(git commit *)"done

先在几个文件上测试,然后大规模运行。使用 --allowedTools 来限制可用工具。

claude-p"<your prompt>"--output-formatjson|your_command

使用 --verbose 获取详细输出。

使用自动模式自主运行

claude--permission-modeauto-p"fix all lint errors"

使用 -p 标志来指定提示。

添加对抗性审查步骤

使用 /code-review 或子智能体来添加对抗性审查。

Use a subagent to review the rate limiter diff against PLAN.md. Check thatevery requirement is implemented, the listed edge cases have tests, andnothing outside the task's scope changed. Report gaps, not style preferences.

避免常见的失败模式

  • 大杂烩会话。你从一个任务开始,然后问 Claude 一些不相关的事情,再回到第一个任务。上下文中充满了不相关的信息。

修复方法:在不相关的任务之间使用 /clear。

  • 反复纠正。Claude 做错了某事,你纠正它,它仍然做错,你再次纠正。上下文被失败的方法污染了。

修复方法:在两次失败的纠正后,使用 /clear 并根据你学到的经验编写一个更好的初始提示。

  • 过度指定的 CLAUDE.md。如果你的 CLAUDE.md 太长,Claude 会忽略其中一半,因为重要的规则在噪音中丢失了。

修复方法:果断精简。如果 Claude 在没有指令的情况下已经正确执行了某项操作,就删除它或将其转换为钩子。

  • 信任后验证的差距。Claude 生成了一个看似合理的实现,但没有处理边缘情况。

修复方法:始终提供验证(测试、脚本、截图)。如果你无法验证它,就不要发布它。

  • 无限探索。你让 Claude "调查"某事但没有限定范围。Claude 读取了数百个文件,填满了上下文。

修复方法:将调查范围缩小或使用子智能体,这样探索不会消耗你的主上下文。

培养你的直觉

相关资源

  • Claude Code 的工作原理:智能体循环、工具和上下文管理
  • 扩展 Claude Code:技能、钩子、MCP、子智能体和插件
  • 常见工作流:调试、测试、PR 等的分步指南
  • CLAUDE.md:存储项目约定和持久化上下文

本页是否对你有帮助?

On this page

  • Give Claude a way to verify its work
  • Explore first, then plan, then code
  • Provide specific context in your promptsProvide rich content
  • Provide rich content
  • Configure your environmentWrite an effective CLAUDE.mdConfigure permissionsUse CLI toolsConnect MCP serversSet up hooksCreate skillsCreate custom subagentsInstall plugins
  • Write an effective CLAUDE.md
  • Configure permissions
  • Use CLI tools
  • Connect MCP servers
  • Set up hooks
  • Create skills
  • Create custom subagents
  • Install plugins
  • Communicate effectivelyAsk codebase questionsLet Claude interview you
  • Ask codebase questions
  • Let Claude interview you
  • Manage your sessionCourse-correct early and oftenManage context aggressivelyUse subagents for investigationRewind with checkpointsResume conversations
  • Course-correct early and often
  • Manage context aggressively
  • Use subagents for investigation
  • Rewind with checkpoints
  • Resume conversations
  • Automate and scaleRun non-interactive modeRun multiple Claude sessionsFan out across filesRun autonomously with auto modeAdd an adversarial review step
  • Run non-interactive mode
  • Run multiple Claude sessions
  • Fan out across files
  • Run autonomously with auto mode
  • Add an adversarial review step
  • Avoid common failure patterns
  • Develop your intuition
  • Related resources

Best practices for Claude Code

Tips and patterns for getting the most out of Claude Code, from configuring your environment to scaling across parallel sessions.

​Give Claude a way to verify its work

  • In one prompt: ask Claude to run the check and iterate in the same message, as in the table above.
  • Across a session: set the check as a/goalcondition. A separate evaluator re-checks it after every turn and Claude keeps working until it holds.

/goal

  • As a deterministic gate: aStop hookruns your check as a script and blocks the turn from ending until it passes. Claude Code overrides the hook and ends the turn after 8 consecutive blocks.
  • By a second opinion: averification subagentor adynamic workflowthat checks its own findings has a fresh model try to refute the result, so the agent doing the work isn’t the one grading it.

/goal

​Explore first, then plan, then code

Explore

read /src/auth and understand how we handle sessions and login.also look at how we manage environment variables for secrets.

read /src/auth and understand how we handle sessions and login.also look at how we manage environment variables for secrets. Plan

I want to add Google OAuth. What files need to change?What's the session flow? Create a plan.

I want to add Google OAuth. What files need to change?What's the session flow? Create a plan. Ctrl+G Implement

implement the OAuth flow from your plan. write tests for thecallback handler, run the test suite and fix any failures.

implement the OAuth flow from your plan. write tests for thecallback handler, run the test suite and fix any failures. Commit

commit with a descriptive message and open a PR

commit with a descriptive message and open a PR

​Provide specific context in your prompts

"what would you improve in this file?"

​Provide rich content

@

  • Reference files with@instead of describing where code lives. Claude reads the file before responding.

@

  • Paste images directly. Copy/paste or drag and drop images into the prompt.
  • Give URLsfor documentation and API references. Use/permissionsto allowlist frequently-used domains.

/permissions

  • Pipe in databy runningcat error.log | claudeto send file contents directly.

cat error.log | claude

  • Let Claude fetch what it needs. Tell Claude to pull context itself using Bash commands, MCP tools, or by reading files.

​Configure your environment

​Write an effective CLAUDE.md

/init /init

# Code style-Use ES modules (import/export) syntax, not CommonJS (require)-Destructure imports when possible (eg. import { foo } from 'bar')# Workflow-Be sure to typecheck when you're done making a series of code changes-Prefer running single tests, and not the whole test suite, for performance

Code style-Use ES modules (import/export) syntax, not CommonJS (require)-Destructure imports when possible (eg. import { foo } from 'bar')# Workflow-Be sure to typecheck when you're done making a series of code changes-Prefer running single tests, and not the whole test suite, for performance

@path/to/import

See @README.md for project overview and @package.json for available npm commands.# Additional Instructions-Git workflow: @docs/git-instructions.md-Personal overrides: @~/.claude/my-project-instructions.md

See @README.md for project overview and @package.json for available npm commands.# Additional Instructions-Git workflow: @docs/git-instructions.md-Personal overrides: @~/.claude/my-project-instructions.md

  • Home folder (~/.claude/CLAUDE.md): applies to all Claude sessions

~/.claude/CLAUDE.md

  • Project root (./CLAUDE.md): check into git to share with your team

./CLAUDE.md

  • Project root (./CLAUDE.local.md): personal project-specific notes; add this file to your.gitignoreso it isn’t shared with your team

./CLAUDE.local.md .gitignore

  • Parent directories: useful for monorepos where bothroot/CLAUDE.mdandroot/foo/CLAUDE.mdare pulled in automatically

root/CLAUDE.md root/foo/CLAUDE.md

  • Child directories: Claude pulls in child CLAUDE.md files on demand when it reads a file in those directories

​Configure permissions

/permissions /sandbox

  • Auto mode: a separate classifier model reviews commands and blocks only what looks risky: scope escalation, unknown infrastructure, or hostile-content-driven actions. Best when you trust the general direction of a task but don’t want to click through every step
  • Permission allowlists: permit specific tools you know are safe, likenpm run lintorgit commit

npm run lint git commit

  • Sandboxing: enable OS-level isolation that restricts filesystem and network access, allowing Claude to work more freely within defined boundaries

​Use CLI tools

gh aws gcloud sentry-cli gh gh Use 'foo-cli-tool --help' to learn about foo tool, then use it to solve A, B, C.

​Connect MCP servers

claude mcp add

​Set up hooks

.claude/settings.json /hooks

​Create skills

SKILL.md .claude/skills/ /skill-name SKILL.md .claude/skills/

---name:api-conventionsdescription:REST API design conventions for our services---# API Conventions-Use kebab-case for URL paths-Use camelCase for JSON properties-Always include pagination for list endpoints-Version APIs in the URL path (/v1/, /v2/)

---name:api-conventionsdescription:REST API design conventions for our services---# API Conventions-Use kebab-case for URL paths-Use camelCase for JSON properties-Always include pagination for list endpoints-Version APIs in the URL path (/v1/, /v2/)

---name:fix-issuedescription:Fix a GitHub issuedisable-model-invocation:true---Analyze and fix the GitHub issue: $ARGUMENTS.1.Use`gh issue view`to get the issue details2.Understand the problem described in the issue3.Search the codebase for relevant files4.Implement the necessary changes to fix the issue5.Write and run tests to verify the fix6.Ensure code passes linting and type checking7.Create a descriptive commit message8.Push and create a PR

---name:fix-issuedescription:Fix a GitHub issuedisable-model-invocation:true---Analyze and fix the GitHub issue: $ARGUMENTS.1.Usegh issue viewto get the issue details2.Understand the problem described in the issue3.Search the codebase for relevant files4.Implement the necessary changes to fix the issue5.Write and run tests to verify the fix6.Ensure code passes linting and type checking7.Create a descriptive commit message8.Push and create a PR /fix-issue 1234 disable-model-invocation: true

​Create custom subagents

.claude/agents/

---name:security-reviewerdescription:Reviews code for security vulnerabilitiestools:Read, Grep, Glob, Bashmodel:opus---You are a senior security engineer. Review code for:-Injection vulnerabilities (SQL, XSS, command injection)-Authentication and authorization flaws-Secrets or credentials in code-Insecure data handlingProvide specific line references and suggested fixes.

---name:security-reviewerdescription:Reviews code for security vulnerabilitiestools:Read, Grep, Glob, Bashmodel:opus---You are a senior security engineer. Review code for:-Injection vulnerabilities (SQL, XSS, command injection)-Authentication and authorization flaws-Secrets or credentials in code-Insecure data handlingProvide specific line references and suggested fixes.

​Install plugins

/plugin

​Communicate effectively

​Ask codebase questions

  • How does logging work?
  • How do I make a new API endpoint?
  • What doesasync move { ... }do on line 134 offoo.rs?

async move { ... } foo.rs

  • What edge cases doesCustomerOnboardingFlowImplhandle?

CustomerOnboardingFlowImpl

  • Why does this code callfoo()instead ofbar()on line 333?

foo() bar()

​Let Claude interview you

AskUserQuestion

I want to build [brief description]. Interview me in detail using the AskUserQuestion tool.Ask about technical implementation, UI/UX, edge cases, concerns, and tradeoffs. Don't ask obvious questions, dig into the hard parts I might not have considered.Keep interviewing until we've covered everything, then write a complete spec to SPEC.md.

I want to build [brief description]. Interview me in detail using the AskUserQuestion tool.Ask about technical implementation, UI/UX, edge cases, concerns, and tradeoffs. Don't ask obvious questions, dig into the hard parts I might not have considered.Keep interviewing until we've covered everything, then write a complete spec to SPEC.md.

​Manage your session

​Course-correct early and often

  • Esc: stop Claude mid-action with theEsckey. Context is preserved, so you can redirect.

Esc Esc

  • Esc + Escor/rewind: pressEsctwice or run/rewindto open the rewind menu and restore previous conversation and code state, or summarize from a selected message.

Esc + Esc /rewind Esc /rewind

  • "Undo that": have Claude revert its changes.

"Undo that"

  • /clear: reset context between unrelated tasks. Long sessions with irrelevant context can reduce performance.

/clear /clear

​Manage context aggressively

/clear

  • Use/clearfrequently between tasks to reset the context window entirely

/clear

  • When auto compaction triggers, Claude summarizes what matters most, including code patterns, file states, and key decisions
  • For more control, run/compact <instructions>, like/compact Focus on the API changes

/compact <instructions> /compact Focus on the API changes

  • To compact only part of the conversation, useEsc + Escor/rewind, select a message checkpoint, and chooseSummarize from hereorSummarize up to here. The first condenses messages from that point forward while keeping earlier context intact; the second condenses earlier messages while keeping recent ones in full. SeeRestore vs. summarize.

Esc + Esc /rewind

  • Customize compaction behavior in CLAUDE.md with instructions like"When compacting, always preserve the full list of modified files and any test commands"to ensure critical context survives summarization

"When compacting, always preserve the full list of modified files and any test commands"

  • For quick questions that don’t need to stay in context, use/btw. The answer appears in a dismissible overlay and never enters conversation history, so you can check a detail without growing context.

/btw

​Use subagents for investigation

"use subagents to investigate X"

Use subagents to investigate how our authentication system handles tokenrefresh, and whether we have any existing OAuth utilities I should reuse.

Use subagents to investigate how our authentication system handles tokenrefresh, and whether we have any existing OAuth utilities I should reuse.

use a subagent to review this code for edge cases

use a subagent to review this code for edge cases

​Rewind with checkpoints

Escape /rewind

​Resume conversations

/rename claude --continue claude --resume oauth-migration

​Automate and scale

​Run non-interactive mode

claude -p "prompt" --output-format stream-json --verbose claude -p "your prompt"

# One-off queriesclaude-p"Explain what this project does"# Structured output for scriptsclaude-p"List all API endpoints"--output-formatjson# Streaming for real-time processingclaude-p"Analyze this log file"--output-formatstream-json--verbose

One-off queriesclaude-p"Explain what this project does"# Structured output for scriptsclaude-p"List all API endpoints"--output-formatjson# Streaming for real-time processingclaude-p"Analyze this log file"--output-formatstream-json--verbose

​Run multiple Claude sessions

  • Worktrees: run separate CLI sessions in isolated git checkouts so edits don’t collide
  • Desktop app: manage multiple local sessions visually, each in its own worktree
  • Claude Code on the web: run sessions on Anthropic-managed cloud infrastructure in isolated VMs
  • Agent teams: automated coordination of multiple sessions with shared tasks, messaging, and a team lead

Implement a rate limiter for our API endpoints Review the rate limiter implementation in @src/middleware/rateLimiter.ts. Look for edge cases, race conditions, and consistency with our existing middleware patterns. Here's the review feedback: [Session B output]. Address these issues.

​Fan out across files

claude -p --allowedTools Generate a task list list all 2,000 Python files that need migrating Write a script to loop through the list

forfilein$(catfiles.txt);doclaude-p"Migrate$filefrom React to Vue. Return OK or FAIL."\--allowedTools"Edit,Bash(git commit *)"done

forfilein$(catfiles.txt);doclaude-p"Migrate$filefrom React to Vue. Return OK or FAIL."\--allowedTools"Edit,Bash(git commit *)"done Test on a few files, then run at scale --allowedTools

claude-p"<your prompt>"--output-formatjson|your_command

claude-p"<your prompt>"--output-formatjson|your_command --verbose

​Run autonomously with auto mode

claude--permission-modeauto-p"fix all lint errors"

claude--permission-modeauto-p"fix all lint errors" -p

​Add an adversarial review step

/code-review

Use a subagent to review the rate limiter diff against PLAN.md. Check thatevery requirement is implemented, the listed edge cases have tests, andnothing outside the task's scope changed. Report gaps, not style preferences.

Use a subagent to review the rate limiter diff against PLAN.md. Check thatevery requirement is implemented, the listed edge cases have tests, andnothing outside the task's scope changed. Report gaps, not style preferences.

​Avoid common failure patterns

  • The kitchen sink session.You start with one task, then ask Claude something unrelated, then go back to the first task. Context is full of irrelevant information.Fix:/clearbetween unrelated tasks.

Fix:/clearbetween unrelated tasks.

/clear

  • Correcting over and over.Claude does something wrong, you correct it, it’s still wrong, you correct again. Context is polluted with failed approaches.Fix: After two failed corrections,/clearand write a better initial prompt incorporating what you learned.

Fix: After two failed corrections,/clearand write a better initial prompt incorporating what you learned.

/clear

  • The over-specified CLAUDE.md.If your CLAUDE.md is too long, Claude ignores half of it because important rules get lost in the noise.Fix: Ruthlessly prune. If Claude already does something correctly without the instruction, delete it or convert it to a hook.

Fix: Ruthlessly prune. If Claude already does something correctly without the instruction, delete it or convert it to a hook.

  • The trust-then-verify gap.Claude produces a plausible-looking implementation that doesn’t handle edge cases.Fix: Always provide verification (tests, scripts, screenshots). If you can’t verify it, don’t ship it.

Fix: Always provide verification (tests, scripts, screenshots). If you can’t verify it, don’t ship it.

  • The infinite exploration.You ask Claude to “investigate” something without scoping it. Claude reads hundreds of files, filling the context.Fix: Scope investigations narrowly or use subagents so the exploration doesn’t consume your main context.

Fix: Scope investigations narrowly or use subagents so the exploration doesn’t consume your main context.

​Develop your intuition

​Related resources

  • How Claude Code works: the agentic loop, tools, and context management
  • Extend Claude Code: skills, hooks, MCP, subagents, and plugins
  • Common workflows: step-by-step recipes for debugging, testing, PRs, and more
  • CLAUDE.md: store project conventions and persistent context

Was this page helpful?