DEV Community

Daniel Vermillion
Daniel Vermillion

Posted on

Building Persistent Memory for AI Agents: A 4-Layer File-Based Architecture

Building Persistent Memory for AI Agents: A 4-Layer File-Based Architecture

As AI agents become more integrated into our workflows, one persistent challenge remains: memory. Unlike humans, AI agents traditionally lack the ability to retain context across sessions. This creates friction in productivity tools, customer support bots, and automated workflows where continuity matters.

After months of experimentation with different memory architectures, I've developed a 4-layer file-based system that gives AI agents persistent memory across sessions. This architecture works with ChatGPT, Claude, Agent Zero, and local LLMs, providing a practical solution that balances performance with simplicity.

The Problem with Stateless AI

Most AI interactions today are stateless. You ask a question, get an answer, and the context disappears. This works fine for simple queries, but breaks down when:

  • You need to reference previous conversations
  • The agent must maintain state across multiple interactions
  • You're building workflows that require continuity

I experienced this firsthand when building an AI-powered documentation assistant. Users would ask follow-up questions that referenced previous exchanges, but the AI had no memory of them. The solution required a robust memory system that could persist across sessions.

The 4-Layer Memory Architecture

My solution uses a hierarchical file-based system with four distinct layers:

  1. Session Layer - Ephemeral, in-memory storage for current interaction
  2. Short-term Layer - File-based storage for recent interactions (24-48 hours)
  3. Long-term Layer - Structured storage for important information (weeks/months)
  4. Metadata Layer - Indexing and search capabilities across all layers

Layer 1: Session Memory (Ephemeral)

The session layer acts as working memory during an active interaction. It's stored in memory and only persists for the duration of the conversation.

# Example session memory structure
session_memory = {
    "conversation_id": "conv_12345",
    "user_id": "user_67890",
    "timestamp": "2023-11-15T14:30:00",
    "messages": [
        {"role": "user", "content": "What's the capital of France?", "timestamp": "2023-11-15T14:30:00"},
        {"role": "assistant", "content": "Paris is the capital of France.", "timestamp": "2023-11-15T14:30:05"}
    ]
}
Enter fullscreen mode Exit fullscreen mode

Layer 2: Short-term Memory (File-based)

When a session ends, relevant information is archived to short-term memory files. These are stored as JSON files in a structured directory:

/memory
  /short_term
    /user_67890
      /2023-11
        conv_12345.json
        conv_12346.json
Enter fullscreen mode Exit fullscreen mode

Each file contains the full conversation history, allowing the agent to reference previous interactions when needed.

Layer 3: Long-term Memory (Structured)

Important information is extracted and stored in long-term memory. This layer uses a combination of:

  • Key-value pairs for facts
  • Graph structures for relationships
  • Time-series data for temporal information

python
# Example long-term memory structure
long_term_memory = {
    "facts": {
        "capital_of_france": "Paris",
        "population_of_paris": 2148000
    },
    "relationships": {
        "paris": ["france",
Enter fullscreen mode Exit fullscreen mode

Top comments (0)