Get the FREE Ultimate OpenClaw Setup Guide →
npx machina-cli add skill kochetkov-ma/claude-brewcode/memory-optimize --openclaw
Files (1)
SKILL.md
5.7 KB

Memory Optimizer

Optimizes Claude Code auto-memory files in 4 interactive steps: removes duplicates, migrates rules to proper config files, compresses remaining entries, validates the result. Typical reduction: 30–50% token count in memory files.

Auto-memory stores context across sessions in ~/.claude/projects/**/memory/MEMORY.md. Enable: CLAUDE_CODE_DISABLE_AUTO_MEMORY=0 · Disable: CLAUDE_CODE_DISABLE_AUTO_MEMORY=1

Benefits: faster context loading · no duplicate rules · cleaner instructions · lower API cost

Usage:

/memory-optimize          # no args — starts 4-step interactive workflow

Skill text is written for LLM consumption and optimized for token efficiency.


Memory Optimizer

Optimizes Claude Code memory files through 4 interactive steps.

No context: fork — must run in main conversation to spawn agents.

Phase 0: Load Context

  1. Glob all memory files: ~/.claude/projects/**/memory/*.md
  2. Read ~/.claude/CLAUDE.md and project CLAUDE.md (if exists)
  3. Glob .claude/rules/*.md — read all project rules
  4. Read ~/.claude/rules/*.md — read all global rules

Build context map:

memory_files: [paths]
claude_md_sections: [sections]
rules_files: [paths with content]

Step 1: Analysis — Remove Duplicates (Interactive)

Goal: Find memory entries that duplicate content already in CLAUDE.md or rules.

  1. Spawn Explore agent to cross-reference all loaded files
  2. Identify entries where:
    • Same rule already in CLAUDE.md
    • Same pattern already in a rules file
    • Contradicts CLAUDE.md (CLAUDE.md wins)
  3. Show analysis:
    Found X duplicate/redundant entries (Y% of memory):
    | Entry | Memory File | Already In | Action |
    |-------|-------------|------------|--------|
    | "Use grepai first" | MEMORY.md:5 | rules/grepai-first.md | DELETE |
    ...
    
  4. AskUserQuestion: "Delete X duplicate entries (Y% of memory)? This is safe — content exists elsewhere."
    • Options: "Yes, delete all" / "Review each" / "Skip this step"
  5. Apply deletion using Edit tool if approved

Step 2: Migration — Move to Rules/CLAUDE.md (Interactive)

Goal: Identify remaining memory entries better suited to persistent config files.

Decision tree (per entry):

  • Applies to ALL projects + IS a rule/constraint → ~/.claude/rules/
  • Applies to THIS project only + IS a rule → .claude/rules/
  • IS an architectural decision → project CLAUDE.md
  • IS a fact/pattern reusable across sessions → KEEP in memory
  1. Show categorization:
    X entries suitable for migration:
    | Entry | Current Location | Target | Reduction |
    |-------|-----------------|--------|-----------|
    | "Always use BD_PLUGIN_ROOT" | MEMORY.md:12 | .claude/rules/brewdoc.md | 15 tokens |
    ...
    Total: X entries → ~Y tokens saved
    
  2. AskUserQuestion: "Migrate X entries to rules/CLAUDE.md?"
    • Options: "Yes, migrate all" / "Review each" / "Skip this step"
  3. If approved:
    • Create/append to target rule files via Edit
    • Remove migrated entries from memory via Edit
    • If target file doesn't exist, create it

Step 3: Compression (Interactive)

Goal: Compress remaining entries using LLM-efficient formatting.

Compression techniques:

  • Prose → table row
  • Multiple related entries → single table
  • Verbose description → imperative one-liner
  • List of examples → pattern + one example
  1. Show compression preview:
    Compression opportunities found:
    | Before | After | Savings |
    |--------|-------|---------|
    | "When you need to... always use..." | "Use X for Y" | 8 tokens |
    ...
    Total: ~Y% token reduction (~Z tokens)
    
    Show 2-3 specific before/after samples.
  2. AskUserQuestion: "Compress remaining memory? (~Y% reduction)"
    • Options: "Yes, compress all" / "Skip compression"
  3. Apply compression via Edit (bottom-up order to preserve line numbers)

Step 4: Validation (Automatic)

Goal: Verify final state and clean orphaned references.

  1. Spawn reviewer agent to verify:
    • No broken file path references in memory files
    • No contradictions between memory and CLAUDE.md
    • Memory files are well-formed markdown
  2. Clean broken references (Edit tool)
  3. Check for orphaned memory files (files in ~/.claude/projects/**/memory/ with no MEMORY.md reference)
  4. Report orphaned files and ask to delete

Final Report:

## Memory Optimization Complete

### Summary
| Metric | Before | After | Saved |
|--------|--------|-------|-------|
| Total entries | X | Y | Z |
| Duplicate entries | X | 0 | — |
| Migrated entries | — | — | X |
| Token estimate | ~X | ~Y | ~Z (~P%) |

### Changes Made
- Step 1: Deleted X duplicate entries
- Step 2: Migrated X entries to rules/CLAUDE.md
- Step 3: Compressed X entries (Y% reduction)
- Step 4: Fixed X broken references, removed X orphaned files

### Final Memory Structure
{directory listing of ~/.claude/projects/.../memory/}

---

**Part of brewdoc:** [brewcode](https://github.com/kochetkov-ma/claude-brewcode) — docs tools: memory optimization, auto-sync, Claude installation docs, Markdown to PDF.
Install: `claude plugin marketplace add https://github.com/kochetkov-ma/claude-brewcode && claude plugin install brewdoc@claude-brewcode`

Source

git clone https://github.com/kochetkov-ma/claude-brewcode/blob/main/skills/memory-optimize/SKILL.mdView on GitHub

Overview

Memory-optimize cleans Claude Code auto-memory files in four interactive steps: remove duplicates, migrate rules to CLAUDE.md/rules files, compress remaining entries, and validate with cleanup. Typical reductions are 30–50% fewer tokens, speeding context loading and lowering API costs.

How This Skill Works

It scans memory files under ~/.claude/projects/**/memory/MEMORY.md, CLAUDE.md, and rule files in .claude/rules/*.md to build a context map. It then executes four interactive steps: duplicate analysis, conditional migration to rules or CLAUDE.md, compression of remaining entries, and final validation/cleanup to ensure consistency.

When to Use It

  • When long Claude Code memory files contain duplicates that inflate token usage
  • When you want to migrate stable rules from memory into persistent config files
  • When memory entries should be compressed into compact formats for faster loading
  • When preparing a project for faster startup and lower API costs
  • When standardizing memory-rule sources across multiple projects

Quick Start

  1. Step 1: /memory-optimize — starts the 4-step interactive workflow
  2. Step 2: Follow prompts to review duplicates and migrate entries
  3. Step 3: Accept the compression preview and apply changes

Best Practices

  • Run in the main conversation (no context: fork) to spawn agents
  • Glob and read all requested memory and rules files before analysis
  • Review migration decisions and approve deletions or moves
  • Use the compression preview to understand token savings before applying
  • Validate results by re-loading memory to confirm no contradictions

Example Use Cases

  • A Claude Code project reduces memory tokens by ~40% after duplicates removal and rule migrations
  • Global rules are centralized under ~/.claude/rules/*.md, reducing memory duplication
  • Project CLAUDE.md updated with non-duplicated semantics after migration
  • Session startup becomes noticeably faster due to smaller MEMORY.md
  • Teams standardize memory rules across multiple projects for consistency

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers