Skip to content

KimYx0207/claude-memory-3layer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Three-Layer Memory System for Claude Code

GitHub stars GitHub forks License Claude Code Python Dependencies

Give Claude Code a real memory โ€” structured, git-trackable, and token-efficient.

๐Ÿง  The management layer that official auto-memory doesn't have

โšก One-command install | Zero dependencies | ~1500 tokens budget

๐Ÿค Coexists with official auto-memory (v2.1.59+) โ€” they complement each other

๐Ÿ”— GitHub: https://github.com/KimYx0207/claude-memory-3layer

Chinese Version / ไธญๆ–‡็‰ˆๆœฌ


๐Ÿ“ž Contact

Contact

Get more AI tips and technical support

๐ŸŒ aiking.dev | ๐Ÿฆ @KimYx0207 | WeChat Official Account: ่€้‡‘ๅธฆไฝ ็ŽฉAI

โ˜• Buy Me a Coffee

If this project helped you, consider supporting it!

WeChat Pay
WeChat Pay
Alipay
Alipay

๐Ÿ“– Why This Exists

Claude Code forgets everything between sessions. Anthropic provides two solutions:

  • CLAUDE.md โ€” manual rules file, you write and maintain it
  • Auto-memory (v2.1.59+) โ€” AI-powered, Claude decides what to remember

Official auto-memory solves knowledge discovery brilliantly. But discovery is only half the problem.

The other half โ€” lifecycle management, git tracking, team sharing, precise token control โ€” is what this project handles.

This is not a replacement. It's the management layer that official auto-memory doesn't have. Use both together.

๐Ÿงฌ Official + Three-Layer: Better Together

Dimension Official v2.1.59 Three-Layer Memory Best for
Knowledge discovery AI semantic understanding Keyword matching (rules) Official wins
Install cost Zero config One command Official wins
Storage format Markdown (flat text + topic files) 3 layers (JSON + MD + MD) Three-Layer more flexible
Git tracking โŒ ~/.claude/projects/ (hidden) โœ… .claude/memory/ (in project) Three-Layer only
Lifecycle Write-only, grows forever status field (active/superseded) Three-Layer only
Token control Truncate at 200 lines (unknown tokens) ~1500 tokens per topic (configurable) Three-Layer more predictable
Team sharing โŒ Local only โœ… Via git Three-Layer only
Knowledge cleanup โŒ None โœ… Auto-suggest via /memory-review Three-Layer only

โœจ Core Features

  • ๐Ÿง  Three-Layer Architecture โ€” Knowledge Graph (JSON) + Daily Notes (MD) + Tacit Knowledge (MD)
  • โ™ป๏ธ Lifecycle Management โ€” Facts can expire (active โ†’ superseded), keeps knowledge lean
  • :git: Git-Trackable โ€” All memory lives in .claude/memory/, commit and share with your team
  • ๐ŸŽฏ Token-Efficient โ€” ~1500 tokens per topic (<1% of 200K context), fully configurable
  • ๐Ÿ“ฆ Zero Dependencies โ€” Pure Python stdlib, no pip install needed
  • ๐Ÿ”Œ Hook-Powered โ€” SessionStart / PostToolUse / PreCompact lifecycle hooks
  • ๐Ÿงน Smart Cleanup โ€” /memory-review finds outdated entries and suggests cleanup
  • ๐Ÿค Coexists with Official โ€” Works alongside auto-memory and CLAUDE.md

๐Ÿš€ Quick Start

One-Line Install

# macOS / Linux
curl -fsSL https://raw.githubusercontent.com/KimYx0207/claude-memory-3layer/main/install.sh | bash

# Windows PowerShell
irm https://raw.githubusercontent.com/KimYx0207/claude-memory-3layer/main/install.ps1 | iex

Manual Install

git clone https://github.com/KimYx0207/claude-memory-3layer.git
cd claude-memory-3layer
./install.sh   # or .\install.ps1 on Windows

What Gets Installed

your-project/
โ””โ”€โ”€ .claude/
    โ”œโ”€โ”€ hooks/
    โ”‚   โ”œโ”€โ”€ memory_loader.py      # SessionStart: load 3 layers
    โ”‚   โ”œโ”€โ”€ memory_extractor.py   # PostToolUse: extract knowledge
    โ”‚   โ”œโ”€โ”€ session_state.py      # State management
    โ”‚   โ””โ”€โ”€ pre_compact.py        # PreCompact: save before compression
    โ”œโ”€โ”€ memory/
    โ”‚   โ”œโ”€โ”€ MEMORY.md             # Layer 3: tacit knowledge
    โ”‚   โ”œโ”€โ”€ memory/               # Layer 2: daily notes
    โ”‚   โ””โ”€โ”€ areas/topics/         # Layer 1: knowledge graph
    โ”œโ”€โ”€ commands/
    โ”‚   โ”œโ”€โ”€ memory-review.md      # /memory-review command
    โ”‚   โ””โ”€โ”€ memory-status.md      # /memory-status command
    โ””โ”€โ”€ settings.json             # Hook registration

โš™๏ธ How It Works

Architecture Overview

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Claude Code Session                        โ”‚
โ”‚                                                              โ”‚
โ”‚  SessionStart โ”€โ”€โ†’ memory_loader.py โ”€โ”€โ†’ Load 3 layers         โ”‚
โ”‚       โ”‚                                    โ”‚                  โ”‚
โ”‚       โ–ผ                                    โ–ผ                  โ”‚
โ”‚  [Context injected with ~1500 tokens of memory]              โ”‚
โ”‚                                                              โ”‚
โ”‚  ... working ...                                             โ”‚
โ”‚                                                              โ”‚
โ”‚  PostToolUse โ”€โ”€โ†’ memory_extractor.py โ”€โ”€โ†’ Save to L1 + L2     โ”‚
โ”‚       โ”‚                                                      โ”‚
โ”‚  PreCompact โ”€โ”€โ†’ pre_compact.py โ”€โ”€โ†’ Save session state        โ”‚
โ”‚       โ”‚                                                      โ”‚
โ”‚  (State auto-persisted via PreCompact for next session)      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Three Layers, Three Purposes

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              Session Start                       โ”‚
โ”‚                                                  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚ Layer 1   โ”‚  โ”‚ Layer 2   โ”‚  โ”‚ Layer 3       โ”‚  โ”‚
โ”‚  โ”‚ JSON      โ”‚  โ”‚ Markdown  โ”‚  โ”‚ Markdown      โ”‚  โ”‚
โ”‚  โ”‚           โ”‚  โ”‚           โ”‚  โ”‚               โ”‚  โ”‚
โ”‚  โ”‚ Knowledge โ”‚  โ”‚ Daily     โ”‚  โ”‚ Tacit         โ”‚  โ”‚
โ”‚  โ”‚ Graph     โ”‚  โ”‚ Notes     โ”‚  โ”‚ Knowledge     โ”‚  โ”‚
โ”‚  โ”‚           โ”‚  โ”‚           โ”‚  โ”‚               โ”‚  โ”‚
โ”‚  โ”‚ Last 10   โ”‚  โ”‚ Last 3    โ”‚  โ”‚ Full file     โ”‚  โ”‚
โ”‚  โ”‚ active    โ”‚  โ”‚ days      โ”‚  โ”‚               โ”‚  โ”‚
โ”‚  โ”‚ per topic โ”‚  โ”‚           โ”‚  โ”‚               โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚        โ”‚              โ”‚              โ”‚           โ”‚
โ”‚        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜           โ”‚
โ”‚                       โ”‚                          โ”‚
โ”‚         ~1500 tokens per topic                   โ”‚
โ”‚         (<1% of 200K context)                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Layer 1: Knowledge Graph (JSON)

Structured facts with lifecycle management.

{
  "id": "fact-20260227143022",
  "fact": "Use pnpm instead of npm for this project",
  "timestamp": "2026-02-27",
  "status": "active"
}
  • Stored in .claude/memory/areas/topics/<topic>/items.json
  • Each fact has a status field: active or superseded
  • Only active items are loaded at session start
  • Auto-extracted by PostToolUse hook

Layer 2: Daily Notes (Markdown)

What happened today โ€” auto-appended, never edited.

# 2026-02-27

## 14:30
Refactored auth module to use JWT instead of sessions
Topics: authentication, refactoring

## 16:45
Fixed N+1 query in user list endpoint
Topics: performance, postgres
  • Stored in .claude/memory/memory/YYYY-MM-DD.md
  • Last 3 days loaded at session start
  • Provides temporal context ("what was I doing yesterday?")

Layer 3: Tacit Knowledge (Markdown)

Hard-won experience that can't be auto-extracted.

## Architecture Decisions
- Chose FastAPI over Flask for async support

## Gotchas & Pitfalls
- Redis connection pool must be closed explicitly on shutdown
- Never use `datetime.now()` in tests, always mock it

## Tool Preferences
- Use ruff instead of flake8+black
  • Stored in .claude/memory/MEMORY.md
  • Manually maintained (similar to official CLAUDE.md)
  • Full file loaded at session start

๐Ÿ”ง Hook Registration

The installer auto-registers hooks in .claude/settings.json. If you need to add them manually:

{
  "hooks": {
    "SessionStart": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "python .claude/hooks/memory_loader.py"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "python .claude/hooks/memory_extractor.py"
          }
        ]
      }
    ],
    "PreCompact": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "python .claude/hooks/pre_compact.py"
          }
        ]
      }
    ]
  }
}

๐Ÿ› ๏ธ Commands

/memory-review

Periodic knowledge hygiene โ€” find patterns worth promoting, and outdated entries worth cleaning up.

  • Scans all 3 layers for recurring patterns (high-frequency, trending, decaying)
  • Suggests promoting patterns to CLAUDE.md as permanent rules
  • Suggests marking outdated entries as superseded
  • Both "add" and "remove" โ€” keeps your knowledge base lean

/memory-status

Show memory system statistics โ€” item counts, date ranges, token estimates.


๐ŸŽ›๏ธ Configuration

Environment Variables

Variable Default Description
MEMORY_DIR .claude/memory/ Override memory directory path
MEMORY_MAX_ITEMS 10 Max active items loaded per topic
MEMORY_DAILY_DAYS 3 Days of daily notes to load
MEMORY_TOPICS (auto-detect) Custom topic mapping (key:topic,key:topic)

Custom Topics

Set MEMORY_TOPICS to define custom keyword-to-topic mappings:

export MEMORY_TOPICS="fastapi:backend,react:frontend,stripe:payments"

โ™ป๏ธ Lifecycle Management

The key feature official auto-memory doesn't have: knowledge can expire.

Active โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Used normally, loaded at session start
   โ”‚
   โ–ผ
Superseded โ”€โ”€โ”€โ”€ Outdated, NOT loaded, kept for reference

To mark a fact as outdated, change its status in items.json:

{
  "id": "fact-20260101",
  "fact": "Use Flask for the API server",
  "status": "superseded"
}

Or use /memory-review to auto-detect and clean up outdated entries.


๐Ÿ“ˆ Token Budget Analysis

The system is designed to stay under ~1500 tokens at session start (single topic):

Component Estimated Tokens
Layer 3 (MEMORY.md, typical) ~300
Layer 2 (3 days x ~3 entries) ~400
Layer 1 (10 facts per topic x ~15 words) ~600
Headers and formatting ~100
Total (single topic) ~1400

Multiple topics scale linearly. Configure via MEMORY_MAX_ITEMS env var.

This is <1% of Claude's 200K context window.


โœ… Compatibility

  • Claude Code: v2.1.0+ (hooks support required)
  • Python: 3.8+ (stdlib only, zero dependencies)
  • OS: macOS, Linux, Windows
  • Coexists with: Official auto-memory (v2.1.59+), CLAUDE.md

๐Ÿ“ Project Structure

claude-memory-3layer/
โ”œโ”€โ”€ SKILL.md              # Claude Code Skill manifest
โ”œโ”€โ”€ README.md             # English documentation
โ”œโ”€โ”€ README_CN.md          # Chinese documentation
โ”œโ”€โ”€ LICENSE               # MIT
โ”œโ”€โ”€ install.sh            # Unix/macOS installer
โ”œโ”€โ”€ install.ps1           # Windows installer
โ”œโ”€โ”€ images/               # Images and QR codes
โ”œโ”€โ”€ hooks/
โ”‚   โ”œโ”€โ”€ memory_loader.py  # SessionStart: load 3 layers
โ”‚   โ”œโ”€โ”€ memory_extractor.py # PostToolUse: extract knowledge
โ”‚   โ”œโ”€โ”€ session_state.py  # Session lifecycle state
โ”‚   โ””โ”€โ”€ pre_compact.py    # PreCompact: save before compression
โ”œโ”€โ”€ commands/
โ”‚   โ”œโ”€โ”€ memory-review.md  # /memory-review command
โ”‚   โ””โ”€โ”€ memory-status.md  # /memory-status command
โ”œโ”€โ”€ templates/
โ”‚   โ”œโ”€โ”€ MEMORY.md         # Layer 3 template
โ”‚   โ””โ”€โ”€ items.json        # Layer 1 template
โ””โ”€โ”€ docs/
    โ”œโ”€โ”€ quickstart.md     # Getting started guide
    โ”œโ”€โ”€ architecture.md   # System architecture deep-dive
    โ””โ”€โ”€ vs-official.md    # Detailed comparison with official

๐Ÿ“Š Project Statistics

Metric Value
Hook Scripts 3 (SessionStart, PostToolUse, PreCompact) + 1 utility (session_state)
Memory Layers 3 (Knowledge Graph + Daily Notes + Tacit Knowledge)
Commands 2 (/memory-review, /memory-status)
Dependencies 0 (pure Python stdlib)
Token Budget ~1500 per topic (configurable)
Python Version 3.8+
Claude Code Version v2.1.0+ (tested on v2.1.59)

๐Ÿ‘ฅ Target Users

  • โœ… Claude Code power users โ€” Want structured, persistent memory across sessions
  • โœ… Team developers โ€” Need git-trackable, shared knowledge base
  • โœ… Token-conscious users โ€” Want precise control over context window usage
  • โœ… Multi-project developers โ€” Need per-project knowledge management
  • โœ… DIY enthusiasts โ€” Want full control over what Claude remembers

๐Ÿค Contributing

Issues and PRs welcome! Please:

  1. Keep zero-dependency policy (Python stdlib only)
  2. Test on macOS + Windows
  3. Follow existing code style

๐Ÿ“„ License

This project is licensed under the MIT License.


๐Ÿ™ Acknowledgments

  • Anthropic for building Claude Code
  • The Claude Code community for feedback and ideas
  • Everyone who contributed to the official auto-memory discussion

๐Ÿ‘ค Author

KimYx0207 (่€้‡‘ / LaojinAI) โ€” Building tools for Claude Code power users.

๐ŸŒ aiking.dev | ๐Ÿฆ @KimYx0207 | WeChat: ่€้‡‘ๅธฆไฝ ็ŽฉAI


๐Ÿ“ Changelog

v1.0.0 (2026-02-27) โ€” Initial Release

  • Three-layer memory architecture (Knowledge Graph + Daily Notes + Tacit Knowledge)
  • 3 lifecycle hooks (SessionStart, PostToolUse, PreCompact) + 1 utility module (session_state)
  • One-command installer for macOS/Linux/Windows
  • /memory-review and /memory-status commands
  • Complementary design with official auto-memory (v2.1.59+)
  • Zero dependencies (pure Python stdlib)
  • Full documentation (quickstart, architecture, comparison)

About

๐Ÿง  Claude Code ไธ‰ๅฑ‚่ฎฐๅฟ†็ณป็ปŸ | ็ป“ๆž„ๅŒ–ๅญ˜ๅ‚จ + ็”Ÿๅ‘ฝๅ‘จๆœŸ็ฎก็† + Git่ฟฝ่ธช | ๅฎ˜ๆ–นAuto Memory็š„็ฎก็†ๅฑ‚ | ไธ€่กŒๅ‘ฝไปคๅฎ‰่ฃ… ้›ถไพ่ต–

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors