# @code_rams: "I Spent 5 Days Debugging My OpenClaw Agent's Memory"
Source: https://x.com/code_rams/status/2025630269559185648

## The 5 Problems & Solutions

**Day 1: Context loss after long conversations**
→ Memory flush before compaction — write important context to disk before compressor runs.

**Day 2: Search returns garbage**
→ Hybrid search (BM25 + vector + reranker) instead of default SQLite semantic search. Fails on proper nouns and exact phrases otherwise.

**Day 3: Agent finds it but doesn't use it**
→ Explicit retrieval instructions in boot sequence. "Before starting any task, search daily logs + LEARNINGS.md."

**Day 4: Multi-compaction data loss**
→ Context pruning alongside compaction — prune old context after 6 hours while keeping last 3 assistant responses.

**Day 5: System prompt was 28% bloated**
→ Full audit: 11,887 → 8,529 tokens. Deleted unused files, slimmed MEMORY.md, removed unused skills.

## The 10 Rules

1. Only auto-loaded files matter: AGENTS.md, SOUL.md, TOOLS.md, IDENTITY.md, USER.md, HEARTBEAT.md, MEMORY.md
2. Boot sequence at top of AGENTS.md
3. Write discipline > read discipline — log decisions/outcomes to disk
4. Never write to MEMORY.md during tasks — daily logs are raw, MEMORY.md is curated during reviews
5. LEARNINGS.md — every mistake becomes a one-line rule (they compound)
6. Test retrieval, not just storage — plant markers, test across sessions
7. Handover protocol before model switches — dump current state to daily log
8. Run token audits regularly
9. Hybrid search beats pure semantic
10. Compaction isn't the enemy — unwritten context is

## What We Implemented (same day)
1. MEMORY.md diet: 410→30 lines, reference material to docs/reference.md
2. BOOTSTRAP.md deleted
3. LEARNINGS.md created with 15 rules
4. AGENTS.md rewritten with boot sequence at top
5. Marker planted for retrieval testing
