Fetch the complete documentation index at: https://code.claude.com/docs/llms.txt Use this file to discover all available pages before exploring further.
Claude Code 最佳实践
从配置环境到跨并行会话扩展,充分利用 Claude Code 的提示和模式。
Claude Code 是一个代理式编码环境。与等待回答问题的聊天机器人不同,Claude Code 可以读取你的文件、运行命令、进行更改,并在你观看、重定向或完全离开的情况下自主解决问题。
这改变了你的工作方式。与其自己编写代码并要求 Claude 审查,不如描述你想要什么,让 Claude 弄清楚如何构建它。Claude 会探索、规划和实现。
但这种自主性仍然伴随着学习曲线。Claude 在某些约束条件下工作,你需要理解这些约束。
本指南涵盖了在 Anthropic 内部团队和在各种代码库、语言和环境中使用 Claude Code 的工程师中已被证明有效的模式。有关代理循环如何在幕后工作的信息,请参阅 Claude Code 如何工作。
让 Claude 获取它需要的东西。告诉 Claude 使用 Bash 命令、MCP 工具或通过读取文件来自己拉取上下文。
配置你的环境
一些设置步骤使 Claude Code 在所有会话中显著更有效。有关扩展功能的完整概述和何时使用每个功能,请参阅 扩展 Claude Code。
编写有效的 CLAUDE.md
运行 `/init` 根据你的当前项目结构生成启动 CLAUDE.md 文件,然后随时间精化。
CLAUDE.md 是一个特殊文件,Claude 在每次对话开始时读取。包括 Bash 命令、代码风格和工作流规则。这给 Claude 提供了它无法从代码中推断的持久上下文。
/init 命令分析你的代码库以检测构建系统、测试框架和代码模式,为你提供坚实的基础来精化。
CLAUDE.md 文件没有必需的格式,但保持简短和易读。例如:
CLAUDE.md theme
1 2 3 4 5 6 7
# 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
在 `.claude/skills/` 中创建 `SKILL.md` 文件,为 Claude 提供域知识和可重用工作流。
Skills 使用特定于你的项目、团队或域的信息扩展 Claude 的知识。Claude 在相关时自动应用它们,或者你可以使用 /skill-name 直接调用它们。
通过向 .claude/skills/ 添加带有 SKILL.md 的目录来创建 skill:
.claude/skills/api-conventions/SKILL.md theme
1 2 3 4 5 6 7 8 9
--- name: api-conventions description: 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/)
Skills 也可以定义你直接调用的可重复工作流:
.claude/skills/fix-issue/SKILL.md theme
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
--- name: fix-issue description: Fix a GitHub issue disable-model-invocation: true --- Analyze and fix the GitHub issue: $ARGUMENTS.
1. Use `gh issue view` to get the issue details 2. Understand the problem described in the issue 3. Search the codebase for relevant files 4. Implement the necessary changes to fix the issue 5. Write and run tests to verify the fix 6. Ensure code passes linting and type checking 7. Create a descriptive commit message 8. Push and create a PR
有关在 skills、subagents、hooks 和 MCP 之间选择的指导,请参阅 扩展 Claude Code。
有效沟通
你与 Claude Code 沟通的方式显著影响结果的质量。
提出代码库问题
问 Claude 你会问资深工程师的问题。
当加入新代码库时,使用 Claude Code 进行学习和探索。你可以问 Claude 你会问另一个工程师的相同类型的问题:
日志如何工作?
我如何创建新的 API 端点?
foo.rs 第 134 行的 async move { ... } 做什么?
CustomerOnboardingFlowImpl 处理哪些边界情况?
为什么这段代码在第 333 行调用 foo() 而不是 bar()?
以这种方式使用 Claude Code 是一个有效的入职工作流,改进了加入时间并减少了对其他工程师的负担。无需特殊提示:直接提问。
让 Claude 采访你
对于更大的功能,让 Claude 先采访你。从最小的提示开始,要求 Claude 使用 `AskUserQuestion` 工具采访你。
Claude 会问你可能还没有考虑过的东西,包括技术实现、UI/UX、边界情况和权衡。
theme
1 2 3 4 5
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.
Fetch the complete documentation index at: https://code.claude.com/docs/llms.txt Use this file to discover all available pages before exploring further.
Claude 如何记住您的项目
使用 CLAUDE.md 文件为 Claude 提供持久指令,并让 Claude 通过自动记忆功能自动积累学习内容。
每个 Claude Code 会话都从一个全新的 context window 开始。两种机制可以跨会话传递知识: