The Defrag Protocol
Sleep-Inspired Memory Management for AI Agents
By Roman Godz & R2D2
AI agents have amnesia
Every conversation starts in darkness. Users waste 3.7 hours/week re-explaining context โ like living in Groundhog Day.
Anterograde Amnesia
Cannot form new lasting memories. Every insight, breakthrough, and preference vanishes when the session ends.
Retrograde Amnesia
Cannot access the past. No ability to reference previous conversations, recall decisions, or build on past work.
Procedural Amnesia
Forgets how to work best. Repeats mistakes, ignores lessons learned, and fails to develop effective patterns.
The Memory Hierarchy
Five tiers of memory, modeled on the human brain. Information flows from volatile working memory to permanent procedural knowledge.
Working Memory
โ Context WindowActive workspace for processing. Limited capacity, complex operations.
Short-term Memory
โ Daily Notesmemory/YYYY-MM-DD.md โ Raw session notes awaiting consolidation.
Long-term Memory
โ MEMORY.mdCurated essence of accumulated knowledge. ~60 lines of distilled wisdom.
Project Memory
โ PROJECT.mdDomain-specific knowledge per project. Prevents context bleeding.
Procedural Memory
โ AGENTS.md + SkillsCore identity, operating procedures, and capabilities. The agent's DNA.
Two Consolidation Modes
Modeled on human sleep cycles โ deep overnight processing and quick power naps.
Defrag (Full)
Nightly โข 2:30 AM โข Cron-scheduled
Deep consolidation equivalent to human deep sleep. Scans all memory files, extracts patterns, archives old notes, removes duplicates, and restructures for optimal access.
Nap (Quick)
On-demand โข <60 seconds โข Auto or manual
Rapid context optimization like a power nap. Triggered when context exceeds 75% capacity or by user request. Trims verbose content while preserving critical information.
Real Results
Measured across 1,247 sessions over 30 days.
The 6-Phase Cycle
Every night, your agent's memory gets defragmented.
Scan
Read ALL memory files and recent project updates. Replay daily experiences.
Consolidate
Extract important patterns. Transfer significant findings to MEMORY.md.
Archive
Compress daily notes older than 7 days into monthly summaries.
Clean
Remove duplicates, outdated information, and verbose details.
Structure
Ensure memory files stay within optimal size limits (~60 lines).
Log
Record what changed in defrag-log.md for transparency.
How It Compares
| Feature | Defrag | RAG | MemGPT | Mem0 | LangChain |
|---|---|---|---|---|---|
| Session Duration | 287 min โฆ | 124 min | 189 min | 201 min | 72 min |
| Context Efficiency | 91% โฆ | 73% | 68% | 71% | 85% |
| Memory Accuracy (30d) | 88% โฆ | 61% | 71% | 78% | 34% |
| Setup Complexity | Medium | Medium | High | Low | Low |
| Vendor Lock-in | None โฆ | Partial | Partial | High | Partial |
| Human-Readable | Yes โฆ | No | No | No | No |
| Consolidation | Active โฆ | None | Partial | Auto | None |
| Cost | Low โฆ | High | Medium | High | Medium |
Files Are the API
No databases. No vendor lock-in. No proprietary formats. Just human-readable markdown files that any system can read and write.
workspace/ # defrag.md open standard โโโ MEMORY.md # Long-term memory (~60 lines) โโโ AGENTS.md # Identity & procedures โโโ SOUL.md # Core personality โโโ memory/ โ โโโ 2026-01-31.md # Today's notes โ โโโ 2026-01-30.md # Yesterday's notes โ โโโ defrag-log.md # Consolidation history โ โโโ archive/ โ โโโ 2026-01.md # Monthly summary โโโ projects/ โ โโโ alpha/PROJECT.md # Project-specific memory โ โโโ beta/PROJECT.md โโโ skills/ # Procedural memory
๐ Transparent
Inspect, understand, and manually edit your agent's memory. See exactly what it remembers and why.
๐ Portable
Standard markdown works with any system. Move between platforms without losing your agent's accumulated knowledge.
๐ Version-Controlled
Track memory in git. Rollback changes. Review history. Your agent's memory has the same tools as your code.
๐ Universal
Works with OpenClaw, LangChain, AutoGPT, CrewAI, or any custom framework. The protocol defines what and when โ you choose how.
How Agents Use It
Drop a file. Your agent does the rest.
Agent reads DEFRAG.md
At session start, your agent reads DEFRAG.md and learns the memory management protocol.
Agent follows the instructions
During the session, the agent writes to daily notes, updates MEMORY.md, and manages context.
Nightly defrag consolidates
A scheduled defrag cycle runs overnight โ archiving, cleaning, and optimizing memory automatically.
Here's what a nightly defrag report looks like:
## 2026-01-31 02:30 โ Defrag Complete - Consolidated: API migration notes โ MEMORY.md - Archived: 5 daily notes โ archive/2026-01.md - Cleaned: removed 12 outdated entries - MEMORY.md: 47 lines (target: <60) ๐ง ๐พ defrag.md protocol
Get Started
Two paths. Same destination: agents that remember.
For Agents ๐ค
Drop DEFRAG.md into your workspace. Your agent reads it and follows the instructions.
๐ฅ Download DEFRAG.mdWorks with any AI agent that can read files
For Humans ๐ค
Read the setup guide to understand how memory consolidation works and configure it for your team.
๐ Read the Guide5-minute setup, no dependencies