Writing everything to MEMORY.md
MEMORY.md loads every session. Keep it under 5K tokens. Put detailed facts in entity memory instead.
Every LLM session starts fresh. Your agent doesnβt remember the meeting you told it about last Tuesday. It doesnβt remember that your boss prefers bullet points. It doesnβt remember that you already tried the approach itβs about to suggest β and it failed.
The solution isnβt a better model. Itβs a better memory system.
The naive approach: dump everything into a massive MEMORY.md and load it every session. This breaks quickly:
The right approach is layered memory β different stores for different types of information, loaded selectively.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ Layer 3: Curated Summaries (always in context) ββ MEMORY.md, entity summaries ββ ~2,000-5,000 tokens | Rewritten weekly ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€β Layer 2: Atomic Facts (queryable on demand) ββ memory/entities/*/items.json ββ Structured JSON | Never deleted ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€β Layer 1: Raw Logs (archive + retrieval) ββ memory/YYYY-MM-DD.md ββ Complete record | Loaded selectively ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| Layer | Files | When Loaded | Purpose |
|---|---|---|---|
| Raw logs | memory/YYYY-MM-DD.md | Session start (today + yesterday) | Complete record of events |
| Atomic facts | memory/entities/*/items.json | On-demand when querying | Structured, queryable facts |
| Summaries | MEMORY.md, entity summary.md | Always (main session only) | Fast context for common questions |
This is how humans actually remember things. You donβt replay every conversation youβve ever had β you have a working mental model (Layer 3), can recall specific facts when relevant (Layer 2), and can dig into detailed memories when needed (Layer 1).
Raw session logs. Everything significant that happened, preserved.
Format:
# 2026-03-27 - Daily Notes
## Session 1 (9:14 AM)
- JD asked about competitor pricing for ProductCo launch- Researched 4 competitors: Acme ($49/mo), Beta ($79/mo), Gamma ($39/mo), Delta ($99/mo)- JD decided to price at $59/mo β above Gamma, below Beta- **Decision:** $59/mo launch price, revisit at 100 customers
## Session 2 (2:30 PM)
- Drafted outreach email to 12 beta users- JD approved draft with minor edits (softened the urgency in para 2)- Emails sent at 3:45 PM- **Follow-up needed:** Check open rates in 48 hours
## Lessons- JD prefers direct pricing (no "starting at" language)- Beta users were acquired from Twitter thread, not paid adsRules for good daily logs:
Loading strategy for sessions:
# In SOUL.md or agent instructions:At session start:1. Read memory/{today}.md if it exists2. Read memory/{yesterday}.md if it exists3. In main sessions only: also read MEMORY.mdLoading today + yesterday gives context without overwhelming the window. Older logs are available via search.
Structured, queryable facts about people, companies, and projects. Unlike daily logs (narrative), entity memory is atomic β one fact per entry, tagged for filtering.
items.json schema:
[ { "id": "sarah-001", "date": "2026-03-15", "category": "preference", "fact": "Sarah prefers async communication. Dislikes being called without notice.", "source": "2026-03-15 session", "confidence": "high", "superseded": false }, { "id": "sarah-002", "date": "2026-03-20", "category": "context", "fact": "Sarah is VP of Engineering at Acme Corp. Reports to CEO Mike Torres.", "source": "LinkedIn + 2026-03-20 session", "confidence": "high", "superseded": false }, { "id": "sarah-003", "date": "2026-02-01", "category": "context", "fact": "Sarah was Director of Engineering at Acme Corp.", "source": "2026-02-01 session", "confidence": "high", "superseded": true, "superseded_by": "sarah-002", "superseded_date": "2026-03-20" }]Key principles:
superseded: true instead. History matters.summary.md β fast retrieval:
# Sarah Chen
**Role:** VP of Engineering at Acme Corp (as of March 2026)**Contact:** sarah@acme.com, LinkedIn: linkedin.com/in/sarah-chen**Working relationship:** JD's main technical contact at Acme
## Key facts- Prefers async; no surprise calls- Direct communicator β skip small talk in emails- Currently leading a major infrastructure migration (Q2 2026)- Budget authority up to $50K without CFO approval
## Recent context- March 2026: Promoted from Director β VP- Evaluating JD's product for Q3 pilot
## Notes- Met at SaaStr 2025, introduced by Mike TorresSummaries are 3-7 sentences covering the most relevant current state. Theyβre rewritten from items.json periodically β theyβre a view of the facts, not the source of truth.
The highest-level layer. Loaded in main sessions only. Contains the distilled essence of everything the agent should always have in mind.
What belongs in MEMORY.md:
# Long-Term Memory
## About JD (my user)- JD Davenport, MBA candidate at BYU (graduating 2027)- Building ProductCo β AI agent platform for SMBs- Wife: Sam. Kids: Ava and John. Provo, Utah.- Budget-conscious; always ask about ROI tradeoffs- Communication style: direct, no filler, facts first
## Active Projects- **ProductCo launch** β Target: May 2026. Status: Beta testing.- **BYU MBA** β Year 2, thesis: AI adoption in SMBs
## Key Decisions (Don't Re-Litigate)- Pricing: $59/mo flat (decided 2026-03-27, revisit at 100 customers)- Stack: Next.js + Supabase + Vercel (locked in, migration cost too high)- Go-to-market: Bottom-up through Twitter community, not enterprise sales
## Lessons Learned- JD wants bottom-line-up-front on everything. Lead with the answer.- Don't suggest things that require >$100/mo new spend without flagging it- JD is in the GTD system; use "capture β clarify β organize" language
## Last Updated: 2026-03-27Rules for MEMORY.md:
The bookend workflow ensures memory is maintained consistently:
1. Read state/current.md β "What was I working on?"2. Read memory/{today}.md and memory/{yesterday}.md3. [Main session only] Read MEMORY.md4. Check for open loops: "follow-up needed" items from recent logs- When a significant decision is made β log it to memory/{today}.md- When you learn a new preference β add to memory/{today}.md AND create/update entity memory item- When a project status changes β update memory/entities/projects/*/items.json- When context is complex β update state/current.md for "future me"1. Update state/current.md with final status and next steps2. If significant new information β update entity summaries3. If major event/decision β add to MEMORY.md4. Commit memory files to git (if using version control)state/current.md β the session bridge:
# Current State β Updated 2026-03-27 3:45 PM
## What I'm Working On- ProductCo landing page redesign (started 2026-03-26)- Draft complete at ~/projects/productco/src/pages/landing.tsx- Waiting on: JD to approve hero copy before deploying
## Open Loops- [ ] Email open rates from beta outreach (check 2026-03-29)- [ ] Sarah Chen follow-up on Q3 pilot (ping 2026-04-01)
## Next Session FocusStart by asking JD about landing page approval.If approved, spawn coding agent to deploy.This file survives session compaction. When the context window fills up and older messages are dropped, state/current.md preserves what matters.
For older memories not in the current context window, use memory_search:
# Search across all memory filesmemory_search "Sarah Chen pricing discussion"# Returns: memory/2026-02-15.md:34 β "Sarah asked about volume discounts"
# Search entity memorymemory_search "acme-corp budget"# Returns: memory/entities/companies/acme-corp/items.json:12 β "$50K budget authority"The retrieval strategy:
memory_search finds relevant snippets across all filesmemory_getDonβt read the entire daily log directory to find one fact. Search first, read specifically.
Create the directory structure
mkdir -p ~/agents/myagent/memory/entities/{people,companies,projects}mkdir -p ~/agents/myagent/statetouch ~/agents/myagent/MEMORY.mdtouch ~/agents/myagent/state/current.mdAdd session start instructions to SOUL.md
## Every Session
Before doing anything else:1. Read `state/current.md` β what was I working on?2. Read `memory/YYYY-MM-DD.md` (today + yesterday)3. If this is a main session with JD: also read `MEMORY.md`
Never skip this. It's how I have continuity.Define the write protocol
## Memory Write Rules
Log to memory/{today}.md when:- A decision is made ("decided to price at $59/mo because...")- A preference is learned ("JD prefers async communication")- A task is completed with notable result- Something needs follow-up
Update entity memory when:- A fact about a person/company/project changes- New important context is learned about someone
Update MEMORY.md weekly or after major events.Write your first MEMORY.md
Start with the most important facts about your user. 20-30 bullets covering: who they are, current projects, key decisions, communication preferences.
Commit memory to git (optional but recommended)
cd ~/agents/myagentgit initgit add .git commit -m "Initial memory setup"# Push to private repo for backupWriting everything to MEMORY.md
MEMORY.md loads every session. Keep it under 5K tokens. Put detailed facts in entity memory instead.
Loading MEMORY.md in sub-agents
Sub-agents donβt need your full life context. They need task context. MEMORY.md in a sub-agent prompt wastes tokens and leaks personal data.
Skipping the session start reads
An agent that doesnβt read its daily log is an agent that forgets what it did this morning. Make it non-negotiable.
Deleting superseded facts
Mark facts as superseded: true, never delete them. History matters β knowing what changed and when is valuable context.
With three-layer memory, your agent:
Memory is what turns a capable tool into a trusted collaborator.
About the author: JD Davenport builds AI agent systems at OpenClaw. Follow on LinkedIn for updates on building AI agents for business.