context-hierarchy-design
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseContext Hierarchy Design Skill
上下文层级设计技能
Design a memory hierarchy that loads context progressively based on task needs.
设计一个根据任务需求渐进式加载上下文的内存层级结构。
Purpose
用途
Not all context is needed all the time. A well-designed hierarchy ensures agents get exactly the context they need without bloat.
并非所有上下文始终都是必需的。一个设计良好的层级结构可确保Agent获得其所需的准确上下文,而不会产生冗余。
When to Use
适用场景
- Setting up a new project's context infrastructure
- Refactoring bloated CLAUDE.md files
- Creating task-specific context loading
- Optimizing agent startup time
- Scaling context for multiple task types
- 搭建新项目的上下文基础设施
- 重构臃肿的CLAUDE.md文件
- 创建特定任务的上下文加载机制
- 优化Agent启动时间
- 为多种任务类型扩展上下文
The Three-Tier Memory Strategy
三级内存策略
text
Tier 1: CLAUDE.md (minimal, always loaded)
|
v
Tier 2: Priming Commands (task-specific, on-demand)
|
v
Tier 3: File Reads (just-in-time, as needed)text
Tier 1: CLAUDE.md (minimal, always loaded)
|
v
Tier 2: Priming Commands (task-specific, on-demand)
|
v
Tier 3: File Reads (just-in-time, as needed)Design Process
设计流程
Step 1: Audit Current State
步骤1:审计当前状态
Analyze existing context infrastructure:
- Measure CLAUDE.md size
- Count imports and their sizes
- Review command coverage
- Identify task types
text
Checklist:
- [ ] CLAUDE.md token count
- [ ] Import count and total
- [ ] Task types identified
- [ ] Command coverage mapped分析现有上下文基础设施:
- 测量CLAUDE.md的大小
- 统计导入文件及其大小
- 检查指令覆盖范围
- 识别任务类型
text
Checklist:
- [ ] CLAUDE.md token count
- [ ] Import count and total
- [ ] Task types identified
- [ ] Command coverage mappedStep 2: Categorize Context
步骤2:分类上下文
For each piece of existing context, ask:
| Question | If Yes -> |
|---|---|
| Needed for EVERY task? | Tier 1 (CLAUDE.md) |
| Needed for task TYPE? | Tier 2 (Priming) |
| Needed for specific work? | Tier 3 (On-demand) |
对于每一段现有上下文,思考:
| 问题 | 如果是 -> |
|---|---|
| 所有任务都需要吗? | Tier 1(CLAUDE.md) |
| 特定任务类型需要吗? | Tier 2(Priming) |
| 特定工作场景需要吗? | Tier 3(按需加载) |
Step 3: Design Tier 1 (CLAUDE.md)
步骤3:设计Tier 1(CLAUDE.md)
Minimal essentials only:
markdown
undefined仅保留最基础的必要内容:
markdown
undefinedProject Name
Project Name
Context
Context
One-sentence project description.
One-sentence project description.
Tooling
Tooling
- Language: X
- Runtime: Y
- Package Manager: Z
- Language: X
- Runtime: Y
- Package Manager: Z
Key Commands
Key Commands
- - Run tests
cmd test - - Build project
cmd build
- - Run tests
cmd test - - Build project
cmd build
Critical Rules
Critical Rules
- Most important rule
- Second most important
- Third most important (max 5)
Target size: less than 2KB.- Most important rule
- Second most important
- Third most important (max 5)
目标大小:小于2KB。Step 4: Design Tier 2 (Priming Commands)
步骤4:设计Tier 2(Priming Commands)
Create commands for each task type:
| Task Type | Command | Context Loaded |
|---|---|---|
| General | /prime | README, git status |
| Bug Fix | /prime-bug | Recent commits, test files |
| Feature | /prime-feature | Architecture, API patterns |
| Review | /prime-review | Style guide, PR diff |
| Docs | /prime-docs | Existing docs, API surface |
Priming Command Template:
markdown
undefined为每种任务类型创建指令:
| 任务类型 | 指令 | 加载的上下文 |
|---|---|---|
| 通用 | /prime | README、git状态 |
| 修复Bug | /prime-bug | 最近提交记录、测试文件 |
| 开发功能 | /prime-feature | 架构、API模式 |
| 代码评审 | /prime-review | 风格指南、PR差异 |
| 文档编写 | /prime-docs | 现有文档、API接口 |
Priming指令模板:
markdown
undefinedPrime: [Task Type]
Prime: [Task Type]
Run
Run
[Discovery commands for current state]
[Discovery commands for current state]
Read
Read
[Essential files for this task type]
[Essential files for this task type]
Report
Report
[Summary prompt for understanding]
undefined[Summary prompt for understanding]
undefinedStep 5: Design Tier 3 (On-Demand)
步骤5:设计Tier 3(按需加载)
Identify files loaded during work:
| Category | Loading Strategy |
|---|---|
| Source files | Read specific file when editing |
| Test files | Read when running tests |
| Configs | Read when configuring |
| Dependencies | Read when debugging deps |
Principle: Never pre-load what can be loaded on-demand.
识别工作过程中需要加载的文件:
| 类别 | 加载策略 |
|---|---|
| 源文件 | 编辑时读取特定文件 |
| 测试文件 | 运行测试时读取 |
| 配置文件 | 配置时读取 |
| 依赖文件 | 调试依赖时读取 |
原则: 绝不预加载可按需加载的内容。
Output Format
输出格式
Design document structure:
markdown
undefined设计文档结构:
markdown
undefinedContext Hierarchy Design: [Project]
Context Hierarchy Design: [Project]
Tier 1: CLAUDE.md (Always Loaded)
Tier 1: CLAUDE.md (Always Loaded)
Target Size: <2KB
Content:
- Project context (1 sentence)
- Tooling (language, runtime, PM)
- Key commands (3-5)
- Critical rules (3-5)
Target Size: <2KB
Content:
- Project context (1 sentence)
- Tooling (language, runtime, PM)
- Key commands (3-5)
- Critical rules (3-5)
Tier 2: Priming Commands (Task-Specific)
Tier 2: Priming Commands (Task-Specific)
/prime
/prime
Purpose: General codebase understanding
Loads: README, git status, project structure
Purpose: General codebase understanding
Loads: README, git status, project structure
/prime-bug
/prime-bug
Purpose: Bug fixing context
Loads: Recent commits, test patterns, error logs
Purpose: Bug fixing context
Loads: Recent commits, test patterns, error logs
/prime-feature
/prime-feature
Purpose: Feature development context
Loads: Architecture, API patterns, similar features
Purpose: Feature development context
Loads: Architecture, API patterns, similar features
Tier 3: On-Demand (Just-in-Time)
Tier 3: On-Demand (Just-in-Time)
Strategy: Load during execution, not pre-emptively
- Source files: Read when editing
- Tests: Read when testing
- Configs: Read when configuring
Strategy: Load during execution, not pre-emptively
- Source files: Read when editing
- Tests: Read when testing
- Configs: Read when configuring
Migration Plan
Migration Plan
- Backup current CLAUDE.md
- Create minimal CLAUDE.md
- Create priming commands
- Test with common task types
- Iterate based on usage
undefined- Backup current CLAUDE.md
- Create minimal CLAUDE.md
- Create priming commands
- Test with common task types
- Iterate based on usage
undefinedDesign Patterns
设计模式
Pattern: Import Hierarchy
模式:导入层级结构
text
CLAUDE.md (always)
-> imports/core.md (always, if large)
-> imports/tooling.md (conditional)
-> imports/workflows.md (conditional)text
CLAUDE.md (always)
-> imports/core.md (always, if large)
-> imports/tooling.md (conditional)
-> imports/workflows.md (conditional)Pattern: Directory-Scoped Memory
模式:目录范围内存
text
/project
CLAUDE.md (project-wide)
/frontend
CLAUDE.md (frontend-specific)
/backend
CLAUDE.md (backend-specific)text
/project
CLAUDE.md (project-wide)
/frontend
CLAUDE.md (frontend-specific)
/backend
CLAUDE.md (backend-specific)Pattern: Task-Type Priming
模式:任务类型预启动
text
/prime -> Base context
/prime-{type} -> Type-specific contexttext
/prime -> Base context
/prime-{type} -> Type-specific contextValidation Criteria
验证标准
After design, verify:
- CLAUDE.md under 2KB
- Every task type has priming command
- No "just in case" loading
- Progressive disclosure works
- Common workflows smooth
设计完成后,验证以下内容:
- CLAUDE.md大小小于2KB
- 每种任务类型都有对应的Priming指令
- 不存在“以防万一”的加载
- 渐进式披露机制有效
- 常见工作流程顺畅
Anti-Patterns to Avoid
需避免的反模式
| Anti-Pattern | Problem | Solution |
|---|---|---|
| Everything in CLAUDE.md | Bloat | Move to priming |
| No priming commands | Static context | Add task-type priming |
| Import everything | Wasted tokens | Conditional imports |
| Directory CLAUDE.md bloat | Compounding | Keep scoped minimal |
| 反模式 | 问题 | 解决方案 |
|---|---|---|
| 所有内容都放在CLAUDE.md中 | 冗余臃肿 | 迁移到Priming指令 |
| 无Priming指令 | 上下文静态不变 | 添加任务类型专属Priming |
| 导入所有内容 | 浪费Token | 使用条件导入 |
| 目录级CLAUDE.md臃肿 | 冗余叠加 | 保持范围内容极简 |
Key Quote
关键引用
"Use context priming over CLAUDE.md or similar auto-loading memory files. Engineering work constantly changes, but memory files only grow."
“优先使用上下文Priming,而非CLAUDE.md或类似的自动加载内存文件。工程工作内容不断变化,但内存文件只会不断膨胀。”
Cross-References
交叉引用
- @context-priming-patterns.md - Priming command patterns
- @rd-framework.md - Reduce unnecessary context
- @context-layers.md - What loads into context
- @minimum-context-principle.md - Include only what's necessary
- @context-priming-patterns.md - Priming指令模式
- @rd-framework.md - 减少不必要的上下文
- @context-layers.md - 上下文加载内容
- @minimum-context-principle.md - 仅保留必要内容
Version History
版本历史
- v1.0.0 (2025-12-26): Initial release
- v1.0.0 (2025-12-26): 初始版本
Last Updated
最后更新
Date: 2025-12-26
Model: claude-opus-4-5-20251101
日期: 2025-12-26
模型: claude-opus-4-5-20251101