context-hierarchy-design

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Context 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:
  1. Measure CLAUDE.md size
  2. Count imports and their sizes
  3. Review command coverage
  4. Identify task types
text
Checklist:
- [ ] CLAUDE.md token count
- [ ] Import count and total
- [ ] Task types identified
- [ ] Command coverage mapped
分析现有上下文基础设施:
  1. 测量CLAUDE.md的大小
  2. 统计导入文件及其大小
  3. 检查指令覆盖范围
  4. 识别任务类型
text
Checklist:
- [ ] CLAUDE.md token count
- [ ] Import count and total
- [ ] Task types identified
- [ ] Command coverage mapped

Step 2: Categorize Context

步骤2:分类上下文

For each piece of existing context, ask:
QuestionIf 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
undefined

Project 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

  • cmd test
    - Run tests
  • cmd build
    - Build project
  • cmd test
    - Run tests
  • cmd build
    - Build project

Critical Rules

Critical Rules

  1. Most important rule
  2. Second most important
  3. Third most important (max 5)

Target size: less than 2KB.
  1. Most important rule
  2. Second most important
  3. 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 TypeCommandContext Loaded
General/primeREADME, git status
Bug Fix/prime-bugRecent commits, test files
Feature/prime-featureArchitecture, API patterns
Review/prime-reviewStyle guide, PR diff
Docs/prime-docsExisting docs, API surface
Priming Command Template:
markdown
undefined
为每种任务类型创建指令:
任务类型指令加载的上下文
通用/primeREADME、git状态
修复Bug/prime-bug最近提交记录、测试文件
开发功能/prime-feature架构、API模式
代码评审/prime-review风格指南、PR差异
文档编写/prime-docs现有文档、API接口
Priming指令模板:
markdown
undefined

Prime: [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]
undefined

Step 5: Design Tier 3 (On-Demand)

步骤5:设计Tier 3(按需加载)

Identify files loaded during work:
CategoryLoading Strategy
Source filesRead specific file when editing
Test filesRead when running tests
ConfigsRead when configuring
DependenciesRead when debugging deps
Principle: Never pre-load what can be loaded on-demand.
识别工作过程中需要加载的文件:
类别加载策略
源文件编辑时读取特定文件
测试文件运行测试时读取
配置文件配置时读取
依赖文件调试依赖时读取
原则: 绝不预加载可按需加载的内容。

Output Format

输出格式

Design document structure:
markdown
undefined
设计文档结构:
markdown
undefined

Context 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

  1. Backup current CLAUDE.md
  2. Create minimal CLAUDE.md
  3. Create priming commands
  4. Test with common task types
  5. Iterate based on usage
undefined
  1. Backup current CLAUDE.md
  2. Create minimal CLAUDE.md
  3. Create priming commands
  4. Test with common task types
  5. Iterate based on usage
undefined

Design 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 context
text
/prime -> Base context
/prime-{type} -> Type-specific context

Validation 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-PatternProblemSolution
Everything in CLAUDE.mdBloatMove to priming
No priming commandsStatic contextAdd task-type priming
Import everythingWasted tokensConditional imports
Directory CLAUDE.md bloatCompoundingKeep 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