← Back to Skills Marketplace
donnieclaw

Context Memory Optimizer

by Donnieclaw · GitHub ↗ · v1.0.5 · MIT-0
cross-platform ✓ Security Clean
176
Downloads
2
Stars
0
Active Installs
6
Versions
Install in OpenClaw
/install context-memory-optimizer
Description
v3.1 (ClawHub-safe, source-verified): OpenClaw context memory optimizer. Prevents context overflow, memory drift, and token waste in long agent sessions. Bas...
README (SKILL.md)

OpenClaw Context Memory Optimizer v3.1

Source-verified · ClawHub-safe · Transparent path declarations

Compaction logic verified against claw-code open-source port (compact.rs). v2.0 turn-count trigger has been corrected to token-count trigger. All file paths and persistent changes are declared in the frontmatter above.


Core Principles

\x3C!-- Why each principle exists — not just what it is -->

  1. Memory as Pointers 记忆是指针 MEMORY.md stores locations, not content. Keeps the always-loaded index under 800 tokens regardless of project size.

  2. Skeptical Memory 怀疑型记忆 Agent treats its own memory as a hint, not ground truth. Always cross-verify against source files before any write or bash action. Prevents stale-memory bugs in long sessions.

  3. Token-Driven Compaction Token 驱动压缩 Trigger is token volume, not turn count. Formula: total_chars ÷ 4. Source: compact.rs → estimate_session_tokens().

  4. Immediate Post-Compact Restore 压缩后立即恢复 Key files are re-injected right after compaction so the agent never "forgets what it was doing." Source: Claude Code post-compact file restore.

  5. Circuit Breaker 熔断保护 Stop retrying after 3 consecutive compaction failures. Prevents the real-world incident where one session wasted 250k API calls/day. Source: Claude Code BigQuery incident note in source comments.


Step 1: Workspace Setup 工作区结构

\x3C!-- All paths listed here are also declared in persistent_paths above. --> \x3C!-- Operators can audit exactly what this skill touches before installing. -->

~/.openclaw/workspace-{agent}/
├── MEMORY.md              # Always loaded. Pointers only. Keep ≤ 800 tokens.
├── memories/
│   ├── project.md         # Tech stack + project fingerprint + module manifest
│   ├── decisions.md       # Decision log — record the "why", not the "what"
│   ├── errors.md          # PermissionDenied / bad-path backlog (append-only)
│   └── context.md         # Current task snapshot — overwritten before each L2
└── AGENTS.md              # Multi-agent roles and coordination rules

MEMORY.md template — each line ≤ 150 chars, full file ≤ 800 tokens:

# MEMORY INDEX
# Each entry is a pointer to a file, not a copy of its content.
_Updated: {timestamp} | Token estimate: {session_chars ÷ 4}_

## Current Project
- Name: {name} | Details → memories/project.md

## Active Task
- {task description ≤ 50 chars} | Status: in-progress | Details → memories/context.md

## Key Constraints
- {constraint ≤ 30 chars}

## Recently Referenced Files
- {file/path.ext} | {one-line purpose}

_This index is a hint, not ground truth. Read the linked files for details._

Privacy note 隐私提示: Add memories/ to .gitignore if your .openclaw/ directory is synced to Git.


Step 2: Verification-First Instructions 验证优先指令

\x3C!-- SYSTEM PROMPT EFFECT: The text block below is a copy-paste snippet for --> \x3C!-- the operator to add to their agent config. This skill itself does NOT --> \x3C!-- modify any system prompt programmatically. -->

Add the following to the static section of your agent's system prompt. Label it clearly so you know what it does:

## Memory Integrity Rules
# Source: context-memory-optimizer skill, Step 2
# Purpose: prevents stale-memory bugs and repeated permission errors

1. MEMORY.md is a hint, not ground truth.
   Before any write or bash action, read the relevant source file to verify.
   Do not act on memory summaries alone.

2. Check memories/errors.md before acting.
   Do not retry operations that previously caused PermissionDenied.
   Do not reuse paths that previously returned FileNotFound.

3. When memory contradicts source files:
   → Trust the source file
   → Update the relevant MEMORY.md pointer entry
   → Append one line to memories/errors.md describing the mismatch

Step 3: Token-Driven Compaction 压缩分层

\x3C!-- TOKEN THRESHOLD CORRECTION from v2.0: --> \x3C!-- v2.0 used "trigger after 15 turns" — this is inaccurate. --> \x3C!-- compact.rs uses token volume as the trigger, not turn count. --> \x3C!-- Production threshold in Claude Code: 200,000 tokens. --> \x3C!-- We use 150,000 here to leave a safety margin. -->

Token estimation formula (from compact.rs estimate_session_tokens):

per message:
  text block    → char_count ÷ 4 + 1
  tool_use      → (name_len + input_len) ÷ 4 + 1
  tool_result   → (name_len + output_len) ÷ 4 + 1
total           → sum of all messages

L1 — Tool Output Soft Trim 工具输出软截断

Runs automatically after every tool call. No user action needed.

  • bash / read / search output > 2,000 chars: keep last 500 chars + one-line conclusion
  • File reads > 3,000 chars: extract key paragraphs, append [trimmed — full content at {path}]
  • Keep the 8 most recent tool records; summarize earlier ones to one line each

L2 — Session Compaction 会话压缩

Trigger: estimated tokens > 150,000

Run these steps in order. Do not skip or reorder.

Step ① — Scan for pending-work keywords
  Search recent messages for sentences containing (case-insensitive):
    todo / next / pending / follow up / remaining
  Collect matched sentences for inclusion in the summary.
  Source: compact.rs → infer_pending_work()

Step ② — Extract key file paths
  Scan all message content for tokens that:
    - contain a forward slash /
    - end with: .md .json .py .ts .js .rs .yaml .toml
  Deduplicate. Keep at most 8 paths.
  Source: compact.rs → collect_key_files()

Step ③ — Generate summary
  Format:
    \x3Csummary>
    - Completed: {bullet list of finished work}
    - Pending:   {output of Step ①}
    - Key files: {output of Step ②}
    - Current task: {first 200 chars of most recent non-empty message}
    \x3C/summary>
  Strip any \x3Canalysis> blocks before inserting.
  Source: compact.rs → format_compact_summary()

Step ④ — Inject continuation message
  Use this exact text. Do not paraphrase.
  ──────────────────────────────────────────────────────────────
  The following is a summary of the earlier portion of this session.
  Continue directly from where the conversation left off.
  Do not acknowledge this summary. Do not recap. Do not ask questions.
  Resume the task immediately.

  Summary:
  {output of Step ③}
  ──────────────────────────────────────────────────────────────
  Source: compact.rs → get_compact_continuation_message(suppress_follow_up=true)

Step ⑤ — Discard old messages
  Keep: continuation message (Step ④) + 4 most recent messages.
  Discard everything else.
  Source: compact.rs → preserve_recent_messages default = 4

Step ⑥ — Run L3 immediately (see below)

L3 — Key File Restore 关键文件恢复

Runs immediately after every L2 compaction.

\x3C!-- These reads are declared in persistent_paths.read above. -->

Read in this order:

  1. MEMORY.md — reload the pointer index
  2. memories/context.md — most important: current task snapshot
  3. Up to 5 files from Step ② — limit each to 1,000 tokens

After reading, resume the task. Do not tell the user "I have restored X files."

L4 — Circuit Breaker 熔断器

If L2 compaction fails 3 consecutive times:
  → Stop retrying L2
  → Fall back to L1 soft trim only
  → Append failure reason to memories/errors.md
  → Wait for operator to manually clear and restart

Why: A real session once failed L2 compaction 3,272 times in a row,
wasting 250,000 API calls per day before the circuit breaker was added.
Source: Claude Code internal BigQuery incident note.

L5 — Emergency Trim 紧急裁剪

Trigger: context_length_exceeded error

1. Write the conclusion of the last assistant message to memories/context.md
   (this is the only write that happens during emergency trim)
2. Discard the 2 oldest conversation turns
3. Repeat until the error clears

Step 4: Multi-Agent Coordination 多 Agent 协作

\x3C!-- AGENTS.md path is declared in persistent_paths.read above. --> \x3C!-- This step provides a template. No files are written automatically. -->

AGENTS.md template

# Multi-Agent Coordination Rules
# Scope: ~/.openclaw/workspace-{agent}/AGENTS.md
# Edit this file to match your actual agent names and responsibilities.

## Agent Roles
- {agent-1}: {responsibility}
- {agent-2}: {responsibility}
- {agent-3}: {responsibility}

## Task Handoff Format (XML — required, no free-text handoffs)
\x3Ctask>
  \x3Cfrom>{sender}\x3C/from>
  \x3Cto>{receiver}\x3C/to>
  \x3Ctask_id>{YYYYMMDD-seq}\x3C/task_id>
  \x3Ctype>read_only | write | decision\x3C/type>
  \x3C!-- read_only tasks may run in parallel.      -->
  \x3C!-- write tasks must run serially.            -->
  \x3C!-- decision tasks require coordinator sign-off before execution. -->
  \x3Ccontext>{background, ≤ 200 chars}\x3C/context>
  \x3Cinput>{specific input}\x3C/input>
  \x3Cexpected_output>{output format}\x3C/expected_output>
  \x3Cconstraints>{constraints}\x3C/constraints>
\x3C/task>

## Result Format
\x3Cr>
  \x3Ctask_id>{matching task_id}\x3C/task_id>
  \x3Cstatus>done | failed | blocked\x3C/status>
  \x3Co>{result}\x3C/o>
  \x3Cside_effects>{list of files or state modified}\x3C/side_effects>
  \x3Cmemory_update>{content to append to memories/ — leave empty if none}\x3C/memory_update>
\x3C/r>

Prompt Cache Protection 缓存保护

\x3C!-- SYSTEM PROMPT EFFECT: same as Step 2 — operator pastes this layout --> \x3C!-- into their agent config. Nothing is modified programmatically. -->

Structure your agent's system prompt in two sections, in this order:

[STATIC SECTION]
# Role definition, tool rules, behavior constraints.
# Never insert dynamic content here — it breaks prefix caching.

--- DYNAMIC BOUNDARY ---

[DYNAMIC SECTION]
# Content that changes each turn goes here, at the END.
{MEMORY.md content}
Current task: {task}
Timestamp: {timestamp}

Do not insert timestamps, summaries, or task descriptions into the middle of the static section. Doing so invalidates the prefix cache and re-bills the full static section on every turn.


Step 5: Speculative Pre-warming 推测预热(optional)

\x3C!-- SYSTEM PROMPT EFFECT: operator pastes this into their agent config. --> \x3C!-- No programmatic system prompt modification occurs. -->

Add to your agent's system prompt (static section, after the memory rules):

## Background Pre-warming
# Source: context-memory-optimizer skill, Step 5
# Runs before every reply. Read-only. Never writes during pre-warming.

Before responding, silently complete:
1. Read MEMORY.md if not already read this turn.
2. Estimate token count: session_chars ÷ 4.
   If estimate > 120,000 → prepare for L2 compaction on next tool cycle.
3. If the task involves code, pre-read the key files listed in MEMORY.md
   (read-only — do not write or modify anything during pre-warming).

Do not mention pre-warming in your reply.

Deployment Checklist 部署验证清单

  • MEMORY.md exists and is ≤ 800 tokens
  • memories/ contains: project.md, decisions.md, errors.md, context.md
  • Step 2 snippet is in the static section of the agent system prompt
  • Dynamic content is after --- DYNAMIC BOUNDARY ---
  • Manually trigger one L2 compaction to verify the continuation message format
  • Multi-agent handoffs use the XML format from AGENTS.md
  • memories/ is in .gitignore (if workspace is Git-synced)

References

  • references/compression-examples.md — before/after compaction examples, key file extraction walkthrough, project fingerprint format
  • references/prompt-cache.md — prompt cache segmentation strategy, common mistakes, cost estimation
Usage Guidance
This skill appears to do what it says: optimize and compact agent memory by reading and writing only under ~/.openclaw/workspace-{agent}/. Before enabling it: 1) Back up the workspace (especially memories/context.md and MEMORY.md) because the skill declares an irreversible overwrite of context.md. 2) Confirm the workspace path(s) are correct and contain no sensitive secrets you don't want agents to read. 3) Ask the publisher to fix the registry metadata mismatch (it should list the config paths/persistent_paths). 4) Review any config snippets the skill tells you to paste into your agent config before applying them. 5) Consider trying it in a test agent first to observe behavior and ensure it doesn't remove history you need.
Capability Assessment
Purpose & Capability
The skill's name and description align with its actions: it operates purely on an OpenClaw workspace and implements context compaction/restore logic. However, the registry metadata at the top claimed 'Required config paths: none' while the SKILL.md declares persistent_paths under ~/.openclaw/workspace-{agent}/; this mismatch should be corrected or explained by the publisher.
Instruction Scope
All runtime instructions are local and file-focused (reading MEMORY.md, memories/*, AGENTS.md and overwriting memories/context.md prior to compaction). The SKILL.md explicitly states no programmatic system-prompt injection and no shell/batch commands. Two operator-facing behaviors to watch: (1) memories/context.md is overwritten (declared irreversible), so history can be lost if not backed up, and (2) the skill provides config snippets for the operator to paste into agent configs — review these snippets before applying them.
Install Mechanism
Instruction-only skill with no install spec and no code files, so nothing is downloaded or written to disk by an installer. Lowest-risk install footprint.
Credentials
The skill requests no environment variables or external credentials. Its declared 'requires' are workspace and file read/write permissions appropriate to its purpose. This is proportionate.
Persistence & Privilege
always: false (not force-included). The skill can be invoked autonomously by the agent (platform default), and it performs local file writes within the declared workspace. That combination is coherent but means an invoked skill can modify the agent's own workspace files during a session — ensure you trust the skill and back up important files.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install context-memory-optimizer
  3. After installation, invoke the skill by name or use /context-memory-optimizer
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.5
No file changes detected. No code or documentation updates in this version. - Version incremented to 1.0.5 with no detected modifications. - All features, behaviors, and documentation remain identical to the previous release.
v1.0.4
- Added transparent path and file-change declarations (required by ClawHub safety review), listing all file read/write actions and their scopes. - Updated and clarified core principles, emphasizing token-based compaction, pointer-based memory, and reproducibility. - System prompt instructions are now strictly opt-in: the skill never modifies prompts programmatically, only provides text for operators to copy. - Persistent workspace structure and all file operations are explicitly documented for user audit and registry transparency. - Documented token estimation and compaction thresholds with direct references to source-verified logic (compact.rs). - Improved privacy explanations and file handling recommendations, especially around .gitignore and workspace hygiene.
v1.0.3
**Major update: Version 3.0 is source-verified against Claude Code and claw-code's compact.rs, with core logic aligned to real-world compression mechanisms.** - Switched from round-count triggers to source-verified, token-based compression, as used in claw-code (strict 200k token threshold, using character count ÷ 4). - Overhauled compression workflow: steps for soft truncation, structured session summarization (including keyfile and TODO extraction), strict message continuation template, aggressive legacy message pruning, and immediate context recovery. - Introduced a circuit breaker (熔断器): automatic pause on three consecutive compression failures, as in Claude Code. - Revised file structure, MEMORY.md template, and clear multi-agent coordination protocol (XML-based with explicit concurrency rules). - Enforced strict separation of static and dynamic prompt sections to optimize prompt cache and cost. - Validation and deployment checklist added for reliability. - All steps, thresholds, and summaries now reflect line-by-line behavior observed in open-source and leaked Claude agent memory logic.
v1.0.2
- Added a Security & Audit Notice clarifying privacy, read-only restrictions, and no bypass of safety filters. - Refined core principles for clarity and merged verification practice under "Verification-First Memory." - Simplified instructions and templates for MEMORY.md and agent prompt integration. - Streamlined the 5-layer compression strategy and aligned terminology; rules now focus on automation and clarity. - Updated multi-agent communication section, condensing and standardizing templates and roles. - Made privacy instructions and security caveats more prominent throughout the documentation.
v1.0.1
## context-memory-optimizer 1.0.1 Changelog - Improved English/Chinese documentation for broader global audience. - Clarified and streamlined core principles and step-by-step usage. - Enhanced and simplified file structure and MEMORY.md templates. - Standardized multi-agent coordination with formal XML-style task/result formats. - Added emphasis on skeptical memory and layered context compression. - Document is now more concise, actionable, and easier to integrate with various LLM agents.
v1.0.0
首次发布
Metadata
Slug context-memory-optimizer
Version 1.0.5
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 6
Frequently Asked Questions

What is Context Memory Optimizer?

v3.1 (ClawHub-safe, source-verified): OpenClaw context memory optimizer. Prevents context overflow, memory drift, and token waste in long agent sessions. Bas... It is an AI Agent Skill for Claude Code / OpenClaw, with 176 downloads so far.

How do I install Context Memory Optimizer?

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

Is Context Memory Optimizer free?

Yes, Context Memory Optimizer is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Context Memory Optimizer support?

Context Memory Optimizer is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Context Memory Optimizer?

It is built and maintained by Donnieclaw (@donnieclaw); the current version is v1.0.5.

💬 Comments