context-optimizer

上下文窗口优化器 - 原创技能。智能管理AI上下文窗口,自动压缩历史、去除冗余、保留关键信息,节省token提升效率。适用于长会话、多文件、大项目等场景。

Safety Notice

This listing is from the official public ClawHub registry. Review SKILL.md and referenced scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "context-optimizer" with this command: npx skills add 534422530/laosi-context-opt

⚠️ 发布规则

所有发布到ClawHub的技能必须严格测试,确定没有问题再发布


技能测试验证清单

  • frontmatter格式正确
  • 优化策略完整
  • 优先级明确
  • 压缩算法清晰
  • 无语法错误

Context Window Optimizer - 上下文窗口优化器

原创技能 | 激活词: 优化上下文 / 精简历史 / 节省token

核心问题

AI会话中的上下文会不断增长:

  • 对话历史越来越长
  • token消耗越来越多
  • 早期信息被稀释
  • 响应质量下降

优化策略

策略1: 关键信息提取

原始对话 (500 tokens):
用户: "我想用React写一个按钮组件"
AI: 好的,我来创建...
用户: "按钮需要支持loading状态"
AI: 好的,添加loading...
用户: "还要支持disabled状态"
AI: 好的,添加disabled...
[... 继续50轮...]

优化后 (50 tokens):
用户意图: 创建React按钮组件
关键需求: loading状态, disabled状态
当前进度: 组件已创建
待办: 添加loading/disabled支持

策略2: 语义压缩

def compress_context(messages: list[Message]) -> list[Message]:
    compressed = []
    
    for msg in messages:
        # 保留决策点
        if is_decision_point(msg):
            compressed.append(msg)
        
        # 保留结果/输出
        elif is_result(msg):
            compressed.append(msg)
        
        # 压缩重复过程
        elif is_redundant(msg):
            summary = summarize(msg)
            compressed.append(summary)
        
        # 丢弃中间步骤
        else:
            pass  # 不保留
    
    return compressed

策略3: 分层保留

层级内容保留策略
L1 核心决策、结论、配置永远保留
L2 重要当前任务相关信息保留最近
L3 一般过程、对话可压缩
L4 垃圾重复、废话丢弃

策略4: 时间衰减

def time_decay(messages: list[Message], current_time: datetime) -> list[Message]:
    for msg in messages:
        age = current_time - msg.timestamp
        
        if age < 1 hour:
            weight = 1.0
        elif age < 1 day:
            weight = 0.8
        elif age < 1 week:
            weight = 0.5
        else:
            weight = 0.2
        
        msg.priority *= weight
    
    return filter_by_priority(messages)

优化触发条件

TRIGGERS = {
    'token_threshold': 50_000,      # token超过此值
    'message_threshold': 100,       # 消息超过100条
    'repetition_rate': 0.3,         # 重复率超过30%
    'quality_drop': 0.2,            # 质量下降20%
}

优化流程

1. 监控 → 跟踪token和消息数量
      ↓
2. 判断 → 是否达到触发条件
      ↓
3. 分析 → 识别各层级内容
      ↓
4. 压缩 → 执行压缩策略
      ↓
5. 验证 → 确保关键信息不丢失
      ↓
6. 替换 → 用压缩版本替换原内容

输出格式

## 上下文优化报告

### 优化前
- Token: 52,341
- 消息数: 127条
- 质量评分: 0.65

### 优化后
- Token: 18,234 (减少 65%)
- 消息数: 31条
- 质量评分: 0.92

### 保留内容
✅ 核心决策: 3条
✅ 关键配置: 2条
✅ 当前任务状态: 完整
✅ 用户偏好: 保留

### 丢弃内容
❌ 中间过程: 89条
❌ 重复对话: 7条
❌ 废话消息: 4条

### 优化建议
1. 建议定期优化,每50条消息触发一次
2. 重要信息标记为L1可防止被优化

优先级队列

必须保留 (L1)

L1_MUST_KEEP = [
    "用户偏好设置",
    "项目配置信息",
    "核心架构决策",
    "已完成的重要工作",
    "待办事项清单",
]

尽量保留 (L2)

L2_TRY_KEEP = [
    "当前任务进度",
    "最近10条有效对话",
    "关键文件路径",
    "错误解决方案",
]

可优化 (L3)

L3_COMPRESSIBLE = [
    "中间调试过程",
    "重复的确认对话",
    "详细的日志输出",
    "试错过程",
]

丢弃 (L4)

L4_DISCARD = [
    "无意义的闲聊",
    "重复的错误尝试",
    "过期的临时信息",
    "明显的废话",
]

集成建议

配合其他技能使用:

配合技能效果
entropy-manager熵值高时触发优化
context-memory-ball优化后的上下文保存为球
mempalace-assistant重要信息存入记忆库

性能指标

指标优化前优化后
Token使用52K18K
响应质量65%92%
上下文清晰度
信息密度

原创性声明

本技能为原创,融合了:

  • 文本压缩算法
  • 信息检索优先级
  • 时间衰减模型
  • 分层缓存策略

作者: laosi 创建日期: 2026-04-28

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

Memory Organizer

Organize, compress, and curate OpenClaw memory without polluting permanent memory. Use when the user wants to compress memory files, clean dated memory notes...

Registry SourceRecently Updated
7892Profile unavailable
General

Context Brief

Persistent context survival for OpenClaw. Writes file-based anchors to memory/anchors/ to preserve critical context across sessions. Reads MEMORY.md and dail...

Registry SourceRecently Updated
2670Profile unavailable
Research

Memory Optimization

Comprehensive memory management optimization for AI agents. Use when: (1) Agent experiences context compression amnesia, (2) Need to rebuild context quickly...

Registry SourceRecently Updated
3280Profile unavailable
Automation

Proactive Memory Agent

Ultimate memory optimization for AI agents. Combines WAL protocol, Working Buffer, three-tier memory (HOT/WARM/COLD), context budgeting (10/40/20/20), and .l...

Registry Source
1380Profile unavailable