Context Optimizer
Automatic context management skill that monitors token usage and creates continuation sessions when needed.
Core Functionality
This skill provides:
- 1. Context Monitoring - Track token usage against 200k max
- Threshold Detection - Trigger at 95% (190k tokens)
- Information Extraction - Extract key decisions, important context, unfinished tasks
- Session Continuation - Create new session with summarized context
Usage
Manual Trigger
CODEBLOCK0
Automatic Monitoring
Add to your workflow or cron:
CODEBLOCK1
When context exceeds threshold, the skill will:
- 1. Analyze conversation history
- Extract key information (decisions, context, todos)
- Generate continuation prompt
- Create new session with extracted context
Extraction Priority
The optimizer extracts in this order:
- 1. Unfinished tasks - Incomplete actions, pending decisions
- Key decisions - Important choices and their rationale
- Critical context - Information essential to task completion
- User preferences - Explicitly stated preferences and requirements
- Recent progress - What has been accomplished so far
Output Format
When creating continuation, output:
CODEBLOCK2
Scripts
- -
context_optimizer.py - Main CLI for context monitoring and splitting - INLINECODE1 - Extracts key information from session history
Integration with OpenClaw
Automatic Split Logic
To automatically split sessions when context exceeds 95%, integrate with OpenClaw's heartbeat:
- 1. Add to HEARTBEAT.md:
CODEBLOCK3
- 2. Create Cron Job:
Use cron with
sessionTarget: "isolated" to run context checks independently.
Tool Integration
The skill uses these OpenClaw tools:
- -
session_status - Get current token/context usage - INLINECODE4 - Fetch conversation for extraction
- INLINECODE5 - Create continuation session
- INLINECODE6 - Schedule periodic checks
上下文优化器
自动上下文管理技能,可监控令牌使用情况并在需要时创建延续会话。
核心功能
本技能提供:
- 1. 上下文监控 - 跟踪令牌使用情况,上限为20万
- 阈值检测 - 在95%(19万令牌)时触发
- 信息提取 - 提取关键决策、重要上下文、未完成任务
- 会话延续 - 使用摘要上下文创建新会话
使用方法
手动触发
bash
检查当前上下文使用情况
context-optimizer check
强制提取并创建延续
context-optimizer split --threshold 95
预览将要提取的内容
context-optimizer preview
自动监控
添加到您的工作流程或定时任务:
每10分钟:运行 context-optimizer check
当上下文超过阈值时,技能将:
- 1. 分析对话历史
- 提取关键信息(决策、上下文、待办事项)
- 生成延续提示
- 使用提取的上下文创建新会话
提取优先级
优化器按以下顺序提取:
- 1. 未完成任务 - 未完成的操作、待定决策
- 关键决策 - 重要选择及其理由
- 关键上下文 - 完成任务所必需的信息
- 用户偏好 - 明确陈述的偏好和要求
- 近期进展 - 目前已完成的成果
输出格式
创建延续时,输出:
markdown
会话延续
已完成
进行中
关键上下文
后续步骤
延续提示
[在新会话中使用的提示]
脚本
- - contextoptimizer.py - 用于上下文监控和分割的主命令行工具
- sessionextractor.py - 从会话历史中提取关键信息
与OpenClaw的集成
自动分割逻辑
要在上下文超过95%时自动分割会话,请集成到OpenClaw的心跳机制:
- 1. 添加到HEARTBEAT.md:
markdown
上下文监控器
每次心跳:检查会话状态
如果上下文 > 85%:记录警告
如果上下文 > 95%:运行 context-optimizer split
- 2. 创建定时任务:
使用带有 sessionTarget: isolated 的定时任务独立运行上下文检查。
工具集成
本技能使用以下OpenClaw工具:
- - sessionstatus - 获取当前令牌/上下文使用情况
- sessionshistory - 获取对话以进行提取
- sessions_spawn - 创建延续会话
- cron - 安排定期检查