๐ŸŽ Get the FREE AI Skills Starter Guide โ€” Subscribe โ†’
BytesAgainBytesAgain
๐Ÿฆ€ ClawHub

Context Memory Optimizer

by @donnieclaw

v3.1 (ClawHub-safe, source-verified): OpenClaw context memory optimizer. Prevents context overflow, memory drift, and token waste in long agent sessions. Bas...

Versionv1.0.5
Downloads473
Starsโญ 2
TERMINAL
clawhub install context-memory-optimizer

๐Ÿ“– About This Skill


name: context-memory-optimizer version: "3.1.0"

description: > v3.1 (ClawHub-safe, source-verified): OpenClaw context memory optimizer. Prevents context overflow, memory drift, and token waste in long agent sessions. Based on logic verified against Claude Code source and the claw-code open-source port (compact.rs). Use when: agent memory is confused, sessions are too long, context window is exhausted, or multi-agent coordination is unstable. ๅฝ“ agent ่ฎฐๅฟ†ๆททไนฑใ€ไผš่ฏ่ฟ‡้•ฟๆŠฅ้”™ใ€ไธŠไธ‹ๆ–‡็ช—ๅฃไธๅคŸ็”จใ€ๅคš agent ๅไฝœไธ็จณๅฎšๆ—ถไฝฟ็”จใ€‚

โ”€โ”€ Transparency Declaration (required by ClawHub safety review) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

This skill instructs agents to READ and WRITE files under a well-defined

directory tree. All paths are listed below. No files outside these paths

are accessed. System prompt modification is IN-SESSION ONLY โ€” the skill

provides snippet text for the operator to paste into their agent config;

it does NOT programmatically alter any runtime system prompt.

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

persistent_paths: # All read/write activity is scoped to the agent's own workspace directory. # Replace {agent} with your OpenClaw agent name (e.g. gemini, minifool). read: - "~/.openclaw/workspace-{agent}/MEMORY.md" - "~/.openclaw/workspace-{agent}/AGENTS.md" - "~/.openclaw/workspace-{agent}/memories/project.md" - "~/.openclaw/workspace-{agent}/memories/decisions.md" - "~/.openclaw/workspace-{agent}/memories/errors.md" - "~/.openclaw/workspace-{agent}/memories/context.md" write: - "~/.openclaw/workspace-{agent}/MEMORY.md" # index pointer updates - "~/.openclaw/workspace-{agent}/memories/errors.md" # error/denial backlog - "~/.openclaw/workspace-{agent}/memories/context.md" # task snapshot on compact

persistent_changes: # What this skill changes and why โ€” required for ClawHub registry transparency. - path: "MEMORY.md" change: "Appends or updates pointer entries when new tasks or decisions are recorded." reversible: true notes: "Safe to delete and regenerate at any time." - path: "memories/errors.md" change: "Appends one-line entries when PermissionDenied or path errors occur." reversible: true notes: "Append-only log. Delete file to reset." - path: "memories/context.md" change: "Overwrites with current task snapshot before each L2 compaction." reversible: false notes: > Previous snapshot is replaced. Keep a copy if you need task history. Consider committing memories/ to a private branch if history matters.

system_prompt_effect: > [operator-controlled only] This skill does NOT programmatically modify any agent base configuration. It provides opt-in snippet text (Steps 2 and 5) that the operator manually pastes into their agent config. All changes are explicit and operator-controlled. No runtime injection occurs.

requires: - openclaw_workspace: true # Agent must have ~/.openclaw/workspace-{agent}/ set up - file_read_permission: true - file_write_permission: true - bash_permission: false # No shell commands are issued by this skill itself


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

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 ๅทฅไฝœๅŒบ็ป“ๆž„

~/.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 ้ชŒ่ฏไผ˜ๅ…ˆๆŒ‡ไปค

    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 ๅŽ‹็ผฉๅˆ†ๅฑ‚

    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:

    - 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} Strip any 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.*

    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 ๅไฝœ

    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)

    {sender} {receiver} {YYYYMMDD-seq} read_only | write | decision {background, โ‰ค 200 chars} {specific input} {output format} {constraints}

    Result Format

    {matching task_id} done | failed | blocked {result} {list of files or state modified} {content to append to memories/ โ€” leave empty if none}

    Prompt Cache Protection ็ผ“ๅญ˜ไฟๆŠค

    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๏ผ‰

    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