Context Manager Skill
AI-powered context management for OpenClaw sessions. Uses the agent itself to generate intelligent summaries, then resets the session with compressed context.
Quick Start
CODEBLOCK0
When to Use
- - Context usage approaching 70-80%+
- Long sessions with extensive conversation history
- Before the session becomes slow or loses coherence
- Proactively to maintain fast, focused sessions
How It Works
- 1. AI Summarization: Sends a prompt to the agent asking it to summarize its own context
- Backup: Saves the original JSONL session file to INLINECODE0
- Reset: Deletes the JSONL file (official reset method)
- Inject: Sends the AI-generated summary as the first message in the fresh session
- Result: Same session key, new session ID, compressed context
Key insight: The agent has full visibility into its own context, so it generates the best possible summary.
Commands
Session Commands
| Command | Description |
|---|
| INLINECODE1 | List all sessions with token usage |
| INLINECODE2 |
Show detailed status for a session |
|
summarize [KEY] | Generate AI summary (read-only) |
|
summarize [KEY] --replace | Summarize AND reset session with compressed context |
|
compress [KEY] | Legacy grep-based extraction (not recommended) |
|
check [KEY] | Check if session exceeds threshold |
|
check-all | Check all sessions at once |
Configuration Commands
| Command | Description |
|---|
| INLINECODE8 | Set compression threshold (50-99%, default: 80) |
| INLINECODE9 |
Set depth: brief/balanced/comprehensive |
|
set-quiet-hours HH | Set quiet hours (e.g., "23:00-07:00") |
|
help | Show help and usage examples |
Examples
List All Sessions
CODEBLOCK1
Check Session Status
CODEBLOCK2
Generate AI Summary (Safe, Read-Only)
CODEBLOCK3
Full Compression (Summarize + Reset + Inject)
CODEBLOCK4
Result: 70k tokens → 16k tokens (77% reduction)
Output Files
When compression occurs, these files are created in memory/compressed/:
| File | Description |
|---|
| INLINECODE13 | AI-generated session summary |
| INLINECODE14 |
Full backup of original session (can restore if needed) |
|
{timestamp}.transcript.md | Raw transcript extraction (legacy) |
|
{timestamp}.summary.md | Grep-based summary (legacy) |
Requirements
- - openclaw - Gateway must be running
- jq - JSON parsing (
brew install jq) - Gateway access - Script uses
openclaw agent and INLINECODE19
Technical Details
Session Reset Method
The script uses JSONL deletion to reset sessions (official method):
- 1. Backup JSONL to INLINECODE20
- Delete INLINECODE21
- Send compressed context via INLINECODE22
- New session is created automatically with summary as first message
Why Not /reset?
The /reset slash command only works in the chat interface. When sent via openclaw agent --session-id, it's treated as a regular message and the agent tries to interpret it as a task.
AI Summarization Prompt
The script asks the agent to provide:
- 1. What was accomplished (key tasks)
- Key decisions made (with rationale)
- Current state (where we left off)
- Pending tasks (what still needs doing)
- Important context (critical info to remember)
Troubleshooting
Summary Text Empty
If the AI summary extraction fails, check stderr redirect:
CODEBLOCK5
Session Not Resetting
Verify the JSONL file path:
CODEBLOCK6
Restore From Backup
If something goes wrong:
CODEBLOCK7
Check Logs
Use openclaw logs to troubleshoot:
CODEBLOCK8
Best Practices
- 1. Backup first: The script auto-backs up, but you can also manually backup before testing
- Test on non-critical sessions first: Try on a Slack channel or cron session before main
- Check the summary: Run
summarize without --replace first to verify the summary quality - Monitor token count: Use
status to verify compression worked
See Also
- - INLINECODE29
- INLINECODE30
上下文管理器技能
基于AI的OpenClaw会话上下文管理。利用智能体自身生成智能摘要,然后通过压缩上下文重置会话。
快速开始
bash
列出所有会话及使用统计
~/openclaw/skills/context-manager/compress.sh list
检查特定会话状态
~/openclaw/skills/context-manager/compress.sh status agent:main:main
生成AI摘要(只读,安全)
~/openclaw/skills/context-manager/compress.sh summarize agent:main:main
压缩会话:生成摘要、重置、注入(破坏性操作)
~/openclaw/skills/context-manager/compress.sh summarize agent:main:main --replace
使用时机
- - 上下文使用率接近70-80%以上
- 长时间会话且对话历史丰富
- 在会话变慢或失去连贯性之前
- 主动维护快速、聚焦的会话
工作原理
- 1. AI摘要生成:向智能体发送提示,要求其总结自身上下文
- 备份:将原始JSONL会话文件保存至memory/compressed/
- 重置:删除JSONL文件(官方重置方法)
- 注入:将AI生成的摘要作为新会话的第一条消息发送
- 结果:相同的会话键,新的会话ID,压缩后的上下文
关键洞察:智能体对其自身上下文有完全可见性,因此能生成最佳摘要。
命令
会话命令
| 命令 | 描述 |
|---|
| list | 列出所有会话及令牌使用量 |
| status [KEY] |
显示会话的详细状态 |
| summarize [KEY] | 生成AI摘要(只读) |
| summarize [KEY] --replace | 生成摘要并重置会话,注入压缩上下文 |
| compress [KEY] | 基于grep的旧式提取(不推荐) |
| check [KEY] | 检查会话是否超过阈值 |
| check-all | 一次性检查所有会话 |
配置命令
| 命令 | 描述 |
|---|
| set-threshold N | 设置压缩阈值(50-99%,默认:80) |
| set-depth LEVEL |
设置深度:简要/平衡/全面 |
| set-quiet-hours HH | 设置静默时段(例如:23:00-07:00) |
| help | 显示帮助和使用示例 |
示例
列出所有会话
bash
$ ~/openclaw/skills/context-manager/compress.sh list
📋 可用会话(共4个)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
会话键 类型 令牌数 使用率
1 agent:main:main 直接 70188 70%
2 agent:main:slack:channel:c0aaruq2en9 群组 20854 20%
3 agent:main:cron:0d02af4b-... 直接 18718 18%
检查会话状态
bash
$ ~/openclaw/skills/context-manager/compress.sh status agent:main:main
📊 上下文管理器状态
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
会话键:agent:main:main
会话ID:fc192a2d-091c-48c7-9fad-12bf34687454
类型: 直接
模型: gemini-3-flash
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
阈值: 80%
令牌数:70188 / 100000
使用率:70%
生成AI摘要(安全,只读)
bash
$ ~/openclaw/skills/context-manager/compress.sh summarize agent:main:main
🧠 正在请求会话的AI摘要:agent:main:main
会话ID:fc192a2d-091c-48c7-9fad-12bf34687454
✅ AI摘要已生成!
保存至:memory/compressed/20260127-123146.ai-summary.md
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
会话摘要:2026年1月27日
1. 已完成事项
...
完整压缩(摘要 + 重置 + 注入)
bash
$ ~/openclaw/skills/context-manager/compress.sh summarize agent:main:main --replace
🧠 正在请求会话的AI摘要:agent:main:main
会话ID:fc192a2d-091c-48c7-9fad-12bf34687454
模式:替换(生成摘要后将重置会话)
✅ AI摘要已生成!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[显示AI生成的摘要]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔄 正在重置会话并注入压缩上下文...
正在备份会话文件...
备份已保存:memory/compressed/20260127-123146.session-backup.jsonl
正在删除会话JSONL以重置...
正在将压缩上下文注入新会话...
✅ 会话压缩成功!
旧会话ID:fc192a2d-091c-48c7-9fad-12bf34687454
新会话ID:a1b2c3d4-...
会话已准备好以压缩上下文继续
结果:70k令牌 → 16k令牌(减少77%)
输出文件
压缩发生时,会在memory/compressed/中创建以下文件:
| 文件 | 描述 |
|---|
| {时间戳}.ai-summary.md | AI生成的会话摘要 |
| {时间戳}.session-backup.jsonl |
原始会话的完整备份(需要时可恢复) |
| {时间戳}.transcript.md | 原始文本提取(旧式) |
| {时间戳}.summary.md | 基于grep的摘要(旧式) |
要求
- - openclaw - 网关必须运行中
- jq - JSON解析(brew install jq)
- 网关访问权限 - 脚本使用openclaw agent和openclaw sessions
技术细节
会话重置方法
脚本使用JSONL删除来重置会话(官方方法):
- 1. 将JSONL备份至memory/compressed/
- 删除~/.openclaw/agents/{agent}/sessions/{sessionId}.jsonl
- 通过openclaw agent --to main发送压缩上下文
- 自动创建新会话,摘要作为第一条消息
为什么不使用/reset?
/reset斜杠命令仅在聊天界面中有效。通过openclaw agent --session-id发送时,它被视为普通消息,智能体会尝试将其解释为任务。
AI摘要生成提示
脚本要求智能体提供:
- 1. 已完成事项(关键任务)
- 关键决策(附理由)
- 当前状态(上次进行到何处)
- 待办任务(仍需完成的事项)
- 重要上下文(需记住的关键信息)
故障排除
摘要文本为空
如果AI摘要提取失败,请检查stderr重定向:
bash
脚本使用2>/dev/null避免Node弃用警告破坏JSON
openclaw agent --session-id $ID -m ... --json 2>/dev/null
会话未重置
验证JSONL文件路径:
bash
ls ~/.openclaw/agents/main/sessions/
从备份恢复
如果出现问题:
bash
cp memory/compressed/{时间戳}.session-backup.jsonl \
~/.openclaw/agents/main/sessions/{会话ID}.jsonl
检查日志
使用openclaw logs进行故障排除:
bash
openclaw logs --limit 50 --json | grep -i error\|fail
最佳实践
- 1. 先备份:脚本会自动备份,但您也可以在测试前手动备份
- 先在非关键会话上测试:在主会话前先在Slack频道或cron会话上尝试
- 检查摘要:先运行不带--replace的summarize验证摘要质量
- 监控令牌数量:使用status验证压缩是否生效
##