strategic-compact

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Strategic Compact Skill

策略性压缩技能

Suggests manual
/compact
at strategic points in your workflow rather than relying on arbitrary auto-compaction.
建议在工作流的关键策略节点手动执行
/compact
命令,而非依赖任意触发的自动压缩。

When 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
suggest-compact.js
script runs on PreToolUse (Edit/Write) and:
  1. Tracks tool calls — Counts tool invocations in session
  2. Threshold detection — Suggests at configurable threshold (default: 50 calls)
  3. Periodic reminders — Reminds every 25 calls after threshold
suggest-compact.js
脚本在PreToolUse(编辑/写入)阶段运行:
  1. 跟踪工具调用——统计会话中的工具调用次数
  2. 阈值检测——在可配置的阈值(默认:50次调用)时建议压缩
  3. 定期提醒——达到阈值后每25次调用提醒一次

Hook Setup

钩子设置

Add to your
~/.claude/settings.json
:
json
{
  "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.json
json
{
  "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:
  • COMPACT_THRESHOLD
    — Tool calls before first suggestion (default: 50)
环境变量:
  • COMPACT_THRESHOLD
    ——首次建议压缩前的工具调用次数(默认:50)

Compaction Decision Guide

压缩决策指南

Use this table to decide when to compact:
Phase TransitionCompact?Why
Research → PlanningYesResearch context is bulky; plan is the distilled output
Planning → ImplementationYesPlan is in TodoWrite or a file; free up context for code
Implementation → TestingMaybeKeep if tests reference recent code; compact if switching focus
Debugging → Next featureYesDebug traces pollute context for unrelated work
Mid-implementationNoLosing variable names, file paths, and partial state is costly
After a failed approachYesClear the dead-end reasoning before trying a new approach
使用下表决定何时压缩:
阶段转换是否压缩?原因
研究→规划研究上下文冗余,规划是提炼后的核心输出
规划→实现规划已存入TodoWrite或文件,释放上下文用于代码编写
实现→测试视情况而定若测试需参考近期代码则保留;若切换焦点则压缩
调试→下一功能调试痕迹会污染无关工作的上下文
实现过程中丢失变量名、文件路径和部分状态代价高昂
尝试失败方案后在尝试新方案前清理无效推理内容

What Survives Compaction

压缩后保留的内容

Understanding what persists helps you compact with confidence:
PersistsLost
CLAUDE.md instructionsIntermediate reasoning and analysis
TodoWrite task listFile contents you previously read
Memory files (
~/.claude/memory/
)
Multi-step conversation context
Git state (commits, branches)Tool call history and counts
Files on diskNuanced user preferences stated verbally
了解压缩后保留的内容能让你更放心地执行压缩:
保留内容丢失内容
CLAUDE.md 指令中间推理和分析过程
TodoWrite 任务列表你之前读取过的文件内容
内存文件(
~/.claude/memory/
多步骤对话上下文
Git 状态(提交、分支)工具调用历史和计数
磁盘上的文件口头提及的细微用户偏好

Best Practices

最佳实践

  1. Compact after planning — Once plan is finalized in TodoWrite, compact to start fresh
  2. Compact after debugging — Clear error-resolution context before continuing
  3. Don't compact mid-implementation — Preserve context for related changes
  4. Read the suggestion — The hook tells you when, you decide if
  5. Write before compacting — Save important context to files or memory before compacting
  6. Use
    /compact
    with a summary
    — Add a custom message:
    /compact Focus on implementing auth middleware next
  1. 规划完成后压缩——一旦规划在TodoWrite中定稿,压缩以全新开始
  2. 调试完成后压缩——在继续工作前清理错误排查上下文
  3. 实现过程中不要压缩——保留上下文以支持相关变更
  4. 查看建议信息——钩子提示你时机,由你决定是否执行
  5. 压缩前先保存——在压缩前将重要上下文保存到文件或内存中
  6. 带摘要使用
    /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%+:
TriggerSkillLoad When
"test", "tdd", "coverage"tdd-workflowUser mentions testing
"security", "auth", "xss"security-reviewSecurity-related work
"deploy", "ci/cd"deployment-patternsDeployment 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
    ~/.claude/rules/
    and project
    .claude/rules/
  • Skills that repeat CLAUDE.md instructions
  • Multiple skills covering overlapping domains
重复上下文的常见来源:
  • ~/.claude/rules/
    和项目
    .claude/rules/
    中存在相同规则
  • 重复CLAUDE.md指令的技能
  • 覆盖重叠领域的多个技能

Context Optimization Tools

上下文优化工具

  • token-optimizer
    MCP — Automated 95%+ token reduction via content deduplication
  • context-mode
    — Context virtualization (315KB to 5.4KB demonstrated)
  • token-optimizer
    MCP——通过内容去重实现95%以上的令牌减少
  • context-mode
    ——上下文虚拟化(已验证可从315KB降至5.4KB)

Related

相关资源

  • The Longform Guide — Token optimization section
  • Memory persistence hooks — For state that survives compaction
  • continuous-learning
    skill — Extracts patterns before session ends
  • 长篇内容指南——令牌优化章节
  • 内存持久化钩子——用于压缩后仍需保留的状态
  • continuous-learning
    技能——在会话结束前提取模式