Chapter 26

Memory Four-Layer Architecture: Session Context, Daily Logs, MEMORY.md and Vector Index

Chapter 26ใ€€The Four-Layer Memory Architecture: Session Context โ†’ Daily Logs โ†’ MEMORY.md โ†’ Vector Index

"The model only 'remembers' what gets saved to disk โ€” there is no hidden state." โ€” OpenClaw Design Documentation


26.1ใ€€Why Layered Memory?

Large language models are fundamentally stateless. When an inference call completes, all intermediate activations and attention weights evaporate. Without an external persistence mechanism, an agent loses all memory the moment a conversation ends โ€” it forgets what you told it last week, can't recall your name-spelling preferences, and has no way to review decisions made three days ago.

OpenClaw's Memory system resolves this fundamental tension through a four-layer architecture: externalizing "things worth remembering" into persistent files on disk, then reinjecting them into the Context Window at the appropriate moment.

The four layers, from most ephemeral to most durable, are:

Session Context
    โ†“ may be promoted after compaction
Daily Logs
    โ†“ periodically consolidated
MEMORY.md (Long-term Memory)
    โ†“ indexed in parallel
Vector Index (SQLite + Embeddings)

Each layer has a clearly defined storage location, write timing, and read strategy. Understanding these four layers is central to mastering OpenClaw's state management.


26.2ใ€€Layer 1: Session Context

26.2.1ใ€€Storage Location and Format

~/.openclaw/agents/<agentId>/sessions/<sessionId>.jsonl

Each line is a JSON message record following the standard conversation history format:

{"role":"user","content":"Analyze this sales report","timestamp":"2026-04-26T09:00:00Z"}
{"role":"assistant","content":"Sure, please upload the file...","timestamp":"2026-04-26T09:00:02Z"}
{"role":"tool","name":"read_file","result":"...","timestamp":"2026-04-26T09:00:05Z"}

26.2.2ใ€€Loading Timing

Session Context is the most "live" layer โ€” it resides in the Context Window throughout the current conversation. When an agent starts a new session, the corresponding .jsonl file is read in full (if it exists), and each new message turn is appended to the same file.

Key constraint: The Context Window is finite (e.g., 200K tokens). When session history grows too long, Compaction or Pruning must be triggered (see Chapter 27).

26.2.3ใ€€Information Types Best Suited for This Layer

Information Type Example Lifetime
In-progress task state "Just finished page 3, continuing to page 4" Current session only
Tool call results File contents, API responses Current session only
Instructions given this turn "Use formal tone today" Current session only
Temporary variables / drafts Code snippets, calculation intermediates Current session only

Session Context is a high-density, short-lifetime information layer. Content is rich but fleeting; once the session ends, anything not explicitly persisted is gone forever.


26.3ใ€€Layer 2: Daily Logs

26.3.1ใ€€Storage Location and Format

~/.openclaw/workspace/<workspaceId>/memory/YYYY-MM-DD.md

For example:

~/.openclaw/workspace/myproject/memory/2026-04-26.md
~/.openclaw/workspace/myproject/memory/2026-04-25.md

The format is plain Markdown, with content structure left to the agent's discretion. Entries typically appear as timestamped append records:

## 2026-04-26

### 09:15 โ€” Sales Report Analysis

User uploaded Q1-2026-sales.xlsx. East China region grew 23% YoY. User wants
to prepare a PPT presentation next week.

### 14:30 โ€” Code Review

Reviewed PR #142 on auth-service. Found a potential SQL injection point;
notified the user.

26.3.2ใ€€Loading Timing

Every time a session starts, OpenClaw automatically loads the today's and yesterday's log files (if they exist). This ensures that at the start of a new session, the agent is aware of significant events from the past 48 hours without requiring the user to re-explain context.

# Loading policy in config (default)
dailyLogDays: 2   # load today + yesterday

26.3.3ใ€€Write Timing

Daily Logs are written in two situations:

  1. Compaction Pre-flush: When the Context Window is about to fill, the agent writes important information to the daily log before compression (see Chapter 27 for details).
  2. Session-end hook: When a session closes normally, the agent may optionally summarize key takeaways into the log.

26.3.4ใ€€Information Types Best Suited for This Layer

Information Type Example
Tasks completed today "Finished the database migration script"
Important issues discovered "Found a memory leak in the production environment"
Changes in user preferences "User prefers more concise output today"
Progress on multi-session tasks "Chapters 4 and 5 still pending"

Daily Logs form a medium-density, periodic-lifecycle information layer โ€” a buffer zone between Session Context and MEMORY.md.


26.4ใ€€Layer 3: Long-term Memory (MEMORY.md)

26.4.1ใ€€Storage Location

~/.openclaw/workspace/<workspaceId>/MEMORY.md

This is a single, curated Markdown file that stores core knowledge that spans time.

26.4.2ใ€€Content Example

# Memory Index

- [User Profile](user_profile.md) โ€” Prefers concise, direct answers; dislikes excessive formalities
- [Architecture Decisions](arch_decisions.md) โ€” Microservices architecture; gRPC for inter-service communication
- [Coding Standards](coding_standards.md) โ€” TypeScript strict mode; no `any` type
- [Key Contacts](contacts.md) โ€” Tech lead: Zhang San ([email protected])

MEMORY.md typically serves as an index file pointing to more detailed sub-files; it can also be a compact inline summary.

26.4.3ใ€€Loading Timing

MEMORY.md is automatically loaded only in Primary Private Sessions. This design is intentional:

# Session types and MEMORY.md loading rules
session:
  primary_private:
    load_memory: true
  sub_session:
    load_memory: false
    inherit_context: minimal
  group_session:
    load_memory: false
    load_shared_context: true

26.4.4ใ€€Write Timing

Updates to MEMORY.md are deliberate and selective, occurring when:

  1. Dreaming process: A background consolidation process periodically promotes important information from Daily Logs into MEMORY.md
  2. Explicit agent decision: The agent determines a piece of information has long-term value and writes it proactively
  3. User instruction: The user explicitly says "remember this"

26.4.5ใ€€Information Types Best Suited for This Layer

Information Type Example Update Frequency
User identity information Name, role, team Very rarely
Long-term preferences Language style, output format Occasionally
Core project decisions Technology choices, architecture decisions Occasionally
Important constraints Security rules, prohibited actions Very rarely
Domain knowledge summaries Key nodes of company business processes As project evolves

26.5ใ€€Layer 4: Vector Index

26.5.1ใ€€Storage Location

~/.openclaw/memory/<agentId>.sqlite

The SQLite database contains two key components:

-- BM25 full-text search virtual table
CREATE VIRTUAL TABLE memory_fts USING fts5(content, ...);

-- Vector storage table (sqlite-vec extension)
CREATE TABLE memory_embeddings (
    id INTEGER PRIMARY KEY,
    content TEXT,
    embedding FLOAT[768],  -- dimension depends on embedding model
    source_file TEXT,
    created_at INTEGER
);

26.5.2ใ€€Loading Timing

The Vector Index is never "loaded" into the Context Window โ€” it is an on-demand query layer. When an agent needs to recall certain information, it issues a semantic search query; the retrieved results are injected into the context as text fragments.

User question โ†’ Agent judges retrieval needed โ†’ Vector query โ†’ Top-K chunks returned โ†’ Injected into Context โ†’ Response generated

26.5.3ใ€€Content Sources

The Vector Index indexes content from the other three layers: Daily Logs, MEMORY.md, and fragments from historical sessions that are flagged as worth retrieving. It is a derived layer โ€” all its content originates from upper-layer files and can be rebuilt at any time through re-indexing.

# Rebuild vector index (no original information is lost)
openclaw memory rebuild-index --workspace myproject

26.5.4ใ€€Information Types Best Suited for This Layer

Retrieval Scenario Example Query
Historical decision review "Why did we choose PostgreSQL?"
Similar problem reference "What was our approach to this type of bug last time?"
Document semantic search "Find all discussions about the authentication flow"
Long-term knowledge accumulation "Is there any historical record about performance optimization?"

26.6ใ€€Files as Database: The Design Philosophy in Depth

26.6.1ใ€€Why Markdown Instead of a Database?

OpenClaw made a distinctive technical choice: Markdown files rather than a relational database or proprietary format as the primary storage. The reasoning:

1. Human Readability

# Anyone can inspect agent memory with standard tools
cat ~/.openclaw/workspace/myproject/MEMORY.md
grep "database" ~/.openclaw/workspace/myproject/memory/2026-04-26.md

By contrast, SQLite binary files and proprietary vector database formats require special tools to read.

2. Version Control Friendly

# Memory can be placed under Git version control
cd ~/.openclaw/workspace/myproject
git init
git add memory/ MEMORY.md
git commit -m "Agent memory snapshot: 2026-04-26"

Every change to MEMORY.md can be tracked, rolled back, and diffed.

3. Human Editable

Users can open Markdown files directly, manually correct wrong memories, delete outdated entries, or inject external knowledge. This transparency is impossible with closed-source memory systems.

4. No Hidden State

"The model only 'remembers' what gets saved to disk โ€” there is no hidden state."

This design declaration means: all of an agent's "memory" is visible on the filesystem. There is no mysterious internal state โ€” what you see is everything it knows.

26.6.2ใ€€SQLite's Role as an Acceleration Layer

SQLite (the Vector Index layer) is the only "non-human-readable" store in the entire architecture, and its role is that of an acceleration layer rather than primary storage:

Markdown files (authoritative source)
    โ†“ async indexing
SQLite (retrieval acceleration layer)
    โ†“ can be rebuilt at any time

The vectors and BM25 indexes in SQLite are entirely derived from Markdown files. If SQLite is corrupted or deleted:

openclaw memory rebuild-index --workspace myproject
# Re-reads all Markdown files and rebuilds the index
# No information is lost

This design ensures Markdown is the Single Source of Truth, and SQLite only makes search faster.


26.7ใ€€Memory and the Context Window

The Context Window is the total information an LLM actually "sees" during each inference call. The core mission of the Memory system is deciding which information is worth occupying precious Context space.

Context Window (200K token example)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ System Prompt (AGENTS.md / SOUL.md)     ~8K  โ”‚
โ”‚ MEMORY.md (if primary session)          ~4K  โ”‚
โ”‚ Daily Logs (today + yesterday)          ~8K  โ”‚
โ”‚ Retrieved Memory Chunks (vector search) ~4K  โ”‚
โ”‚ Current Session History                 rest  โ”‚
โ”‚ Available for new messages             ~176K  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

As session history grows and available space shrinks, the Compaction mechanism intervenes (see Chapter 27).


26.8ใ€€Memory Loading Differences Across Session Types

Session Type Session Context Daily Logs MEMORY.md Vector Index
Primary Private Session Full load Today + Yesterday Full load On-demand retrieval
Sub-session Current task context Not loaded Not loaded Restricted retrieval
Group Session Shared group history Not loaded Not loaded Shared workspace retrieval
Read-only Sandbox Session Current context Read-only Read-only Read-only retrieval

Sub-session design follows the principle of minimal context: it receives only the minimum information needed to complete the current sub-task. This avoids polluting the task context with unrelated historical information and also reduces token consumption.


26.9ใ€€Practical Guide: Which Information Belongs in Which Layer

Decision Tree

How long does this information need to be retained?
โ”œโ”€โ”€ Only today / this conversation โ†’ Session Context (auto-managed, no action needed)
โ”œโ”€โ”€ Need to recall within a few days โ†’ Daily Logs (auto-written during Compaction)
โ”œโ”€โ”€ Keep permanently, needed in every session โ†’ MEMORY.md (explicit write or Dreaming promotion)
โ””โ”€โ”€ Need to find via semantic search โ†’ Vector Index (auto-indexed, no action needed)

Layer Selection Principles

Principle Explanation
The more durable, the more concise MEMORY.md should store only core knowledge; avoid accumulation
Don't manually manage Session Context It is automatically maintained by the system
Daily Logs tolerate redundancy Stream-of-consciousness records are fine; Dreaming will consolidate
Vector Index is automatic No need to manually decide "whether to index"; the system handles it
Review MEMORY.md periodically Outdated information occupies Context; clean it up regularly

Common Mistakes

โŒ Wrong: Writing every conversation summary into MEMORY.md
   โ†’ Result: MEMORY.md balloons to 50K tokens, consuming enormous Context each time

โœ“ Correct: Only write "decisions that affect long-term agent behavior" into MEMORY.md
   โ†’ Result: MEMORY.md stays at 2-4K tokens, highly refined
โŒ Wrong: Relying on Session Context to preserve important decisions (no persistence)
   โ†’ Result: After the session ends, critical decisions are lost forever

โœ“ Correct: At session end, trigger a Memory Flush to write key points to Daily Logs

26.10ใ€€Chapter Summary

OpenClaw's four-layer Memory architecture is a systematic solution to the fundamental constraint of "LLM statelessness":

  1. Session Context โ€” Working memory for the current conversation; auto-managed
  2. Daily Logs โ€” Buffer for recent memory; append-written, auto-loaded
  3. MEMORY.md โ€” Curated long-term memory; maintained carefully, loaded in primary sessions
  4. Vector Index โ€” Semantic retrieval acceleration layer; derived, rebuildable

The "files as database" philosophy runs throughout: Markdown is the authoritative source, and SQLite is merely a derived layer that accelerates retrieval. This design makes the agent's memory system fully transparent and fully controllable โ€” no hidden state of any kind.

In the next chapter, we dive into the Compaction mechanism โ€” how the system performs "memory compression" when the Context Window approaches its limit without losing critical information.


Next: Chapter 27 โ€” Compaction Algorithm: Trigger Formula, Pre-flush Mechanism, and Long-session Information Preservation

Rate this chapter
4.6  / 5  (5 ratings)

๐Ÿ’ฌ Comments