strategic-compact
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStrategic Compact Skill
策略性压缩技能
Suggests manual at strategic points in your workflow rather than relying on arbitrary auto-compaction.
/compact建议在工作流的关键策略节点手动执行命令,而非依赖任意触发的自动压缩。
/compactWhen to Activate
激活场景
- Running long sessions that approach context limits (200K+ tokens)
- Working on multi-phase tasks (research → plan → implement → test)
- Switching between unrelated tasks within the same session
- After completing a major milestone and starting new work
- When responses slow down or become less coherent (context pressure)
- 运行接近上下文限制的长会话(200K+令牌)
- 处理多阶段任务(研究→规划→实现→测试)
- 在同一会话中切换不相关任务
- 完成重要里程碑并开始新工作时
- 响应变慢或连贯性下降时(上下文压力)
Why Strategic Compaction?
为什么选择策略性压缩?
Auto-compaction triggers at arbitrary points:
- Often mid-task, losing important context
- No awareness of logical task boundaries
- Can interrupt complex multi-step operations
Strategic compaction at logical boundaries:
- After exploration, before execution — Compact research context, keep implementation plan
- After completing a milestone — Fresh start for next phase
- Before major context shifts — Clear exploration context before different task
自动压缩会在任意节点触发:
- 常发生在任务进行中,丢失重要上下文
- 无法识别逻辑任务边界
- 可能中断复杂的多步骤操作
在逻辑边界进行策略性压缩:
- 探索完成后,执行前——压缩研究上下文,保留实现规划
- 完成里程碑后——为下一阶段提供全新开始
- 重大上下文切换前——在切换到不同任务前清理探索上下文
How It Works
工作原理
The script runs on PreToolUse (Edit/Write) and:
suggest-compact.js- Tracks tool calls — Counts tool invocations in session
- Threshold detection — Suggests at configurable threshold (default: 50 calls)
- Periodic reminders — Reminds every 25 calls after threshold
suggest-compact.js- 跟踪工具调用——统计会话中的工具调用次数
- 阈值检测——在可配置的阈值(默认:50次调用)时建议压缩
- 定期提醒——达到阈值后每25次调用提醒一次
Hook Setup
钩子设置
Add to your :
~/.claude/settings.jsonjson
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit",
"hooks": [{ "type": "command", "command": "node ~/.claude/skills/strategic-compact/suggest-compact.js" }]
},
{
"matcher": "Write",
"hooks": [{ "type": "command", "command": "node ~/.claude/skills/strategic-compact/suggest-compact.js" }]
}
]
}
}添加到你的:
~/.claude/settings.jsonjson
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit",
"hooks": [{ "type": "command", "command": "node ~/.claude/skills/strategic-compact/suggest-compact.js" }]
},
{
"matcher": "Write",
"hooks": [{ "type": "command", "command": "node ~/.claude/skills/strategic-compact/suggest-compact.js" }]
}
]
}
}Configuration
配置
Environment variables:
- — Tool calls before first suggestion (default: 50)
COMPACT_THRESHOLD
环境变量:
- ——首次建议压缩前的工具调用次数(默认:50)
COMPACT_THRESHOLD
Compaction Decision Guide
压缩决策指南
Use this table to decide when to compact:
| Phase Transition | Compact? | Why |
|---|---|---|
| Research → Planning | Yes | Research context is bulky; plan is the distilled output |
| Planning → Implementation | Yes | Plan is in TodoWrite or a file; free up context for code |
| Implementation → Testing | Maybe | Keep if tests reference recent code; compact if switching focus |
| Debugging → Next feature | Yes | Debug traces pollute context for unrelated work |
| Mid-implementation | No | Losing variable names, file paths, and partial state is costly |
| After a failed approach | Yes | Clear the dead-end reasoning before trying a new approach |
使用下表决定何时压缩:
| 阶段转换 | 是否压缩? | 原因 |
|---|---|---|
| 研究→规划 | 是 | 研究上下文冗余,规划是提炼后的核心输出 |
| 规划→实现 | 是 | 规划已存入TodoWrite或文件,释放上下文用于代码编写 |
| 实现→测试 | 视情况而定 | 若测试需参考近期代码则保留;若切换焦点则压缩 |
| 调试→下一功能 | 是 | 调试痕迹会污染无关工作的上下文 |
| 实现过程中 | 否 | 丢失变量名、文件路径和部分状态代价高昂 |
| 尝试失败方案后 | 是 | 在尝试新方案前清理无效推理内容 |
What Survives Compaction
压缩后保留的内容
Understanding what persists helps you compact with confidence:
| Persists | Lost |
|---|---|
| CLAUDE.md instructions | Intermediate reasoning and analysis |
| TodoWrite task list | File contents you previously read |
Memory files ( | Multi-step conversation context |
| Git state (commits, branches) | Tool call history and counts |
| Files on disk | Nuanced user preferences stated verbally |
了解压缩后保留的内容能让你更放心地执行压缩:
| 保留内容 | 丢失内容 |
|---|---|
| CLAUDE.md 指令 | 中间推理和分析过程 |
| TodoWrite 任务列表 | 你之前读取过的文件内容 |
内存文件( | 多步骤对话上下文 |
| Git 状态(提交、分支) | 工具调用历史和计数 |
| 磁盘上的文件 | 口头提及的细微用户偏好 |
Best Practices
最佳实践
- Compact after planning — Once plan is finalized in TodoWrite, compact to start fresh
- Compact after debugging — Clear error-resolution context before continuing
- Don't compact mid-implementation — Preserve context for related changes
- Read the suggestion — The hook tells you when, you decide if
- Write before compacting — Save important context to files or memory before compacting
- Use with a summary — Add a custom message:
/compact/compact Focus on implementing auth middleware next
- 规划完成后压缩——一旦规划在TodoWrite中定稿,压缩以全新开始
- 调试完成后压缩——在继续工作前清理错误排查上下文
- 实现过程中不要压缩——保留上下文以支持相关变更
- 查看建议信息——钩子提示你时机,由你决定是否执行
- 压缩前先保存——在压缩前将重要上下文保存到文件或内存中
- 带摘要使用——添加自定义消息:
/compact(接下来专注于实现认证中间件)/compact Focus on implementing auth middleware next
Token Optimization Patterns
令牌优化模式
Trigger-Table Lazy Loading
触发表懒加载
Instead of loading full skill content at session start, use a trigger table that maps keywords to skill paths. Skills load only when triggered, reducing baseline context by 50%+:
| Trigger | Skill | Load When |
|---|---|---|
| "test", "tdd", "coverage" | tdd-workflow | User mentions testing |
| "security", "auth", "xss" | security-review | Security-related work |
| "deploy", "ci/cd" | deployment-patterns | Deployment context |
不要在会话开始时加载完整技能内容,使用触发表将关键词映射到技能路径。技能仅在触发时加载,可减少50%以上的基线上下文:
| 触发词 | 技能 | 加载时机 |
|---|---|---|
| "test", "tdd", "coverage" | tdd-workflow | 用户提及测试时 |
| "security", "auth", "xss" | security-review | 处理安全相关工作时 |
| "deploy", "ci/cd" | deployment-patterns | 部署场景下 |
Context Composition Awareness
上下文组合感知
Monitor what's consuming your context window:
- CLAUDE.md files — Always loaded, keep lean
- Loaded skills — Each skill adds 1-5K tokens
- Conversation history — Grows with each exchange
- Tool results — File reads, search results add bulk
监控占用上下文窗口的内容:
- CLAUDE.md 文件——始终加载,保持精简
- 已加载技能——每个技能增加1-5K令牌
- 对话历史——随每次交互增长
- 工具结果——文件读取、搜索结果会增加体积
Duplicate Instruction Detection
重复指令检测
Common sources of duplicate context:
- Same rules in both and project
~/.claude/rules/.claude/rules/ - Skills that repeat CLAUDE.md instructions
- Multiple skills covering overlapping domains
重复上下文的常见来源:
- 和项目
~/.claude/rules/中存在相同规则.claude/rules/ - 重复CLAUDE.md指令的技能
- 覆盖重叠领域的多个技能
Context Optimization Tools
上下文优化工具
- MCP — Automated 95%+ token reduction via content deduplication
token-optimizer - — Context virtualization (315KB to 5.4KB demonstrated)
context-mode
- MCP——通过内容去重实现95%以上的令牌减少
token-optimizer - ——上下文虚拟化(已验证可从315KB降至5.4KB)
context-mode
Related
相关资源
- The Longform Guide — Token optimization section
- Memory persistence hooks — For state that survives compaction
- skill — Extracts patterns before session ends
continuous-learning
- 长篇内容指南——令牌优化章节
- 内存持久化钩子——用于压缩后仍需保留的状态
- 技能——在会话结束前提取模式
continuous-learning