← Back to Skills Marketplace
534422530

上下文窗口优化器

by 534422530 · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ Security Clean
40
Downloads
0
Stars
0
Active Installs
1
Versions
Install in OpenClaw
/install laosi-context-opt
Description
上下文窗口优化器 - 原创技能。智能管理AI上下文窗口,自动压缩历史、去除冗余、保留关键信息,节省token提升效率。适用于长会话、多文件、大项目等场景。
README (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

Usage Guidance
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.
Capability Analysis
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.
Capability Assessment
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.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install laosi-context-opt
  3. After installation, invoke the skill by name or use /laosi-context-opt
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.0
原创技能 - 智能压缩上下文节省token
Metadata
Slug laosi-context-opt
Version 1.0.0
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 1
Frequently Asked Questions

What is 上下文窗口优化器?

上下文窗口优化器 - 原创技能。智能管理AI上下文窗口,自动压缩历史、去除冗余、保留关键信息,节省token提升效率。适用于长会话、多文件、大项目等场景。 It is an AI Agent Skill for Claude Code / OpenClaw, with 40 downloads so far.

How do I install 上下文窗口优化器?

Run "/install laosi-context-opt" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is 上下文窗口优化器 free?

Yes, 上下文窗口优化器 is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does 上下文窗口优化器 support?

上下文窗口优化器 is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created 上下文窗口优化器?

It is built and maintained by 534422530 (@534422530); the current version is v1.0.0.

💬 Comments