context-management

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Context Management Setup

上下文管理设置

Automates context management best practices for Claude Code projects. Analyzes the current project and generates optimized configuration files.
为Claude Code项目自动化实现上下文管理最佳实践。 分析当前项目并生成优化后的配置文件。

When to Use

适用场景

  • Starting a new project (no CLAUDE.md exists)
  • Existing project needs context optimization
  • User reports context loss after compaction
  • User wants to set up rules, memory, or subagent guidelines
  • Before a long coding session
  • 启动新项目(尚无CLAUDE.md)
  • 现有项目需要上下文优化
  • 用户反馈压缩后出现上下文丢失
  • 用户想要设置规则、内存或子代理指南
  • 长时间编码会话开始前

Workflow

工作流程

Step 1: Analyze the Project

步骤1:分析项目

Before generating anything, read the project to understand its shape:
  1. Read
    package.json
    ,
    Cargo.toml
    ,
    pyproject.toml
    , or equivalent to detect stack
  2. Read existing
    CLAUDE.md
    if present (will update, not overwrite)
  3. Check for
    .claude/rules/
    directory
  4. Scan
    src/
    structure to identify major areas (api, components, lib, tests, etc.)
  5. Check for existing
    tsconfig.json
    ,
    .eslintrc
    ,
    prettier.config
    for conventions
  6. Read
    README.md
    if present for project context
Collect this into a mental model of the project before proceeding.
在生成任何内容前,先读取项目了解其结构:
  1. 读取
    package.json
    Cargo.toml
    pyproject.toml
    或等效文件,识别技术栈
  2. 如果已有
    CLAUDE.md
    则读取该文件(将更新而非覆盖)
  3. 检查是否存在
    .claude/rules/
    目录
  4. 扫描
    src/
    结构,识别核心模块(api、components、lib、tests等)
  5. 检查已有
    tsconfig.json
    .eslintrc
    prettier.config
    等文件,获取编码规范
  6. 如果有
    README.md
    则读取,获取项目上下文
在继续操作前将上述信息整合为项目的心智模型。

Step 2: Generate or Update CLAUDE.md

步骤2:生成或更新CLAUDE.md

The CLAUDE.md MUST stay under 200 lines. Use @references for details.
Structure to follow:
markdown
undefined
CLAUDE.md必须保持在200行以内,详情使用@引用。
遵循以下结构:
markdown
undefined

[Project Name]

[Project Name]

[1-2 sentence description from README/package.json]
[1-2 sentence description from README/package.json]

Stack

Stack

[Detected stack — language, framework, DB, key libs]
[Detected stack — language, framework, DB, key libs]

Key Commands

Key Commands

[Detected from package.json scripts, Makefile, etc.]
  • Dev:
    [command]
  • Build:
    [command]
  • Test:
    [command]
  • Lint:
    [command]
[Detected from package.json scripts, Makefile, etc.]
  • Dev:
    [command]
  • Build:
    [command]
  • Test:
    [command]
  • Lint:
    [command]

Architecture

Architecture

[Brief description of src/ structure — max 5-8 lines]
  • @[key-file-1] for [purpose]
  • @[key-file-2] for [purpose]
[Brief description of src/ structure — max 5-8 lines]
  • @[key-file-1] for [purpose]
  • @[key-file-2] for [purpose]

Conventions

Conventions

[Detected from config files — indent, quotes, semicolons, naming]
[Detected from config files — indent, quotes, semicolons, naming]

Context Management

Context Management

Compact Instructions

Compact Instructions

When compacting, always preserve:
  • List of modified files and their purpose
  • Failing tests with error messages
  • Architectural decisions made this session
  • Current task status and next steps
When compacting, safe to discard:
  • File contents that were only read for exploration
  • Search results that didn't lead anywhere
  • Verbose command output already acted upon
When compacting, always preserve:
  • List of modified files and their purpose
  • Failing tests with error messages
  • Architectural decisions made this session
  • Current task status and next steps
When compacting, safe to discard:
  • File contents that were only read for exploration
  • Search results that didn't lead anywhere
  • Verbose command output already acted upon

Subagent Guidelines

Subagent Guidelines

  • Use Explore agent for codebase searches requiring > 3 queries
  • Use background agents for running test suites
  • Use Plan agent before refactors touching > 5 files
  • Delegate file-heavy research to subagents to protect main context
  • Use Explore agent for codebase searches requiring > 3 queries
  • Use background agents for running test suites
  • Use Plan agent before refactors touching > 5 files
  • Delegate file-heavy research to subagents to protect main context

Rules

Rules

See .claude/rules/ for scoped coding rules.

**Important:** If CLAUDE.md already exists, MERGE — don't replace. Add missing sections only.
See .claude/rules/ for scoped coding rules.

**注意:**如果已存在CLAUDE.md,请合并而非替换,仅补充缺失的章节。

Step 3: Generate Scoped Rules

步骤3:生成作用域规则

Create
.claude/rules/
with rules scoped to relevant paths. Only create rules for areas that actually exist in the project.
Read
references/rule-templates.md
for templates per area.
Rules to consider creating (only if the area exists):
FileScopeCreates when
code-style.md
**/*
Always — general conventions
api.md
src/api/**
,
src/routes/api/**
API routes exist
components.md
src/components/**
,
src/lib/components/**
UI components exist
testing.md
**/*.test.*
,
**/*.spec.*
Test files exist
database.md
prisma/**
,
src/db/**
,
drizzle/**
ORM/DB config exists
Each rule file should be 10-30 lines max. Use the templates from references.
创建
.claude/rules/
目录,存放对应路径的规则。 仅为项目中实际存在的模块创建规则。
读取
references/rule-templates.md
获取各模块的规则模板。
可考虑创建的规则(仅当对应模块存在时):
文件名作用域创建时机
code-style.md
**/*
始终创建 — 通用规范
api.md
src/api/**
src/routes/api/**
存在API路由时
components.md
src/components/**
src/lib/components/**
存在UI组件时
testing.md
**/*.test.*
**/*.spec.*
存在测试文件时
database.md
prisma/**
src/db/**
drizzle/**
存在ORM/DB配置时
每个规则文件最多10-30行,使用参考目录中的模板。

Step 4: Initialize Memory Structure

步骤4:初始化内存结构

If auto-memory is enabled, create initial memory notes:
~/.claude/projects/<project-hash>/memory/MEMORY.md
Content:
markdown
undefined
如果启用了自动内存功能,创建初始内存记录:
~/.claude/projects/<project-hash>/memory/MEMORY.md
内容:
markdown
undefined

[Project Name] Memory

[Project Name] Memory

Project

Project

  • Stack: [detected]
  • Key paths: [detected]
  • Stack: [detected]
  • Key paths: [detected]

Patterns

Patterns

[Leave empty — Claude fills as it learns]
[Leave empty — Claude fills as it learns]

Debugging

Debugging

[Leave empty — Claude fills as issues arise]
undefined
[Leave empty — Claude fills as issues arise]
undefined

Step 5: Summary Report

步骤5:汇总报告

After setup, show the user a summary:
Context Management Setup Complete
==================================
CLAUDE.md:     [created/updated] (X lines)
Rules:         [N] files in .claude/rules/
Memory:        [initialized/already exists]

Files created:
  - .claude/rules/code-style.md
  - .claude/rules/[others...]

Recommendations:
  - [Any project-specific suggestions]
设置完成后,向用户展示汇总信息:
Context Management Setup Complete
==================================
CLAUDE.md:     [created/updated] (X lines)
Rules:         [N] files in .claude/rules/
Memory:        [initialized/already exists]

Files created:
  - .claude/rules/code-style.md
  - .claude/rules/[others...]

Recommendations:
  - [Any project-specific suggestions]

Important Constraints

重要约束

  • CLAUDE.md must stay under 200 lines
  • Each rule file must stay under 30 lines
  • Never overwrite existing files without showing diff first
  • Detect conventions from config files, don't assume
  • If stack is unknown, ask the user before generating
  • Use @references in CLAUDE.md instead of inlining large content
  • CLAUDE.md必须保持在200行以内
  • 每个规则文件必须保持在30行以内
  • 不得覆盖现有文件,需先展示差异
  • 从配置文件中识别编码规范,不要主观假设
  • 如果技术栈未知,生成前先询问用户
  • 在CLAUDE.md中使用@引用,而非直接嵌入大量内容