← 返回 Skills 市场
534422530

上下文窗口优化器

作者 534422530 · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ 安全检测通过
40
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install laosi-context-opt
功能描述
上下文窗口优化器 - 原创技能。智能管理AI上下文窗口,自动压缩历史、去除冗余、保留关键信息,节省token提升效率。适用于长会话、多文件、大项目等场景。
使用说明 (SKILL.md)

⚠️ 发布规则

所有发布到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 \x3C 1 hour:
            weight = 1.0
        elif age \x3C 1 day:
            weight = 0.8
        elif age \x3C 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使用 52K 18K
响应质量 65% 92%
上下文清晰度
信息密度

原创性声明

本技能为原创,融合了:

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

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

安全使用建议
This skill is internally consistent and does not request secrets or installs. Before enabling it in a production agent, test it on non-critical conversations so you can verify the pruning behavior and ensure no important data is lost. Mark any irreplacable data (credentials, legal text, critical decisions) as L1/‘must keep’ so the optimizer won't remove it. Also review interactions with other skills or memory stores you use (the SKILL.md suggests integrations) — those other skills may have their own permissions and risks. Finally, because this is instruction-only, the actual effect depends on the agent runtime implementing these instructions correctly; prefer running it in a controlled environment first.
功能分析
Type: OpenClaw Skill Name: laosi-context-opt Version: 1.0.0 The 'context-optimizer' skill is a documentation-based bundle providing instructions and pseudo-code for an AI agent to manage and compress its conversation history. It contains no executable code, external dependencies, or network requests, and its instructions (SKILL.md) are strictly focused on token efficiency and information retention without any signs of malicious intent or prompt injection.
能力评估
Purpose & Capability
Name/description (context optimization) align with the SKILL.md content: algorithms, triggers, and retention policies. There are no unrelated required binaries, credentials, or config paths.
Instruction Scope
SKILL.md contains pseudocode and policies for compressing chat history, monitoring tokens/messages, and replacing history with compressed summaries. It does not instruct the agent to read arbitrary system files, access secrets, or transmit data to external endpoints. The only potentially impactful action is 'replace original content' (i.e., pruning history), which is coherent with the stated purpose.
Install Mechanism
Instruction-only skill with no install spec and no code files; nothing is written to disk or downloaded during install. Lowest-risk install profile.
Credentials
No environment variables, credentials, or config paths are requested. The SKILL.md does not reference hidden env vars or external tokens. Required access is proportional to purpose.
Persistence & Privilege
Skill is not always-enabled and does not request system-wide modifications. It can be invoked autonomously by the agent (default behavior), which is expected for skills of this type and is not combined with other red flags here.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install laosi-context-opt
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /laosi-context-opt 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
原创技能 - 智能压缩上下文节省token
元数据
Slug laosi-context-opt
版本 1.0.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

上下文窗口优化器 是什么?

上下文窗口优化器 - 原创技能。智能管理AI上下文窗口,自动压缩历史、去除冗余、保留关键信息,节省token提升效率。适用于长会话、多文件、大项目等场景。 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 40 次。

如何安装 上下文窗口优化器?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install laosi-context-opt」即可一键安装,无需额外配置。

上下文窗口优化器 是免费的吗?

是的,上下文窗口优化器 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

上下文窗口优化器 支持哪些平台?

上下文窗口优化器 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 上下文窗口优化器?

由 534422530(@534422530)开发并维护,当前版本 v1.0.0。

💬 留言讨论