Skip to content
rem-sleep logo

REM Sleep - Memory Consolidation for AI Agents

rem-sleep

Memory consolidation and defragmentation for long-term memory maintenance. Use when asked to consolidate memories, defrag memory, run REM sleep, clean up memory files, or process session logs into durable memory. Also use periodically during heartbeats for memory maintenance.

SKILL.md

Full skill instructions

REM Sleep - Memory Consolidation for AI Agents

Like biological REM sleep, this skill processes raw experience (session logs) into consolidated long-term memory.

Works with: OpenClaw, Claude Code, or any agent with session logs and memory files.

The Problem

  • Session logs accumulate but are expensive to re-read
  • Important insights get buried in noise
  • "Mental notes" don't survive context compaction
  • After a restart, you're starting from scratch unless you wrote it down

The Solution

Periodic "sleep cycles" that:

  1. Search session logs for significant patterns
  2. Extract what's worth remembering
  3. Consolidate into durable memory files

Modes

1. Consolidate

Process recent session logs → extract significant events → update MEMORY.md

2. Defrag

Review MEMORY.md → remove stale/​outdated entries → merge duplicates → compress

3. Full

Run both consolidate then defrag.


Consolidation Workflow

Step 1: Gather Recent Sessions

Option A: Using grep/​jq (no extra software)

# OpenClaw session logs location
SESSIONS_DIR="$HOME/​.openclaw/​agents/​main/​sessions"

# Search for patterns in recent sessions
grep -r "decision\|learned\|important\|remember\|TODO" "$SESSIONS_DIR" --include="*.jsonl" | head -100

# Parse JSONL and search content
find "$SESSIONS_DIR" -name "*.jsonl" -mtime -3 -exec cat {} \; | \
  jq -r 'select(.content) | .content' 2>/​dev/​null | \
  grep -i "decision\|learned\|important"

Option B: Using Repo Prompt (if installed)

# More powerful semantic search
rp -e 'search "decision" --context-lines 2'
rp -e 'search "learned" --context-lines 2'
rp -e 'search "important" --context-lines 2'

Option C: Using memory_search (OpenClaw built-in)

If your agent has the memory_search tool, use it to semantically search memory files:

memory_search("decisions made this week")
memory_search("lessons learned")

Step 2: Identify Consolidation Candidates

From search results, look for:

  • Decisions made — choices, preferences, conclusions
  • Facts learned — new info about people, projects, systems
  • Lessons — things that worked/​didn't, mistakes to avoid
  • TODOs/​commitments — things promised or planned
  • Relationship context — interactions with people, their preferences

Step 3: Update Memory Files

Two-tier system:

  1. Daily file (memory/​YYYY-MM-DD.md): Raw events, specific details
  2. MEMORY.md: Distilled, durable knowledge worth keeping long-term

Consolidation prompt:

Review these session excerpts. Extract significant information that should be remembered long-term. Focus on: decisions, facts about people/​projects, lessons learned, and preferences. Format as bullet points suitable for MEMORY.md.


Defrag Workflow

Step 1: Analyze Current Memory

Read MEMORY.md and identify:

  • Stale entries — outdated info, completed TODOs, old dates
  • Duplicates — same info repeated in different sections
  • Inconsistencies — conflicting information
  • Bloat — overly verbose entries that could be compressed

Step 2: Categorize Issues

STALE: [entry] — reason it's outdated
DUPLICATE: [entry A] ≈ [entry B]
INCONSISTENT: [entry A] vs [entry B]
BLOAT: [verbose entry] → [compressed version]

Step 3: Apply Fixes

  • Remove stale entries (or move to an archive section if uncertain)
  • Merge duplicates into single authoritative entry
  • Resolve inconsistencies (check session logs if needed)
  • Compress verbose entries

Step 4: Reorganize

Ensure MEMORY.md has logical sections:

  • About [User]
  • My Setup
  • Projects
  • People
  • Preferences
  • Lessons Learned

Scheduling

Recommended cadence:

  • Consolidate: Every few days, or after busy periods
  • Defrag: Weekly or bi-weekly
  • Full: Monthly deep clean

Trigger options:

  • Manually: "Run REM sleep" / "Consolidate my memories"
  • Heartbeat: Add to HEARTBEAT.md for periodic runs
  • Cron: Schedule isolated job for off-hours

Quick Reference

# Native search (no dependencies)
grep -r "pattern" ~/​.openclaw/​agents/​main/​sessions --include="*.jsonl"

# With Repo Prompt
rp -e 'search "PATTERN" --context-lines 2'

# Helper script (if using Repo Prompt)
./​scripts/​gather-sessions.sh [days_back]

File Structure

rem-sleep/
├── SKILL.md          # This file
├── README.md         # GitHub readme
└── scripts/
    └── gather-sessions.sh   # Helper script (requires Repo Prompt)

Notes

  • Session logs are JSONL format — content is wrapped in JSON
  • When uncertain if something is stale, keep it (conservative approach)
  • MEMORY.md is loaded in main sessions — keep it focused and relevant
  • The skill is a workflow, not a binary — adapt to your setup

Contributing

PRs welcome! Ideas for improvement:

  • Better heuristics for "what's worth remembering"
  • Alternative search methods
  • Automation scripts for different platforms
  • Integration with vector DBs for semantic search

GitHub: https://github.com/stewnight/rem-sleep-skill

More Productivity & Planning skills

brainstorming logo
Productivity & Planning

brainstorming

Structured design dialogue that validates ideas before implementation begins.

295.4K 385.7K
View
ui-ux-pro-max logo
Productivity & Planning

ui-ux-pro-max

Comprehensive design intelligence for web and mobile UI/UX across 10 technology stacks.

133.1K 383.9K
View
writing-plans logo
Productivity & Planning

writing-plans

Comprehensive implementation plans for multi-step tasks, breaking down specs into bite-sized, testable steps.

295.4K 268K
View
using-superpowers logo
Productivity & Planning

using-superpowers

Introduction to the obra skills system with mandatory skill invocation rules and best practices.

295.4K 259.8K
View
executing-plans logo
Productivity & Planning

executing-plans

Execute a written implementation plan with critical review and task checkpoints.

295.4K 229.1K
View
dispatching-parallel-agents logo
Productivity & Planning

dispatching-parallel-agents

Delegate independent tasks to specialized agents working concurrently with isolated context.

295.4K 206.3K
View
using-git-worktrees logo
Productivity & Planning

using-git-worktrees

Isolated git worktrees with smart directory selection and safety verification.

295.4K 205.2K
View
webapp-testing logo
Productivity & Planning

webapp-testing

Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.

179.7K 170.6K
View
content-strategy logo
Productivity & Planning

content-strategy

Plan searchable and shareable content that drives traffic, builds authority, and generates leads.

53.3K 150.9K
View
repo-intake-and-plan logo
Productivity & Planning

repo-intake-and-plan

README-first repository scanner that extracts commands and classifies reproduction candidates without executing them.

497 139.6K
View
marketing-ideas logo
Productivity & Planning

marketing-ideas

Brainstorm and prioritize marketing strategies tailored to your SaaS stage, budget, and goals.

53.3K 137.2K
View
site-architecture logo
Productivity & Planning

site-architecture

Plan and optimize your website's page hierarchy, navigation, URL structure, and internal linking.

53.3K 112.8K
View

Productivity AI tools

Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View
SaveDay logo
Productivity

SaveDay

Capture, organize, and utilize your knowledge effortlessly.

Free
View
Any Summary logo
Productivity

Any Summary

Instant Summaries of Audio & Video Interviews with AnySummary

Freemium
View
M
Productivity

Map This

Transform PDFs into engaging mind maps.

Freemium
View
ChatPDF logo
Productivity

ChatPDF

Chat with any PDF instantly

Free
View
I
Productivity

intellisay

Create an optimal daily plan using your voice

Paid
View
A
Productivity

Aurora AI

A productivity platform to centralize organizational knowledge and workflows with contextual AI assistance.

Paid
View
AskYourPDF logo
Productivity

AskYourPDF

AskYourPDF Pricing Plans: Tailored to Your Needs

Free
View