claude-md-authoring

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

CLAUDE.md Authoring

CLAUDE.md 编写指南

Quickstart

快速入门

  1. Determine scope: User-level (
    ~/.claude/
    ) or project-level (
    .claude/
    )?
  2. Apply WHAT/WHY/HOW framework (project-level only)
  3. Keep instructions under budget: ~50 for user-level, ~100 for project-level
  4. Never use Claude as a linter: Style rules belong in tooling, not CLAUDE.md
  5. Validate with quality checklist before finalizing
  1. 确定范围:用户级(
    ~/.claude/
    )还是项目级(
    .claude/
    )?
  2. 应用WHAT/WHY/HOW框架(仅项目级适用)
  3. 控制指令数量在预算内:用户级约50条,项目级约100条
  4. 切勿将Claude用作检查器:风格规则应交给专门工具处理,而非CLAUDE.md
  5. 最终确定前使用质量清单验证

Core Principle

核心原则

"The only thing that the model knows about your codebase is the tokens you put into it."
CLAUDE.md is the highest-leverage point for context transfer. Every instruction competes for limited attention. Include only what Claude cannot infer and must apply universally.
"模型对您代码库的了解仅限于您输入的Token内容。"
CLAUDE.md是传递上下文的最高效切入点。每条指令都会争夺有限的注意力资源。仅需添加Claude无法自行推断且必须全局应用的内容。

Scope Decision

范围决策

User-Level (
~/.claude/CLAUDE.md
)

用户级(
~/.claude/CLAUDE.md

Applies to ALL projects. Include only:
  • Personal working style preferences
  • Relationship dynamics (pushback, honesty, communication)
  • Universal philosophy (YAGNI, root-cause debugging)
  • Proactiveness boundaries
Never include:
  • Project-specific commands (build, test, lint)
  • Tech stack details
  • Style rules that vary by project
  • Tool/framework references that don't exist everywhere
适用于所有项目。仅需包含:
  • 个人工作风格偏好
  • 协作互动规则(如异议反馈、诚实沟通方式)
  • 通用开发理念(如YAGNI、根因调试法)
  • 主动操作的边界限制
切勿包含:
  • 项目特定命令(构建、测试、检查)
  • 技术栈细节
  • 因项目而异的风格规则
  • 并非所有环境都存在的工具/框架引用

Project-Level (
.claude/CLAUDE.md
)

项目级(
.claude/CLAUDE.md

Applies to ONE project. Use the WHAT/WHY/HOW framework:
WHAT (2-5 lines)
  • Project purpose in one sentence
  • Primary language/framework
  • Key directories and their purposes
WHY (optional, 2-3 lines)
  • Non-obvious architectural decisions
  • Constraints that affect implementation choices
HOW (3-7 lines)
  • Essential commands: build, test, lint, run
  • Any project-specific workflows
  • Where to find more documentation
仅适用于单个项目。使用WHAT/WHY/HOW框架:
WHAT(2-5行)
  • 一句话说明项目用途
  • 主要语言/框架
  • 关键目录及其用途
WHY(可选,2-3行)
  • 非显而易见的架构决策
  • 影响实现选择的约束条件
HOW(3-7行)
  • 核心命令:构建、测试、检查、运行
  • 任何项目特定工作流
  • 更多文档的查找位置

Instruction Budget

指令预算

LLMs reliably follow ~150-200 instructions. Claude Code's system prompt uses ~50, leaving limited budget.
ScopeTargetMaximum
User-level~30-4060
Project-level~50-80120
Combined~80-120150
Counting instructions: Each actionable directive counts as one instruction.
  • "NEVER skip tests" = 1 instruction
  • "Use TypeScript for new files" = 1 instruction
  • A 5-item list = 5 instructions
大语言模型通常能可靠遵循约150-200条指令。Claude Code的系统提示已占用约50条,剩余预算有限。
范围目标值最大值
用户级~30-4060
项目级~50-80120
组合级~80-120150
指令计数规则:每个可执行指令计为1条。
  • "绝不跳过测试" = 1条指令
  • "新文件使用TypeScript" = 1条指令
  • 一个5项列表 = 5条指令

Anti-Pattern: Claude as Linter

反模式:将Claude用作检查器

Never rely on Claude for style enforcement:
  • Naming conventions with specific examples
  • Comment formatting rules
  • Whitespace/indentation requirements
  • File header requirements (ABOUTME, copyright)
Use deterministic tools instead:
  • ESLint/Biome for JavaScript/TypeScript style
  • Black/Ruff for Python formatting
  • Pre-commit hooks for file headers
  • Claude Code Hooks to run formatters
Why this matters: LLMs follow style rules inconsistently. A linter fails deterministically; Claude fails silently and unpredictably.
Exception: High-level philosophy is acceptable:
  • ✓ "Names should describe purpose, not implementation"
  • ❌ "Never use names like ZodValidator or MCPWrapper"
绝不要依赖Claude执行风格检查
  • 带具体示例的命名规范
  • 注释格式规则
  • 空白/缩进要求
  • 文件头要求(如ABOUTME、版权信息)
改用确定性工具
  • 针对JavaScript/TypeScript风格的ESLint/Biome
  • 针对Python格式化的Black/Ruff
  • 针对文件头的Pre-commit钩子
  • 用于运行格式化工具的Claude Code Hooks
重要原因:大语言模型遵循风格规则的一致性较差。检查器会确定性地报错;而Claude会静默且不可预测地失效。
例外情况:可接受高层级理念指导:
  • ✓ "命名应描述用途,而非实现细节"
  • ❌ "绝不要使用ZodValidator或MCPWrapper这类命名"

Progressive Disclosure

渐进式披露

For complex guidance, don't embed everything in CLAUDE.md.
Pattern: Reference external docs, let Claude decide relevance.
markdown
undefined
对于复杂指导内容,无需全部嵌入CLAUDE.md。
模式:引用外部文档,让Claude自行判断相关性。
markdown
undefined

Reference Documentation

参考文档

  • Code conventions: See
    docs/conventions.md
  • API patterns: See
    docs/api-design.md
  • Testing philosophy: See
    docs/testing.md
Read these when the task involves the relevant area.

**Benefits**:
- Keeps CLAUDE.md focused
- Claude reads docs only when relevant
- Easier to maintain separate concerns
  • 代码规范:详见
    docs/conventions.md
  • API模式:详见
    docs/api-design.md
  • 测试理念:详见
    docs/testing.md
当任务涉及相关领域时再阅读这些文档。

**优势**:
- 保持CLAUDE.md的聚焦性
- Claude仅在相关时才会读取文档
- 便于单独维护不同关注点

Structure Template

结构模板

User-Level

用户级

markdown
[Opening persona/philosophy - 1-2 lines]
markdown
[开篇人设/理念 - 1-2行]

Working Together

协作方式

[Relationship dynamics, communication preferences]
[互动规则、沟通偏好]

Proactiveness

主动操作边界

[When to proceed vs. pause for confirmation]
[何时自行推进 vs. 暂停确认]

Development Philosophy

开发理念

[Universal principles: YAGNI, debugging approach, etc.]
[通用原则:YAGNI、调试方法等]

Guardrails

防护规则

[Version control, testing principles - kept brief]
undefined
[版本控制、测试原则 - 保持简洁]
undefined

Project-Level

项目级

markdown
undefined
markdown
undefined

Project Overview

项目概述

[WHAT: Purpose, stack, structure]
[WHAT:用途、技术栈、结构]

Development

开发流程

[HOW: Build, test, lint commands]
[HOW:构建、测试、检查命令]

Architecture

架构说明

[WHY: Key decisions, constraints - if non-obvious]
[WHY:关键决策、约束条件 - 若非显而易见则添加]

Conventions

规范指引

[Brief pointers to detailed docs if needed]
undefined
[如需详细文档可添加简短指向]
undefined

Quality Checklist

质量清单

Before finalizing, verify:
最终确定前,请验证以下内容:

Universal Applicability

通用性

  • Every instruction applies to every session (user-level) or every task in project (project-level)
  • No project-specific commands in user-level config
  • No tool references that may not exist
  • 每条指令都适用于所有会话(用户级)或项目中的所有任务(项目级)
  • 用户级配置中无项目特定命令
  • 无可能不存在的工具引用

Instruction Economy

指令经济性

  • Under instruction budget (count them)
  • No redundancy with Claude Code's built-in behaviors
  • Examples trimmed or moved to reference docs
  • 指令数量在预算内(请计数)
  • 与Claude Code内置行为无冗余
  • 示例已精简或移至参考文档

No LLM-as-Linter

未将LLM用作检查器

  • No specific naming convention examples
  • No comment formatting rules
  • No file header requirements
  • Style guidance limited to philosophy, not specifics
  • 无具体命名规范示例
  • 无注释格式规则
  • 无文件头要求
  • 风格指导仅限于理念层面,而非具体规则

Progressive Disclosure

渐进式披露

  • Complex topics reference external docs
  • Main file under 100 lines (user) or 150 lines (project)
  • Examples in reference files, not inline
  • 复杂主题已引用外部文档
  • 主文件行数不超过100行(用户级)或150行(项目级)
  • 示例放在参考文件中,而非内联

Clarity

清晰度

  • Consistent heading hierarchy
  • No conflicting instructions
  • Actionable directives, not vague guidance
  • 标题层级一致
  • 无冲突指令
  • 指令可执行,而非模糊指导

Common Pitfalls

常见陷阱

  1. Over-specification: Including every preference instead of universally critical ones
  2. Style enforcement: Detailed naming/comment rules that Claude follows inconsistently
  3. Stale commands: Build/test commands that drift from actual tooling
  4. Auto-generation: Using
    /init
    instead of hand-crafting
  5. Tool assumptions: Referencing MCP servers or tools that aren't always available
  6. Conflicting rules: "Match surrounding style" + "Never use X pattern"
  7. Instruction inflation: Adding rules after each frustration instead of fixing root cause
  1. 过度指定:包含所有偏好而非仅全局关键项
  2. 风格强制:Claude难以一致遵循的详细命名/注释规则
  3. 过时命令:与实际工具不符的构建/测试命令
  4. 自动生成:使用
    /init
    而非手动编写
  5. 工具假设:引用并非始终可用的MCP服务器或工具
  6. 规则冲突:如“匹配周边风格” + “绝不要使用X模式”
  7. 指令膨胀:每次遇到问题就添加规则,而非修复根本原因

When to Skip This Skill

何时无需使用本技能

  • Quick one-off projects (just use defaults)
  • Projects with existing, well-maintained CLAUDE.md
  • When the team already has established patterns
  • 快速一次性项目(直接使用默认配置即可)
  • 已有维护良好的CLAUDE.md的项目
  • 团队已建立成熟模式的项目

Example: Before/After

示例:优化前后对比

Before (problematic user-level):
markdown
undefined
优化前(存在问题的用户级配置):
markdown
undefined

Naming

命名规则

NEVER use names like ZodValidator, MCPWrapper, JSONParser... [20 more lines of specific examples]
绝不要使用ZodValidator、MCPWrapper、JSONParser这类命名... [20多行具体示例]

Comments

注释要求

All files MUST start with ABOUTME: header...

**After** (improved user-level):
```markdown
所有文件必须以ABOUTME: 开头...

**优化后**(改进后的用户级配置):
```markdown

Code Style

代码风格

Names should describe purpose, not implementation details. Use project linters for formatting; don't rely on manual enforcement.

*Why better*: Philosophy over specifics. Linter handles enforcement deterministically.
命名应描述用途,而非实现细节。 使用项目检查器进行格式化;不要依赖手动强制。

*优化原因*:用理念替代具体规则。检查器可确定性地执行强制要求。

Integration

集成方式

After authoring CLAUDE.md:
  • User-level: Symlink to
    ~/.claude/CLAUDE.md
    or place directly
  • Project-level: Commit to
    .claude/CLAUDE.md
    in repository root
  • Validation: Start a new Claude Code session and observe if instructions are followed
编写完CLAUDE.md后:
  • 用户级:创建符号链接至
    ~/.claude/CLAUDE.md
    或直接放置
  • 项目级:提交至仓库根目录的
    .claude/CLAUDE.md
  • 验证:启动新的Claude Code会话,观察指令是否被遵循