memory-optimize
kochetkov-ma/claude-brewcode
Optimize Claude Code memory files in 4 interactive steps, reducing token count by 30-50%.
What is memory-optimize?
Removes duplicate entries, migrates rules to proper config files, compresses remaining content, and validates the result. Use this when your Claude Code memory files have grown large or contain redundant information across sessions.
- Identifies and removes duplicate entries already present in CLAUDE.md or rules files
- Migrates reusable rules and constraints to ~/.claude/rules/ or project .claude/rules/
- Compresses remaining memory entries using efficient formatting (prose→tables, verbose→one-liners)
- Validates final state and cleans broken file references and orphaned memory files
- Reports token savings with before/after metrics
How to install memory-optimize
npx skills add https://github.com/kochetkov-ma/claude-brewcode --skill memory-optimize- Claude Code with auto-memory enabled (CLAUDE_CODE_DISABLE_AUTO_MEMORY=0)
- Existing memory files in ~/.claude/projects/*/memory/
- Optional: existing CLAUDE.md or .claude/rules/ files to migrate content into
How to use memory-optimize
- 1.Run `/memory-optimize` with no arguments to start the interactive workflow
- 2.Review the duplicate analysis in Step 1 and approve deletion of redundant entries
- 3.Approve migration of rules to CLAUDE.md and rules files in Step 2
- 4.Accept compression recommendations in Step 3 to reduce token count
- 5.Review the final report showing token savings and changes applied
Use cases
- Reduce API costs by optimizing memory file size before long projects
- Clean up accumulated memory entries after multiple sessions in a project
- Migrate project-specific rules from memory into persistent configuration files
- Consolidate duplicate constraints that have accumulated across memory files
- Prepare memory files for sharing by removing redundant content
- Claude Code users managing multiple projects with auto-memory enabled
- Developers looking to reduce token consumption in long-running sessions
- Teams standardizing rules across projects via CLAUDE.md and rules files
- Anyone with memory files that have grown to 30%+ redundancy
memory-optimize FAQ
30-50% reduction in memory file token count, depending on how much duplication and compressible content exists.
Yes. Each step is interactive with approval prompts. Step 1 only deletes content that exists elsewhere, and you can review or skip any step.
Global rules go to ~/.claude/rules/, project-specific rules go to .claude/rules/ in the project, and architectural decisions go to CLAUDE.md.
The skill uses standard Edit operations. Review the final report carefully; consider backing up memory files beforehand if you want a safety net.
No. Memory optimization works on stored memory files. Changes take effect on the next session when memory is reloaded.
Full instructions (SKILL.md)
Source of truth, from kochetkov-ma/claude-brewcode.
name: memory-optimize description: "Optimizes Claude Code memory files in 4 interactive steps: removes duplicates, migrates rules to CLAUDE.md/rules files, compresses remaining entries, validates with cleanup. Typical reduction: 30-50% on token count." tags: [auto-memory, claude-code-memory, claude-code-auto-memory] license: MIT metadata: author: "kochetkov-ma" version: "3.1.0" source: "claude-brewcode" user-invocable: true allowed-tools: Read Write Edit Glob Grep Bash Task AskUserQuestion model: opus
Plugin: kochetkov-ma/claude-brewcode
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
- Glob all memory files:
~/.claude/projects/**/memory/*.md - Read
~/.claude/CLAUDE.mdand projectCLAUDE.md(if exists) - Glob
.claude/rules/*.md— read all project rules - 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.
- Spawn
Exploreagent to cross-reference all loaded files - Identify entries where:
- Same rule already in CLAUDE.md
- Same pattern already in a rules file
- Contradicts CLAUDE.md (CLAUDE.md wins)
- Show analysis:
Found X duplicate/redundant entries (Y% of memory): | Entry | Memory File | Already In | Action | |-------|-------------|------------|--------| | "Search via Bash grep" | MEMORY.md:5 | rules/code-search.md | DELETE | ... AskUserQuestion: "Delete X duplicate entries (Y% of memory)? This is safe — content exists elsewhere."- Options: "Yes, delete all" / "Review each" / "Skip this step"
- Apply deletion using
Edittool 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
- 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 AskUserQuestion: "Migrate X entries to rules/CLAUDE.md?"- Options: "Yes, migrate all" / "Review each" / "Skip this step"
- 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
- Create/append to target rule files via
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
- Show compression preview:
Show 2-3 specific before/after samples.Compression opportunities found: | Before | After | Savings | |--------|-------|---------| | "When you need to... always use..." | "Use X for Y" | 8 tokens | ... Total: ~Y% token reduction (~Z tokens) AskUserQuestion: "Compress remaining memory? (~Y% reduction)"- Options: "Yes, compress all" / "Skip compression"
- Apply compression via
Edit(bottom-up order to preserve line numbers)
Step 4: Validation (Automatic)
Goal: Verify final state and clean orphaned references.
- Spawn
revieweragent to verify:- No broken file path references in memory files
- No contradictions between memory and CLAUDE.md
- Memory files are well-formed markdown
- Clean broken references (Edit tool)
- Check for orphaned memory files (files in
~/.claude/projects/**/memory/with no MEMORY.md reference) - 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, Claude installation docs, Markdown to PDF.
Install: `claude plugin marketplace add https://github.com/kochetkov-ma/claude-brewcode && claude plugin install brewdoc@claude-brewcode`
Related skills
More from kochetkov-ma/claude-brewcode and the wider catalog.

text-optimizer
Reduces token count in prompts and docs by 20–40% using 52 research-backed optimization rules.

finlab
Comprehensive quantitative trading package for global stock markets with backtesting, factor analysis, and strategy development.

404-page-generator
When the user wants to create, optimize, or audit 404 error pages. Also use when the user mentions "404 page," "404 error," "error page," "page not found," "broken link page," "404 design," "custom 404," "404 redirect," "404 page UX," or "404 recovery." For sitewide page planning, use website-structure.

about-page-generator
When the user wants to create, optimize, or audit About page content. Also use when the user mentions "about page," "about us," "company story," "our team," "about section," "company overview," "brand story," "team page," or "who we are." For sitewide page planning, use website-structure.

affiliate-marketing
When the user wants to plan, implement, or optimize affiliate marketing strategy. Also use when the user mentions "affiliate marketing," "affiliate program strategy," "CPS model," "affiliate recruitment," "commission structure," "affiliate partners," "affiliate network," "affiliate tracking," "affiliate commission," or "partner marketing." For affiliate page, use affiliate-page-generator.

affiliate-page-generator
When the user wants to create, optimize, or audit affiliate program page content. Also use when the user mentions "affiliate program," "affiliate page," "partner program," "referral program page," "affiliate landing," "partner landing," "commission page," or "affiliate signup." For affiliate program strategy, use affiliate-marketing.