memory-compression
Skill9 starsChanged 5 months agoMITView on GitHub
Production memory management for Energy agents. Prevents context loss, enables compound memory. Plain Markdown beats vector databases: 74% accuracy (ClaWHow) vs 68.5% (vector DBs). Structure IS the retrieval mechanism. OpenClaw's formula: threshold = contextWindow - reserveFloor - softThreshold Each compaction MERGES into this anchor rather than starting fresh. Factory tested this on 36,000 sessions: 4.04 accuracy vs 3.74 for alternatives. Three-stage pipeline from arXiv 2601.02553: Results: ~17K → ~550 tokens per query (97% compression). F1: 43.24%. From OpenClaw's production implementation:
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 219
Always preserve: maintainer's recent messages, active task, file paths, decisions, errors, API status, sprint progress.
- Distinctive: line 220
Always drop: tool outputs, search results, debugging steps, file contents (keep path+line only).
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Skill: Memory Compression & Management
Production memory management for Energy agents. Prevents context loss, enables compound memory.
Three-Layer Memory Architecture
agent/
├── SOUL.md # Static identity — NEVER modified by agent. Human-authored.
├── MEMORY.md # Curated long-term — capped at ~500 lines. Agent-maintained.
├── LEARNINGS.md # Append-only patterns and mistakes.
└── memory/
├── daily/
│ ├── 2026-03-01.md # Structured daily journals
│ └── 2026-03-03.md # Last 7 days active, older archived
└── backups/
└── pre-compact-*.md # Auto-generated before compactionPlain Markdown beats vector databases: 74% accuracy (ClaWHow) vs 68.5% (vector DBs). Structure IS the retrieval mechanism.
Pre-Compaction Flush (The Critical Hook)
When It Triggers
OpenClaw's formula: threshold = contextWindow - reserveFloor - softThreshold
- On 200K window: 200K - 20K - 4K = 176K tokens
- One flush per compaction cycle (prevents double-flush)
What Gets Saved (Anchor State Pattern)
## Anchor State
### Intent
[What we're building right now, one sentence]
### Changes Made
- [file]: [what changed and why]
### Decisions Taken
- [decision]: [rationale] — alternatives: [X, Y]
### Next Steps (ordered)
1. [Most important next action]
2. [Second priority]Each compaction MERGES into this anchor rather than starting fresh. Factory tested this on 36,000 sessions: 4.04 accuracy vs 3.74 for alternatives.
What to KEEP (Always Survives)
- Architecture decisions with rationale
- User preferences and constraints
- Error patterns and their fixes
- File paths and their purposes
- Key function signatures, API endpoints
- The current anchor state
What to SUMMARIZE (Compress)
- Multi-turn debugging → "Fixed X by changing Y in file Z"
- Research exploration → "Compared A vs B vs C; chose B because [reason]"
- Tool output → strip to relevant lines only
- Code review conversations → keep only final decision
What to DISCARD
- Confirmations ("sounds good", "yes", "ok")
- Repeated failed attempts (keep only the fix)
- Full file contents that were read (keep path + line refs)
- Verbose tool output (keep summary)
SimpleMem Compression (30x, Production-Proven)
Three-stage pipeline from arXiv 2601.02553:
Stage 1: Entropy-Aware Filtering
- Divide conversation into overlapping windows (10 turns, stride 5)
- Score:
entropy = entity_novelty * new_entities + semantic_divergence * cosine_distance - Windows below 0.35 threshold discarded (chit-chat, confirmations)
- Coreference resolution: pronouns → names
- Temporal normalization: "next Friday" → "2026-03-07"
Stage 2: Recursive Consolidation
- Cluster related memories:
affinity = semantic_sim * 0.6 + temporal_proximity * 0.4 - Clusters above 0.85 → synthesize into abstract representations
- Fine-grained entries archived (retrievable, not in default retrieval)
Stage 3: Intent-Aware Retrieval
- Simple queries → retrieve 3 entries
- Multi-hop queries → expand to 20 entries
- Hybrid scoring:
score = 0.5 * semantic + 0.3 * BM25 + 0.2 * symbolic
Results: ~17K → ~550 tokens per query (97% compression). F1: 43.24%.
Hybrid Search (BM25 + Vectors)
From OpenClaw's production implementation:
function mergeHybridResults(params: {
vector: VectorResult[];
keyword: KeywordResult[];
vectorWeight: number; // 0.7
textWeight: number; // 0.3
}): ScoredResult[] {
const byId = new Map();
for (const r of params.vector)
byId.set(r.id, { ...r, vectorScore: r.vectorScore, textScore: 0 });
for (const r of params.keyword) {
const existing = byId.get(r.id);
if (existing) existing.textScore = r.textScore;
else byId.set(r.id, { ...r, vectorScore: 0, textScore: r.textScore });
}
return Array.from(byId.values())
.map((e) => ({
...e,
score:
params.vectorWeight * e.vectorScore + params.textWeight * e.textScore,
}))
.sort((a, b) => b.score - a.score);
}Chunking: 400 tokens/chunk, 80 tokens overlap. Over-fetch 4x before merging. Embedding fallback: local ggml → OpenAI text-embedding-3-small → Gemini → BM25-only.
Session Handoff Protocol
At session end, write to daily log:
# Session Handoff — {date} {time}
## Anchor State
### Intent
[one sentence]
### Changes Made
- [file]: [change]
### Decisions Made
- [decision]: [rationale]
### Next Steps
1. [next action]
## Context Recovery
- Daily log: memory/daily/{date}.md
- Backup: .claude/backups/pre-compact-{timestamp}.md
- Git diff: `git diff HEAD~{n}`
## Active Problems
- [unresolved bug, last known state]Prompt Caching (Anthropic — maximize cache hits)
Cache hierarchy: Tools → System → Messages. Content is cached as a byte-identical prefix.
| Operation | Multiplier | Break-even |
| Cache write | 1.25x | Pays off after 1 read |
| Cache READ | 0.1x | 90% savings per read |
| 1-hour TTL | 2x write | Pays off after 2 reads |
- Max 4 breakpoints per request
- Minimum: 1024 tokens (Sonnet/Opus), 2048 tokens (Haiku)
- Cached tokens DON'T count toward ITPM rate limits — critical for Tier 1
- TTL is sliding: refreshed on every hit (5min default)
Implementation
// Cache tools + system prompt (static content)
const systemWithCache = [
{
type: "text",
text: systemPrompt,
cache_control: { type: "ephemeral" },
},
];
// Tools are cached automatically when system prompt is cached (same prefix)
// For conversation history: mark last message in history with cache_controlPost-Compaction Recovery (Energy Hook System)
Hook Chain
- PreCompact (
pre-compact-memory-flush.sh): Write anchor-state.md + backup + daily log + compaction count + auto-compress LEARNINGS if > 500 lines - PostCompact (
post-compact-restore.shvia SessionStartcompactmatcher): Re-inject anchor state + active task + recent logs + compaction warning
New Tools (MNEMOSYNE — March 2026)
/memory search "query"— Searches LEARNINGS, daily, topics, vault, rules viascripts/memory-search.sh/memory compress— Archives old entries viascripts/memory-compress.sh/memory store "key" "value"— Stores in MCP server-memory entity graph- MCP
mixgram— SQLite FTS5 full-text search over markdown (14 tools)
Compact Instructions (in CLAUDE.md)
Always preserve: maintainer's recent messages, active task, file paths, decisions, errors, API status, sprint progress. Always drop: tool outputs, search results, debugging steps, file contents (keep path+line only).
Compaction Trigger
- Auto-compaction at ~64-75% context usage (128K-150K tokens on 200K window)
- Leaves ~25% as working memory
- CLAUDE.md is automatically re-read
- Daily logs + anchor state provide continuity
Production Memory Solutions
| Solution | Compression | Key Pattern | Best For |
| SimpleMem | 30x (97%) | 3-stage: filter → consolidate → retrieve | Long-running agents |
| MemGPT/Letta | Agent-managed | 3-tier: Core/Recall/Archival | Agents that manage own memory |
| Mem0 | 90% tokens | Extract → CRUD (ADD/UPDATE/DELETE) | Personal AI assistants |
| Zep/Graphiti | N/A | Temporal knowledge graph | Time-aware reasoning |
| Memoria | 99.6% | Weighted KG + recency retrieval | Conversational AI |
Implementation Libraries
| Need | Library | Notes |
| BM25/FTS | SQLite FTS5 (via better-sqlite3) | Zero dependencies, built into SQLite |
| Vector search | LanceDB or OpenMem | LanceDB: hybrid mode built-in |
| Embeddings | text-embedding-3-small | Fallback: local ggml |
| Observation/Reflection | Mastra Memory | Observer at 30K tokens, Reflector at 40K |
| Fact extraction | Mem0 (pip/npm) | CRUD memory ops, dedup, atomic facts |
| MCP memory server | SimpleMem-MCP | LanceDB backend, 30x compression |
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 2
- naman10parikh/pixel★ 0100% same
- naman10parikh/turbo★ 0100% same
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository31 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- llms.txtllms.txt
- agentgrid-orchestrateSkill
- agentgrid-sessionsSkill
- agentgridSkill
- agent-runtimeSkill
- frontend-designSkill
- architectSkill
- deep-thinkSkill
- design-systemsSkill
- ecosystem-referenceSkill
- fractal-delegationSkill
- harness-reviewSkill
- heartbeatSkill
- integrate-resourcesSkill
- llm-council-critiqueSkill
- loop-createSkill
- mcp-setupSkill
- memorySkill
- model-routingSkill
- oss-project-scaffoldSkill
- playwright-cliSkill
- repo-scoutSkill
- self-improveSkill
- skill-creatorSkill
- skill-routingSkill
- sprintSkill
- test-visualSkill
- troubleshootSkill
- ux-auditSkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

