BACK_TO_GUIDES //
Memory & StateAdvanced

SAMS and the Persistent Memory Layer: Beyond Stateless LLM Contexts

An engineering breakdown of Semantic Memory Systems (SAMS), how memory graph compilation works, and why RAG alone fails your long-running agents.

BY: ClawDoc Team2026-06-2110 min read

//The 80% Memory Problem

Standard LLM architectures are fundamentally stateless. When you interact with a basic agent, every new session is a blank slate.

Developers try to fix this in two ways:

1.Context Stuffing: Shoving the entire chat history into the context window. (Fails due to context bloat, token cost, and middle-of-the-prompt attention decay).
2.Naive RAG: Querying a vector database for the top-3 most similar messages. (Fails because semantic similarity is not chronological or relational relevance; "What did I decide on yesterday?" does not match "yesterday" semantically).

To build an agent that actually acts as an assistant over months, you need a Semantic Memory System (SAMS). SAMS splits memory into three operational layers: Core Memory (Scratchpad), Recall Memory (Chronological logs), and Archival Memory (Vector store).


//The Three-Layer Memory Architecture

text
┌────────────────────────────────────────────────────────┐
│                      AGENT BRAIN                       │
└───────────────────────────┬────────────────────────────┘
                            │
         ┌──────────────────┼──────────────────┐
         ▼                  ▼                  ▼
┌─────────────────┐┌─────────────────┐┌─────────────────┐
│   CORE MEMORY   ││  RECALL MEMORY  ││ ARCHIVAL MEMORY │
│  (Scratchpad)   ││ (Chronological) ││  (Vector/Graph) │
│                 ││                 ││                 │
│ Size: 2KB-8KB   ││ Size: Infinite  ││ Size: Infinite  │
│ Write: LLM edit ││ Write: Auto-log ││ Write: Vector   │
└─────────────────┘└─────────────────┘└─────────────────┘

1. Core Memory (The Scratchpad)

The agent's immediate workspace. This is a small, highly structured text block injected into every single system prompt. It contains critical facts about the user (name, preferences, current active task) and the agent's active state.

How it works: The agent has a tool called core_memory_append or core_memory_replace. When it learns something vital, it calls the tool to update its scratchpad. The LLM edits its own system prompt dynamically.

2. Recall Memory (The Ledger)

A full SQL-backed chronological history of every message, tool call, and response.

How it works: Used for search and scrolling backwards. When the user asks "What did we do about X last week?", the agent queries the database using full-text search (FTS5) over the recall table instead of semantic vector search.

3. Archival Memory (The Semantic Store)

A vector database containing deep background knowledge, old session summaries, and uploaded files.

How it works: Vector embeddings are generated for segments. The agent dynamically page-swaps these chunks in and out of its context window using a cosine-similarity retrieval tool when a query demands background facts.

//Comparing Memory Implementations: Letta vs. MemGPT vs. Custom DB

FeatureLetta / MemGPTCustom Local SQLite + Vector
ComplexityHigh. Strict state machines.Low. Developer has 100% control over query logic.
LatencyMedium-High (requires LLM reflection cycles).Low (direct database query before prompt is compiled).
EcosystemStrong. Open-source CLI, clean REST server, SDKs.Manual. Requires writing custom SQLite/pgvector migrations.
Tool IntegrationOut-of-the-box system instructions.Manual tool binding (read_db, write_db).

//How to Implement a Clean, Lightweight SAMS

If you want to build a persistent memory layer without importing a massive, opinionated framework like MemGPT, you can write a simple SQLite schema with FTS5 (Full Text Search) and pgvector or sqlite-vss:

sql
-- Schema for a highly robust, self-hosted memory layer
CREATE TABLE recall_memory (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id TEXT NOT NULL,
    role TEXT NOT NULL, -- 'user', 'assistant', 'system', 'tool'
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Enable virtual table for rapid chronological search
CREATE VIRTUAL TABLE recall_search USING fts5(
    content,
    session_id UNINDEXED,
    role UNINDEXED
);

When compilation runs, you fetch the last 10 messages from recall_memory (for conversation flow) and prepend the Core Memory block directly to the system prompt. This ensures your agent is never stateless, never forgets the user's name, and doesn't bleed dollars in bloated context windows.

Need Professional Setup?

Skip the sandbox configuration, container setups, and API troubleshooting. Hire a verified ClawDoc professional agent to securely upgrade and calibrate your custom AI setup.

Explore ClawDoc