claude-memory
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese<core_principle>
Memory files consume tokens from the context window. ~100-150 instruction slots available for your customizations. Keep files minimal — only include what the agent cannot discover on its own.
Two approaches:
- CLAUDE.md — Single file, best for small projects (< 100 lines)
- .claude/rules/ — Modular files with optional path-scoping, best for large projects </core_principle>
<quick_start>
Run to auto-generate a CLAUDE.md. Or create manually:
/initmarkdown
undefined<core_principle>
内存文件会占用上下文窗口的token。你有大约100-150个指令槽位可用于自定义配置。请保持文件极简——仅包含Agent无法自行发现的内容。
两种实现方案:
- CLAUDE.md — 单文件,最适合小型项目(< 100行)
- .claude/rules/ — 支持可选路径范围配置的模块化文件,最适合大型项目 </core_principle>
<quick_start>
运行自动生成CLAUDE.md,也可以手动创建:
/initmarkdown
undefinedProject Name
Project Name
Tech Stack
Tech Stack
- [Primary framework]
- [Key non-obvious libraries]
- [Primary framework]
- [Key non-obvious libraries]
Commands
Commands
- - Dev server
npm run dev - - Run tests
npm test - - Build
npm run build
- - Dev server
npm run dev - - Run tests
npm test - - Build
npm run build
Rules
Rules
- [2-3 critical project-specific rules]
- Press `#` during a session to add memory items quickly
- Use `/memory` to open CLAUDE.md in your editor
</quick_start>
<file_hierarchy>
| Priority | Location | Scope |
|----------|----------|-------|
| 1 (Highest) | Enterprise policy (managed by IT) | All org users |
| 2 | `./CLAUDE.md` or `./.claude/CLAUDE.md` | Team via git |
| 2 | `./.claude/rules/*.md` | Team via git |
| 3 | `~/.claude/CLAUDE.md` | All your projects |
| 3 | `~/.claude/rules/*.md` | All your projects |
| 4 (Lowest) | `./CLAUDE.local.md` (auto-gitignored) | Just you |
Claude recurses UP from current directory, loading all CLAUDE.md files found. Also discovers CLAUDE.md in subtrees when reading files in those directories.
**Monorepo strategy:** Root file defines WHEN; subtree files define HOW.root/CLAUDE.md # Universal: tech stack, git workflow
apps/web/CLAUDE.md # Frontend-specific
apps/api/CLAUDE.md # Backend-specific
</file_hierarchy>
<rules_directory>
The `.claude/rules/` directory splits instructions into focused markdown files.
**Use `.claude/rules/` when:** many concerns, different rules for different file types, team maintains different areas.
**Use CLAUDE.md when:** small project, universal rules, single source of truth.
Path-scoped rules use YAML frontmatter:
```yaml
---
paths:
- "src/api/**/*.ts"
---- [2-3 critical project-specific rules]
- 会话过程中按下`#`可快速添加记忆项
- 使用`/memory`可在编辑器中打开CLAUDE.md
</quick_start>
<file_hierarchy>
| 优先级 | 位置 | 作用范围 |
|----------|----------|-------|
| 1(最高) | 企业策略(由IT管理) | 组织内所有用户 |
| 2 | `./CLAUDE.md` 或 `./.claude/CLAUDE.md` | 团队共享(通过git同步) |
| 2 | `./.claude/rules/*.md` | 团队共享(通过git同步) |
| 3 | `~/.claude/CLAUDE.md` | 你的所有项目 |
| 3 | `~/.claude/rules/*.md` | 你的所有项目 |
| 4(最低) | `./CLAUDE.local.md`(自动加入gitignore) | 仅对你生效 |
Claude会从当前目录向上递归,加载所有找到的CLAUDE.md文件。读取子目录下的文件时,也会识别对应子树中的CLAUDE.md。
**Monorepo策略:** 根目录文件定义通用规则,子树文件定义对应模块的特定规则。root/CLAUDE.md # Universal: tech stack, git workflow
apps/web/CLAUDE.md # Frontend-specific
apps/api/CLAUDE.md # Backend-specific
</file_hierarchy>
<rules_directory>
`.claude/rules/`目录可以将指令拆分为多个聚焦的markdown文件。
**适合使用`.claude/rules/`的场景:** 需管理的规则较多、不同文件类型适用不同规则、团队负责不同模块的维护。
**适合使用CLAUDE.md的场景:** 小型项目、规则通用、需要单一可信源。
路径范围规则使用YAML前置元数据:
```yaml
---
paths:
- "src/api/**/*.ts"
---API Rules
API Rules
- All endpoints must include input validation
Supported patterns: `**/*.ts`, `src/**/*`, `src/**/*.{ts,tsx}`, `{src,lib}/**/*.ts`
Rules without `paths` frontmatter load unconditionally.
See [references/rules-directory-guide.md](references/rules-directory-guide.md) for complete guide including symlinks, user-level rules, and migration.
</rules_directory>
<content_structure>
Structure CLAUDE.md with only these sections:
1. **Project purpose** (1-3 lines) — What the project is
2. **Tech stack** (compact) — Only non-obvious technologies
3. **Commands** — Non-obvious dev, build, and test commands
4. **Important files** — Architecture-critical, non-obvious files only
5. **Rules** — Prohibitions and constraints that prevent mistakes (highest-value lines)
6. **Workflow** (optional) — Only if non-standard
**Do NOT include:**
- Repository overviews (agent discovers structure itself)
- Code style rules (linters enforce these)
- Generic best practices ("write clean code", "DRY", "SOLID")
- Redundant specs (copies of config files, env vars, schema descriptions)
- Marketing/goals (vision statements, KPIs, roadmaps)
- Verbose explanations (paragraphs where one line suffices)
See [references/section-templates.md](references/section-templates.md) for ready-to-use templates.
See [references/project-patterns.md](references/project-patterns.md) for framework-specific patterns.
</content_structure>
<writing_rules>
**Golden rule:** If someone with zero project context reads your CLAUDE.md and gets confused, Claude will too.
**Be specific, never vague:**❌ "Format code properly" / "Write good tests" / "Follow best practices"
✅ "Run before committing" / "Tests in using Vitest"
pnpm lint__tests__/
**Prohibitions > positive guidance:**❌ "Try to use TanStack Form for forms"
✅ "NEVER use native form/useState for forms — ALWAYS use TanStack Form"
**Show, don't tell:** When format matters, show a concrete example (3-5 lines max).
**Emphasis hierarchy:** CRITICAL > NEVER > ALWAYS > IMPORTANT > YOU MUST
- Put critical rules **first** in each section
- Use **bold + keyword** for non-negotiable rules: `**CRITICAL**: Never commit secrets`
See [references/prompting-techniques.md](references/prompting-techniques.md) for advanced techniques.
</writing_rules>
<size_limits>
- **Ideal:** < 100 lines
- **Maximum:** 150 lines before performance degrades
- **Over 200 lines:** directives start getting lost
When exceeding limits, split into `.claude/rules/` files or link to separate docs:
```markdown
- **API patterns**: See [docs/api-patterns.md](docs/api-patterns.md)
- **Testing guide**: See [docs/testing-guide.md](docs/testing-guide.md)CLAUDE.md supports importing: (relative/absolute paths, expansion, up to 5 levels deep, not evaluated inside code blocks).
</size_limits>
<workflow>
**ALWAYS ASK FIRST: Storage Strategy**
@docs/coding-standards.md~Before creating or updating memory files, use AskUserQuestion:
- Option 1: Single CLAUDE.md — < 100 lines, simple project, universal rules
- Option 2: Modular .claude/rules/ — 100+ lines, different rules for different files
Creating new memory:
- Start with or minimal template
/init - Add tech stack and commands first
- Add rules only as you encounter friction
- Test with real tasks, iterate based on Claude's behavior
Maintaining:
- Review quarterly or when project changes significantly
- Remove outdated instructions
- Add patterns that required repeated explanation
- Use for quick additions during work
#
Troubleshooting:
| Problem | Solution |
|---|---|
| Claude ignores instructions | Reduce file size, add emphasis (CRITICAL, NEVER) |
| Context overflow | Use |
| Instructions conflict | Consolidate, use hierarchy (root vs subtree) |
| Path rules not applying | Verify glob pattern matches target files |
<init_workflow>
- All endpoints must include input validation
支持的模式:`**/*.ts`, `src/**/*`, `src/**/*.{ts,tsx}`, `{src,lib}/**/*.ts`
没有`paths`前置元数据的规则会无条件加载。
完整指南(包括符号链接、用户级别规则、迁移方案)请查看[references/rules-directory-guide.md](references/rules-directory-guide.md)。
</rules_directory>
<content_structure>
CLAUDE.md仅可包含以下板块:
1. **项目用途**(1-3行)—— 项目的核心定位
2. **技术栈**(简洁)—— 仅列非显而易见的技术
3. **命令**—— 非显而易见的开发、构建、测试命令
4. **重要文件**—— 仅列架构核心、非显而易见的文件
5. **规则**—— 可避免错误的禁止项和约束(价值最高的内容)
6. **工作流**(可选)—— 仅当流程非标准时添加
**禁止包含以下内容:**
- 仓库概览(Agent可自行发现结构)
- 代码风格规则(已由lint工具强制执行)
- 通用最佳实践(如“编写整洁代码”、“DRY”、“SOLID”)
- 冗余说明(配置文件、环境变量、Schema描述的副本)
- 营销/目标内容(愿景声明、KPI、路线图)
- 冗余解释(能用一行说明就不要用段落)
可用模板请查看[references/section-templates.md](references/section-templates.md)。
框架特定模式请查看[references/project-patterns.md](references/project-patterns.md)。
</content_structure>
<writing_rules>
**黄金法则:** 如果零项目上下文的人阅读你的CLAUDE.md会感到困惑,那么Claude也会。
**要具体,不要模糊:**❌ "Format code properly" / "Write good tests" / "Follow best practices"
✅ "Run before committing" / "Tests in using Vitest"
pnpm lint__tests__/
**禁止项 > 正向引导:**❌ "Try to use TanStack Form for forms"
✅ "NEVER use native form/useState for forms — ALWAYS use TanStack Form"
**展示而非告知:** 格式很重要时,给出具体示例(最多3-5行)。
**优先级层级:** CRITICAL > NEVER > ALWAYS > IMPORTANT > YOU MUST
- 把关键规则放在每个板块的**最前面**
- 不可协商的规则使用**加粗+关键词**标记:`**CRITICAL**: Never commit secrets`
高级技巧请查看[references/prompting-techniques.md](references/prompting-techniques.md)。
</writing_rules>
<size_limits>
- **理想长度:** < 100行
- **最大长度:** 150行,超过后性能会下降
- **超过200行:** 指令会开始丢失
超出长度限制时,拆分为`.claude/rules/`文件或链接到独立文档:
```markdown
- **API patterns**: See [docs/api-patterns.md](docs/api-patterns.md)
- **Testing guide**: See [docs/testing-guide.md](docs/testing-guide.md)CLAUDE.md支持导入:(支持相对/绝对路径、路径展开,最多嵌套5层,代码块内不会解析)。
</size_limits>
<workflow>
**始终优先确认:存储策略**
@docs/coding-standards.md~在创建或更新记忆文件前,使用AskUserQuestion询问:
- 选项1:单CLAUDE.md文件 — < 100行、简单项目、规则通用
- 选项2:模块化.claude/rules/ — 100行以上、不同文件适用不同规则
创建新记忆:
- 从或极简模板开始
/init - 先添加技术栈和命令
- 只有遇到问题时再添加规则
- 用真实任务测试,根据Claude的表现迭代调整
维护:
- 每季度或项目发生重大变更时复查
- 移除过时指令
- 添加需要反复解释的模式
- 工作过程中使用快速添加内容
#
故障排查:
| 问题 | 解决方案 |
|---|---|
| Claude忽略指令 | 减小文件大小,添加强调标记(CRITICAL、NEVER) |
| 上下文溢出 | 使用 |
| 指令冲突 | 合并规则,使用层级配置(根目录vs子目录) |
| 路径规则不生效 | 验证glob模式是否匹配目标文件 |
<init_workflow>
/claude-memory init
— Create Minimal CLAUDE.md
/claude-memory init/claude-memory init
— 创建极简CLAUDE.md
/claude-memory initWhen the user argument contains "init", execute this workflow to scaffold a CLAUDE.md with only the essential sections.
当用户参数包含“init”时,执行此工作流来生成仅包含核心板块的CLAUDE.md脚手架。
Step 1: Detect project context
步骤1:检测项目上下文
Read (or equivalent: , , , etc.) to detect:
package.jsonpyproject.tomlCargo.tomlgo.mod- Project name
- Tech stack (framework, language)
- Available scripts/commands (dev, build, test)
Also check for existing linter configs (ESLint, Biome, Prettier, tsconfig) — do NOT include rules they already enforce.
读取(或等效文件:、、等)来检测:
package.jsonpyproject.tomlCargo.tomlgo.mod- 项目名称
- 技术栈(框架、语言)
- 可用脚本/命令(开发、构建、测试)
同时检查现有的lint配置(ESLint、Biome、Prettier、tsconfig)—— 不要包含这些工具已经强制执行的规则。
Step 2: Identify important files
步骤2:识别重要文件
Scan for non-obvious architecture-critical files. Look for:
- Auth config files
- Server action wrappers, API helpers
- Database schema files
- Custom middleware or shared utilities
- Skip standard framework files (package.json, tsconfig, next.config, etc.)
扫描非显而易见的架构核心文件,查找:
- 鉴权配置文件
- 服务端操作封装、API助手
- 数据库Schema文件
- 自定义中间件或共享工具
- 跳过标准框架文件(package.json、tsconfig、next.config等)
Step 3: Generate CLAUDE.md
步骤3:生成CLAUDE.md
Create the file with ONLY these sections. Each section should be compact — the whole file should be under 50 lines.
markdown
undefined仅使用以下板块创建文件,每个板块保持简洁——整个文件应少于50行。
markdown
undefined[Project Name]
[Project Name]
[One-line description of what this project is]
[One-line description of what this project is]
Tech Stack
Tech Stack
- [Only non-obvious technologies — skip deps detectable from config files]
- [Only non-obvious technologies — skip deps detectable from config files]
Commands
Commands
- - Dev server
[dev command] - - Build
[build command] - - Tests
[test command] - [Any non-obvious commands like db:push, seed, etc.]
- - Dev server
[dev command] - - Build
[build command] - - Tests
[test command] - [Any non-obvious commands like db:push, seed, etc.]
Important Files
Important Files
- [Only files the agent wouldn't discover naturally]
- [Only files the agent wouldn't discover naturally]
Rules
Rules
- [Leave empty with a comment: "Add project-specific rules as you encounter friction"]
undefined- [Leave empty with a comment: "Add project-specific rules as you encounter friction"]
undefinedStep 4: Present and write
步骤4:展示并写入
Show the generated file to the user. Write it to after approval.
./CLAUDE.mdRules:
- NEVER include directory structure, code style rules, generic best practices, or marketing text
- NEVER include anything a linter or TypeScript already enforces
- Target: 20-50 lines. If it's longer, you're including too much
- The Rules section starts empty — it gets populated over time as mistakes happen </init_workflow>
<optimize_workflow>
将生成的文件展示给用户,经批准后写入。
./CLAUDE.md规则:
- 永远不要包含目录结构、代码风格规则、通用最佳实践或营销文本
- 永远不要包含lint或TypeScript已经强制执行的内容
- 目标长度:20-50行,超过则说明包含了过多内容
- 规则板块初始为空——随着后续遇到错误逐步补充 </init_workflow>
<optimize_workflow>
/claude-memory optimize
— Deep CLAUDE.md Cleanup
/claude-memory optimize/claude-memory optimize
— CLAUDE.md深度清理
/claude-memory optimizeWhen the user argument contains "optimize", execute this workflow.
CRITICAL — Step 0 is MANDATORY. Do NOT skip it. Do NOT start optimizing without reading the guide first.
当用户参数包含“optimize”时,执行此工作流。
关键提示——步骤0是强制要求,不得跳过。未阅读指南前不要开始优化。
Step 0: Read the optimization guide (REQUIRED FIRST)
步骤0:阅读优化指南(必须首先执行)
YOU MUST use the Read tool on BEFORE doing anything else.
This file contains the research data (ETH Zurich study), the 6 bloat categories with specific examples, target metrics, and before/after examples. Without reading it, you will miss removal criteria and produce a subpar optimization.
{SKILL_PATH}/references/optimize-guide.md你必须先使用Read工具读取再进行其他操作。
该文件包含研究数据(苏黎世联邦理工学院研究)、6类冗余内容的具体示例、目标指标和前后对比示例。未阅读的话你会遗漏删除标准,导致优化效果不佳。
{SKILL_PATH}/references/optimize-guide.mdStep 1: Inventory
步骤1:清点现有文件
Read every CLAUDE.md, CLAUDE.local.md, and in the project. Count total lines.
.claude/rules/*.md读取项目中所有CLAUDE.md、CLAUDE.local.md和文件,统计总行数。
.claude/rules/*.mdStep 2: Read linter configs
步骤2:读取lint配置
Read ESLint/Biome/Prettier/TypeScript configs. Any CLAUDE.md line duplicating an enforced rule → delete.
读取ESLint/Biome/Prettier/TypeScript配置,CLAUDE.md中任何重复已强制执行规则的行都要删除。
Step 3: Apply the 6 bloat categories from the guide
步骤3:应用指南中的6类冗余内容标准
For each line ask: "Can the agent discover this by reading the project, or does a linter enforce this?" If yes → delete.
Remove everything matching:
- Linter-enforced rules (ESLint, Prettier, Biome, TypeScript strict)
- Marketing / goals / vision (zero code value)
- Obvious info the agent discovers itself (directory structure, framework defaults, deps from package.json)
- Verbose explanations (paragraphs where 1 line suffices, tutorials, history)
- Redundant specs (copies of config files, schema descriptions, env var lists)
- Generic best practices ("write clean code", "DRY", "SOLID")
逐行检查:“Agent可以通过读取项目自行发现该内容吗?或者lint工具已经强制执行该规则吗?”如果是,则删除。
删除所有符合以下类别的内容:
- Lint强制执行的规则(ESLint、Prettier、Biome、TypeScript strict模式)
- 营销/目标/愿景内容(无代码价值)
- Agent可自行发现的显而易见的信息(目录结构、框架默认配置、package.json里的依赖)
- 冗余解释(能用1行说明的段落、教程、历史背景)
- 冗余说明(配置文件、Schema描述、环境变量列表的副本)
- 通用最佳实践(“编写整洁代码”、“DRY”、“SOLID”)
Step 4: Keep only essentials
步骤4:仅保留核心内容
- Project purpose (1-3 lines)
- Tech stack (compact, non-obvious only)
- Core commands (non-obvious only)
- Testing commands
- Important files (non-obvious only)
- Project-specific rules (prohibitions + constraints)
- Workflow (only if non-standard)
- 项目用途(1-3行)
- 技术栈(简洁,仅列非显而易见的内容)
- 核心命令(仅列非显而易见的内容)
- 测试命令
- 重要文件(仅列非显而易见的内容)
- 项目特定规则(禁止项+约束)
- 工作流(仅当非标准时)
Step 5: Compress
步骤5:压缩内容
- Paragraphs → bullet points
- 3-line rules → 1-line rules
- Zero filler words ("In order to", "It's important to note that")
- Merge related items
- 段落转列表
- 3行规则合并为1行
- 删除填充词(“为了”、“需要注意的是”)
- 合并相关条目
Step 6: Present diff
步骤6:展示差异
Show before/after with line counts. For each removal, cite which bloat category it falls under.
Let user approve before applying changes.
Target: < 100 lines ideal, < 150 max.
</optimize_workflow>
<reference_guides>
- Optimization guide: references/optimize-guide.md — Research-backed bloat checklist, 6 removal categories, before/after examples
- Rules directory: references/rules-directory-guide.md — Complete .claude/rules/ guide with path-scoping, YAML syntax, symlinks, migration
- Prompting techniques: references/prompting-techniques.md — Emphasis strategies, clarity techniques, constraint patterns
- Section templates: references/section-templates.md — Copy-paste templates for each section type
- Comprehensive example: references/comprehensive-example.md — Full production SaaS CLAUDE.md
- Project patterns: references/project-patterns.md — Next.js, Express, Python, Monorepo patterns </reference_guides>
展示前后对比和行数变化,每个删除项要说明属于哪类冗余内容。
应用变更前需要获得用户批准。
目标: < 100行理想,最大不超过150行。
</optimize_workflow>
<reference_guides>
- 优化指南:references/optimize-guide.md — 研究支持的冗余检查清单、6类删除标准、前后对比示例
- 规则目录指南:references/rules-directory-guide.md — 完整的.claude/rules/指南,包含路径范围配置、YAML语法、符号链接、迁移方案
- 提示词技巧:references/prompting-techniques.md — 强调策略、清晰化技巧、约束模式
- 板块模板:references/section-templates.md — 各板块的可复制模板
- 完整示例:references/comprehensive-example.md — 生产环境SaaS项目的完整CLAUDE.md示例
- 项目模式:references/project-patterns.md — Next.js、Express、Python、Monorepo模式 </reference_guides>