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.
//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:
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
┌────────────────────────────────────────────────────────┐
│ 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.
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.
3. Archival Memory (The Semantic Store)
A vector database containing deep background knowledge, old session summaries, and uploaded files.
//Comparing Memory Implementations: Letta vs. MemGPT vs. Custom DB
| Feature | Letta / MemGPT | Custom Local SQLite + Vector |
|---|---|---|
| Complexity | High. Strict state machines. | Low. Developer has 100% control over query logic. |
| Latency | Medium-High (requires LLM reflection cycles). | Low (direct database query before prompt is compiled). |
| Ecosystem | Strong. Open-source CLI, clean REST server, SDKs. | Manual. Requires writing custom SQLite/pgvector migrations. |
| Tool Integration | Out-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:
-- 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.