Origin: This skill was extracted from Claude Code's internal implementation and rules. Claude Code openly exposes its safety mechanisms (hooks, system prompts, skill definitions) in the ~/.claude/ directory. The tiered compression strategy, layer classification system, and memory file structure were reverse-engineered from Claude Code's PreCompact/PostCompact hooks and session memory handling, then adapted for OpenClaw's multi-channel environment.
Context Compression
Why Tiered Compression Is Needed
A one-size-fits-all approach to dropping context either leaves the agent without memory or burns through context too quickly. The core principle of tiered strategy is: different information has different lifecycles. A user's statement "I prefer concise responses" is worth remembering forever; but a resolved error message from three days ago has no value today.
Before Compression: Identify the Scenario
Before starting compression, determine which conversation scenario you're in, as retention strategies differ:
| Scenario | Characteristics | Compression Tendency |
|---|
| Task-oriented | Clear goal, step-driven | Keep goal and incomplete steps, compress process details |
| Chat-oriented |
Open topics, no clear task | Keep only user preference signals, discard aggressively |
|
Research-oriented | Gathering information, continuous accumulation | Keep conclusions and sources, compress procedural discussions |
|
Group-chat | Multiple people, high noise | Discard aggressively, keep only directly relevant content |
Three-Layer Compression Strategy
Layer 1: Must Preserve (Keep Original)
These contents remain in original or near-original form after compression:
- - User's explicitly stated goals, requirements, deadlines
- Incomplete tasks (in-progress, interrupted)
- Confirmed important decisions ("We decided to go with Plan B")
- User-expressed explicit preferences ("I don't like X", "Always use Y format going forward")
- Key credentials or config (accounts, paths, special settings)
Layer 2: Compress to Summary (Extract and Keep)
Keep conclusions, discard process:
- - Completed tasks → One-sentence conclusion ("Completed X, result was Y")
- Long explanations → Core point in 1-2 sentences
- Tool call outputs → Keep only final results, discard intermediate steps
- Repeated topics → Merge into one record
Layer 3: Discard Directly
- - Small talk, thanks, acknowledgment messages ("ok", "thanks", "got it")
- Rejected or obsolete proposals
- Multiple attempts at the same question (keep only the final effective one)
- Pure transitional content ("let me think", "hold on")
- Resolved error messages that won't be needed again
Compression Execution Steps
Step 1: Scan All Conversation
Identify all Layer 1 content, make a checklist — this cannot be discarded.
Step 2: Process Layer 2
For each conversation segment, judge: Is there a conclusion worth keeping? If yes, distill into one sentence.
Step 3: Generate Compressed Summary
In chronological order, combine Layer 1 content + Layer 2 extractions into a compact context summary, typically no more than 600 characters.
Step 4: Update Memory File
Write high-persistence-value information (user preferences, long-term goals, important decisions) to the memory file. See memory-template.md for format.
Step 5: Inform the User
Briefly explain what was compressed and what key information was preserved, so the user knows the context has been updated.
OpenClaw Multi-Channel Supplementary Rules
OpenClaw runs across multiple messaging platforms, pay extra attention:
Group chat scenarios: Other members' messages default to Layer 3 (discard) unless the user explicitly responds to or quotes that message.
Cross-day conversations: Judge by topic unit, not time unit. An unfinished task from yesterday belongs to Layer 1; a completed topic from yesterday drops one level today.
Channel switching: If the user asks similar questions on different channels (WhatsApp vs Telegram), it indicates genuine concern — promote priority to Layer 1.
Optional: Auto-Trigger (Hook Configuration)
If you use Claude Code or an agent that supports PreCompact hooks, you can configure auto-trigger. See setup-hook.md for details.
Without hook configuration, you can trigger manually: just tell the agent "compress my context" or "context is getting long, clean it up".
技能名称:context-compression
详细描述:
来源:该技能提取自Claude Code的内部实现和规则。Claude Code在~/.claude/目录中公开暴露其安全机制(钩子、系统提示、技能定义)。分层压缩策略、层级分类系统和记忆文件结构均通过逆向分析Claude Code的PreCompact/PostCompact钩子和会话记忆处理得出,随后针对OpenClaw的多通道环境进行了适配。
上下文压缩
为何需要分层压缩
一刀切式的上下文丢弃方法要么让智能体失去记忆,要么过快消耗上下文。分层策略的核心原则是:不同信息具有不同的生命周期。用户说我更喜欢简洁的回答这句话值得永远记住;但三天前一个已解决的错误信息今天毫无价值。
压缩前:识别场景
开始压缩前,先判断当前处于哪种对话场景,因为保留策略各不相同:
| 场景 | 特征 | 压缩倾向 |
|---|
| 任务导向型 | 目标明确,按步骤推进 | 保留目标和未完成步骤,压缩过程细节 |
| 聊天导向型 |
话题开放,无明确任务 | 仅保留用户偏好信号,大胆丢弃 |
|
研究导向型 | 收集信息,持续积累 | 保留结论和来源,压缩过程性讨论 |
|
群聊型 | 多人参与,噪音高 | 大胆丢弃,仅保留直接相关内容 |
三层压缩策略
第一层:必须保留(保持原样)
以下内容在压缩后保持原始或接近原始的形式:
- - 用户明确陈述的目标、需求、截止日期
- 未完成的任务(进行中、被打断的)
- 已确认的重要决策(我们决定采用方案B)
- 用户表达的明确偏好(我不喜欢X、以后始终使用Y格式)
- 关键凭证或配置(账户、路径、特殊设置)
第二层:压缩为摘要(提取并保留)
保留结论,丢弃过程:
- - 已完成的任务 → 一句话结论(已完成X,结果是Y)
- 冗长的解释 → 用1-2句话概括核心要点
- 工具调用输出 → 仅保留最终结果,丢弃中间步骤
- 重复的话题 → 合并为一条记录
第三层:直接丢弃
- - 闲聊、感谢、确认消息(好的、谢谢、知道了)
- 被拒绝或过时的提议
- 同一问题的多次尝试(仅保留最终有效的那次)
- 纯过渡性内容(让我想想、稍等)
- 不再需要的已解决错误信息
压缩执行步骤
步骤1:扫描整个对话
识别所有第一层内容,制作检查清单——这些内容不可丢弃。
步骤2:处理第二层
对每个对话片段进行判断:是否有值得保留的结论?如果有,提炼为一句话。
步骤3:生成压缩摘要
按时间顺序,将第一层内容与第二层提取内容合并为一份紧凑的上下文摘要,通常不超过600个字符。
步骤4:更新记忆文件
将高持久价值的信息(用户偏好、长期目标、重要决策)写入记忆文件。格式参见memory-template.md。
步骤5:告知用户
简要说明压缩了哪些内容以及保留了哪些关键信息,让用户知道上下文已更新。
OpenClaw多通道补充规则
OpenClaw在多个消息平台上运行,需特别注意:
群聊场景:其他成员的消息默认归为第三层(丢弃),除非用户明确回复或引用该消息。
跨日对话:按话题单元而非时间单元判断。昨天未完成的任务属于第一层;昨天已完成的话题今天降一级。
通道切换:如果用户在不同通道(WhatsApp与Telegram)上提出类似问题,表明是真实关切——提升优先级至第一层。
可选:自动触发(钩子配置)
如果你使用支持PreCompact钩子的Claude Code或智能体,可以配置自动触发。详情参见setup-hook.md。
未配置钩子时,可手动触发:只需告诉智能体压缩我的上下文或上下文太长了,清理一下。