context-compactor
Token-based context compaction for local models (MLX, llama.cpp, Ollama) that don't report context limits.
- License 许可
- MIT
- 最近更新
- 2026-06-07
- Stars
- ★ 0
- Forks
- 1
01 前言 Front Matter
- name
- context-compactor
- version
- 0.3.8
- description
- Token-based context compaction for local models (MLX, llama.cpp, Ollama) that don't report context limits.
03 SKILL.md 正文
Context Compactor
Automatic context compaction for OpenClaw when using local models that don't properly report token limits or context overflow errors.
The Problem
Cloud APIs (Anthropic, OpenAI) report context overflow errors, allowing OpenClaw's built-in compaction to trigger. Local models (MLX, llama.cpp, Ollama) often:
- Silently truncate context
- Return garbage when context is exceeded
- Don't report accurate token counts
This leaves you with broken conversations when context gets too long.
The Solution
Context Compactor estimates tokens client-side and proactively summarizes older messages before hitting the model's limit.
How It Works
┌─────────────────────────────────────────────────────────────┐
│ 1. Message arrives │
│ 2. before_agent_start hook fires │
│ 3. Plugin estimates total context tokens │
│ 4. If over maxTokens: │
│ a. Split into "old" and "recent" messages │
│ b. Summarize old messages (LLM or fallback) │
│ c. Inject summary as compacted context │
│ 5. Agent sees: summary + recent + new message │
└─────────────────────────────────────────────────────────────┘
Installation
# One command setup (recommended)
npx jasper-context-compactor setup
# Restart gateway
openclaw gateway restart
The setup command automatically:
- Copies plugin files to
~/.openclaw/extensions/context-compactor/ - Adds plugin config to
openclaw.jsonwith sensible defaults
Configuration
Add to openclaw.json:
{
"plugins": {
"entries": {
"context-compactor": {
"enabled": true,
"config": {
"maxTokens": 8000,
"keepRecentTokens": 2000,
"summaryMaxTokens": 1000,
"charsPerToken": 4
}
}
}
}
}
Options
| Option | Default | Description |
|---|---|---|
enabled |
true |
Enable/disable the plugin |
maxTokens |
8000 |
Max context tokens before compaction |
keepRecentTokens |
2000 |
Tokens to preserve from recent messages |
summaryMaxTokens |
1000 |
Max tokens for the summary |
charsPerToken |
4 |
Token estimation ratio |
summaryModel |
(session model) | Model to use for summarization |
Tuning for Your Model
MLX (8K context models):
{
"maxTokens": 6000,
"keepRecentTokens": 1500,
"charsPerToken": 4
}
Larger context (32K models):
{
"maxTokens": 28000,
"keepRecentTokens": 4000,
"charsPerToken": 4
}
Small context (4K models):
{
"maxTokens": 3000,
"keepRecentTokens": 800,
"charsPerToken": 4
}
Commands
/compact-now
Force clear the summary cache and trigger fresh compaction on next message.
/compact-now
/context-stats
Show current context token usage and whether compaction would trigger.
/context-stats
Output:
📊 Context Stats
Messages: 47 total
- User: 23
- Assistant: 24
- System: 0
Estimated Tokens: ~6,234
Limit: 8,000
Usage: 77.9%
✅ Within limits
How Summarization Works
When compaction triggers:
- Split messages into "old" (to summarize) and "recent" (to keep)
- Generate summary using the session model (or configured
summaryModel) - Cache the summary to avoid regenerating for the same content
- Inject context with the summary prepended
If the LLM runtime isn't available (e.g., during startup), a fallback truncation-based summary is used.
Differences from Built-in Compaction
| Feature | Built-in | Context Compactor |
|---|---|---|
| Trigger | Model reports overflow | Token estimate threshold |
| Works with local models | ❌ (need overflow error) | ✅ |
| Persists to transcript | ✅ | ❌ (session-only) |
| Summarization | Pi runtime | Plugin LLM call |
Context Compactor is complementary — it catches cases before they hit the model's hard limit.
Troubleshooting
Summary quality is poor:
- Try a better
summaryModel - Increase
summaryMaxTokens - The fallback truncation is used if LLM runtime isn't available
Compaction triggers too often:
- Increase
maxTokens - Decrease
keepRecentTokens(keeps less, summarizes earlier)
Not compacting when expected:
- Check
/context-statsto see current usage - Verify
enabled: truein config - Check logs for
[context-compactor]messages
Characters per token wrong:
- Default of 4 works for English
- Try 3 for CJK languages
- Try 5 for highly technical content
Logs
Enable debug logging:
{
"plugins": {
"entries": {
"context-compactor": {
"config": {
"logLevel": "debug"
}
}
}
}
}
Look for:
[context-compactor] Current context: ~XXXX tokens[context-compactor] Compacted X messages → summary
Links
04 同仓库更多 Skills
crowd-prompting
A marketplace where AI agents improve prompts, system instructions, tool descriptions, and other text-based content with domain expertise from real-world operations — and earn tokens for valuable contributions.
ceaser
Interact with the Ceaser privacy protocol on Base L2 using the ceaser-mcp MCP tools. This skill uses the ceaser-mcp npm package for ALL operations -- shield, unshield, note management, and protocol queries. All ceaser tool calls use CLI subcommands (npx -y ceaser-mcp <subcommand>). Ceaser is a privacy-preserving ETH wrapper using Noir/UltraHonk zero-knowledge proofs on Base L2 (chain ID 8453).
ceaser-send
Fully automated private ETH transfer via Ceaser Protocol on Base L2 using the ceaser-mcp MCP tools. This skill uses the ceaser-mcp npm package for ALL operations -- all ceaser tool calls use CLI subcommands (npx -y ceaser-mcp <subcommand>). WARNING -- creates observable on-chain links between user wallet and hot wallet (see Privacy Warning). Generates an ephemeral hot wallet with BIP-39 mnemonic recovery, user funds it, agent signs and broadcasts the Shield TX automatically. Exactly one manual step required (funding the hot wallet with ETH). Uses Noir/UltraHonk zero-knowledge proofs.
dilemm-ai
Compete in DilemmAI, the prisoner's dilemma AI arena at dilemm.ai. Use when an OpenClaw agent wants to create an account, design and submit strategy prompts for their bot, enter matchmaking, analyze historical matches and leaderboards, and iterate on bot strategy. Requires headless browser automation (no API available yet).
moldium
Post and manage content on the Moldium blog platform. Triggered by "post to Moldium", "write a blog post", "publish an article", etc.
arxiv-paper-reviews
Interact with arXiv Crawler API to fetch papers, read reviews, submit comments, search papers, and import papers. Use when working with arXiv papers, fetching paper lists by date/category/interest, viewing paper details with comments, submitting paper reviews, searching papers by title, or importing papers from arXiv URLs via API at http://weakaccept.top:8000/.