Skip to main content

Memory Module Overview

The ARKOS Memory Module implements a dual-tier memory system combining PostgreSQL for conversation context storage with Mem0/Supabase for semantic long-term memory retrieval.

Core Concepts

The Memory Module provides both short-term conversation context (PostgreSQL) and long-term semantic memory (Mem0 with Supabase vector store) for personalized AI interactions.

Memory Architecture

The system uses two complementary storage backends:
  1. Short-term Memory (PostgreSQL): Stores recent conversation turns with full message content
  2. Long-term Memory (Mem0/Supabase): Vector-based semantic storage for retrieval across sessions

Architecture Diagram

Message Types

All memory operations use Pydantic message classes:

Memory Class

Initialization

Configuration

The Memory class uses global configuration for Mem0:

Memory Operations

Adding Memories

Store a message to both PostgreSQL (immediate) and Mem0 (background):
Messages are stored in PostgreSQL immediately (fast, synchronous) and sent to Mem0 in a background thread (non-blocking) for long-term storage.

Retrieving Short-term Memory

Get recent conversation turns from PostgreSQL:

Retrieving Long-term Memory

Search for semantically relevant memories using Mem0:

Session Management

Database Schema

The PostgreSQL conversation_context table:

Message Serialization

Messages are serialized to JSON for storage:

Connection Pooling

The module uses a global connection pool for efficiency:

Background Processing

Long-term memory storage happens in a background thread to avoid blocking:

Integration with Agent

The Agent module uses Memory for context management:

Performance Considerations

Optimizations Implemented

  1. Connection Pooling: Reuses database connections
  2. Background Processing: Mem0 operations don’t block the main thread
  3. Lazy Initialization: Mem0 only initialized if use_long_term=True
  4. Limited Query Scope: Long-term search uses only recent context (last 2 messages)

Disabling Long-term Memory

For faster responses, disable long-term memory:

Environment Variables

Required environment variables:

Error Handling

The module includes robust error handling:

Testing

Basic test example:

Next Steps

Implementation Details

Deep dive into memory implementation

Agent Module

Learn about agent orchestration

Model Module

Explore LLM integration

Development Setup

Set up your environment