OpenClaw Memory
Stop the Amnesia
Understanding how memory actually works
Meta's Alignment Director Lost Control
Summer Yue (Meta's Director of Alignment) had to physically run to her computer to stop her OpenClaw agent from bulk deleting hundreds of emails.
What Happened
- She told the agent: "Don't do anything until I say so"
- Agent worked fine on test inbox for weeks
- Pointed it at real inbox with thousands of messages
- Context window filled up → compaction fired
- That instruction (given in chat, not saved to file) disappeared from the summary
- Agent went autonomous and started deleting emails
The Lesson
If it's not written to a file, it doesn't exist.
Safety rules given in chat don't survive long sessions.
If Meta's Director of Alignment can lose control, so can you.
Understanding Memory
Most people think of memory as one thing.
It's actually 4 different systems.
Layer 1: Bootstrap Files
SOUL.md, AGENTS.md, USER.md, MEMORY.md, TOOLS.md
- ✓ Loaded at session start
- ✓ Survive compaction (reloaded from disk)
- ✓ Most durable layer
Layer 2: Session Transcript
Every conversation saved as a file on disk
- Rebuilt into context when you continue a session
- ⚠ Gets compacted when context fills
- ✗ Model loses access to original messages after compaction
Layer 3: LLM Context Window
Fixed-size container (e.g., 200K tokens)
Everything competes for space:
- System prompt
- Workspace files
- Conversation history
- Tool calls & results
When it fills → compaction fires
Layer 4: Retrieval Index
Searchable layer beside your memory files
- Agent queries with
memory_search tool
- Finds relevant context from past sessions
- ⚠ Only works if info was written to files first
3 Failure Modes
When your agent forgets, it's always one of these:
Failure A: Never Stored
- Instruction/preference/decision only existed in conversation
- Never written to a file
- When compaction fires or new session starts → gone
This is what happened to Summer Yue
Failure B: Compaction Changed Context
Long session hits token limit → compaction summarizes old messages
- Summary can be lossy
- Drops important details, nuance, constraints
- Agent operates from summary, not your original words
Failure C: Session Pruning
Tool outputs trimmed to optimize caching
Important: Pruning is actually your friend!
- ✓ Reduces bloat without destroying conversation
- ✓ Only affects tool results, not messages
- Compaction is the dangerous one
Compaction vs Pruning
Compaction
- Summarizes entire conversation
- Changes what model sees
- Affects everything
- ⚠️ Dangerous
Pruning
- Trims old tool results
- In-memory only
- Only affects tool outputs
- ✓ Your friend
Quick Start: 3 Things
Do these three and you're ahead of 95% of OpenClaw users
1. Put Durable Rules in Files
- Not in chat. In
MEMORY.md, AGENTS.md files.
- Those survive compaction
- Instructions typed in conversation? Don't rely on them.
2. Enable Memory Flush
Check that memory flush is enabled with enough buffer:
{
"reserveTokensFloor": 40000,
"memoryFlushEnabled": true,
"softThresholdTokens": 4000
}
Built-in safety net that saves context before compaction
3. Make Retrieval Mandatory
Add to AGENTS.md:
Before acting, search memory first.
Without this rule, agent guesses instead of
checking its notes.
Pre-Compaction Memory Flush
Most important config change
Built-in safety net that triggers before compaction:
- Silent agentic turn
- Reminds model to write important context to disk
- Happens automatically when context nears limit
Key Config Settings
{
"reserveTokensFloor": 40000,
"memoryFlushEnabled": true,
"softThresholdTokens": 4000
}
reserveTokensFloor: 40,000
- Headroom for flush + compaction
- Prevents overflow into "bad path"
How Flush Triggers
Flush fires at:
context_window - reserveTokensFloor - softThreshold
Example with 200K context:
200,000 - 40,000 - 4,000 = 156K tokens
Gives enough room for flush to save context before compaction
Two Compaction Paths
Good Path
- Context nearing limit
- Memory flush fires first
- Saves context to disk
- Compaction summarizes
- Agent continues
Bad Path
- Context overflows
- API rejects request
- No memory flush
- Emergency compaction
- Most context lost
Manual Memory Discipline
Automated flush is a safety net, not a guarantee.
When to save manually:
- Finishing a large task
- Before switching to new work
- After making important decisions
Just tell the agent:
"Save this to MEMORY.md"
"Write today's key decisions to memory"
The /compact Trick
Manual compaction on your terms
Use case: Mid-session, want to keep going
- Context at 123K / 206K (62%)
- Run
/compact
- Context drops to 21K
- Fresh space for new instructions
Can guide it: /compact and focus on decisions
Essential Commands
/context list - Check what's actually loaded
- Is MEMORY.md loading?
- Are files truncated?
- Raw chars = injected chars?
If truncated, it's invisible to the agent
Max per file: 20K chars | Max total: 150K chars
Other Commands
/status - Check model, context window, thinking level
/compact - Manual compaction with optional focus
/new - Start fresh session (use between tasks)
/verbose - Debug memory search
Making Memory Searchable
Memory files are useless if the agent can't find information.
Memory Search Tools
memory_search - Searches MEMORY.md + memory/ directory
Hybrid search: keyword + semantic matching
memory_get - Reads specific memory file directly
The Memory Protocol
Before doing anything non-trivial, search memory first.
Use memory_search to check for:
- Prior decisions
- User preferences
- Past solutions
- Relevant context
Then act based on what you find.
Hybrid Search
Keyword
Finds exact words
Misses: "we picked the $29 tier"
Semantic
Captures meaning
Finds: "pricing decision"
Hybrid = both working together
Retrieval Options
Built-in (default):
- Indexes MEMORY.md + memory/ automatically
- Local embedding model
- Good for most users
QMD Backend (advanced):
- For thousands of files
- Searches entire Obsidian vault, past sessions
- Returns small snippets
Why Memory Saves Money
- Every message includes full system prompt + history
- Prompt caching = 90% discount on repeated tokens
- Compaction invalidates cache
- Next request pays full price to re-cache
Every unnecessary compaction = reliability problem + cost problem
What Breaks Cache
- Compaction - Rewrites conversation history
- Changing prompt inputs - Dynamic content in bootstrap files
Best practice:
- Keep workspace files stable
- Keep MEMORY.md small
- Don't constantly rewrite bootstrap files
Memory Hygiene
Daily:
- Append to daily log (automatic)
- Manual saves for important decisions
Weekly:
- Promote durable rules to MEMORY.md
- Keep MEMORY.md under 100 lines
- Remove outdated info
Backup Your Memories
cd ~/your-workspace
git init
git add .
git commit -m "Initial memory"
- Set up auto-commit (daily cron or heartbeat)
- Full history + diffs
- Roll back as far as you want
Add to .gitignore: .openclaw/, credentials/, *.json
5 Things to Remember
- Files are memory. If it's not written to disk, it doesn't exist.
- Verify the flush. Set reserve tokens floor to 40K.
- Compact proactively. Use /compact mid-session on your terms.
- Search before acting. Add the rule to AGENTS.md.
- Pruning is your friend. Compaction is what hurts.
Remember
If it's not written to a file, it doesn't exist.
Now go save this to a file.
You know what happens if you don't.