← 返回
开发者工具 中文

Session Context Compressor

Compress OpenClaw session context to reduce token usage and extend session lifetime. Uses NLP summarization (Sumy) to intelligently compact conversation history while preserving essential context. Triggers on mentions of session compression, token reduction, context cleanup, or when session size exceeds safe thresholds (~300KB). Use when (1) OpenClaw approaches 50% context limit, (2) Sessions are slowing down due to large context, (3) Reducing API costs from excessive token consumption, (4) Exte
压缩 OpenClaw 会话上下文以减少 Token 消耗并延长会话时长。利用 NLP 摘要技术(Sumy)智能压缩对话历史,同时保留关键语境。当提及会话压缩、Token 缩减、上下文清理或会话大小超出安全阈值(约 300KB)时触发。适用于以下情况:(1) OpenClaw 上下文接近 50% 限制;(2) 大上下文导致会话运行缓慢;(3) 需降低因 Token 过量消耗产生的 API 成本;(4) 需延长会话寿命以避免强制重启。
beboxos
开发者工具 clawhub v1.0.0 1 版本 99772.9 Key: 无需
★ 0
Stars
📥 1,318
下载
💾 23
安装
1
版本
#compression#context#latest#optimization#session#tokens

概述

ClawPressor - Session Context Compressor

Intelligently compress OpenClaw session files to reduce token usage by 85-96%.

Author: JARVIS (AI Coder) | Managed by: BeBoX

License: MIT | Version: 1.0.0

Quick Start

# Preview compression without changes
python3 scripts/compress.py --dry-run

# Apply compression
python3 scripts/compress.py --apply

# Restore from backup
python3 scripts/compress.py --restore

When to Use

SituationAction
-------------------
Context at 30-40%Plan compression soon
Context at 50%URGENT — OpenClaw will force compact
Session > 300KBCompress to restore performance
Slow responsesLarge context likely the cause
High API costsCompress regularly to save tokens

How It Works

  1. Preserves recent context — Keeps last 5 messages intact for immediate context
  2. Summarizes old messages — Uses LexRank algorithm to extract key information
  3. Replaces with compact block — Single system message containing summary
  4. Creates backup — Original preserved as .backup file

Prerequisites

pip install sumy
python -c "import nltk; nltk.download('punkt_tab'); nltk.download('stopwords')"

Command Reference

# Find and compress latest session (dry-run)
python3 scripts/compress.py

# Compress specific session
python3 scripts/compress.py --session /path/to/session.jsonl --apply

# Keep more recent messages (default: 5)
python3 scripts/compress.py --keep 10 --apply

# Restore if something went wrong
python3 scripts/compress.py --restore

# View compression statistics
python3 scripts/compress.py --stats

Typical Results

MetricBeforeAfterGain
-----------------------------
Messages1686-96%
Size347 KB12 KB-96%
Context tokens~50k~8k-84%
Session duration~30 min~2-3h+400%

Integration with Workflows

In HEARTBEAT.md:

## Context Maintenance (1x/jour)
- Check session size: `ls -lh ~/.openclaw/agents/main/sessions/*.jsonl`
- If > 200KB: `python3 skills/clawpressor/scripts/compress.py --apply`

Manual check:

# See current session stats
ls -lh ~/.openclaw/agents/main/sessions/*.jsonl | head -1

Safety

  • Always creates .backup before compressing
  • --restore recovers original session
  • Recent messages always preserved intact
  • Summary stored as system message (visible to model)

Troubleshooting

IssueSolution
-----------------
"Sumy not installed"Run pip install sumy and NLTK downloads
No session foundCheck ~/.openclaw/agents/main/sessions/ exists
Backup not foundFile may have been overwritten; no recovery
Poor summariesIncrease --keep to preserve more context

Credits

  • Coding: JARVIS (AI Assistant)
  • Project Management: BeBoX
  • Technique: NLP summarization via Sumy (LexRank algorithm)

Related

  • See memory/openclaw-context-optimization.md for full strategy
  • Combine with SOUL_MIN/USER_MIN files for maximum efficiency

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-29 03:25 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 668 📥 324,156
developer-tools

Gog

steipete
Google Workspace 命令行工具,支持 Gmail、日历、云端硬盘、通讯录、表格和文档。
★ 921 📥 185,798
developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 68 📥 180,173