--- title: 'System Architecture' description: 'Technical architecture and design decisions for T6 Mem0 v2' --- ## Architecture Overview T6 Mem0 v2 implements a **hybrid storage architecture** combining vector search, graph relationships, and structured data storage for optimal memory management. ``` ┌─────────────────────────────────────────────────────────────┐ │ Client Layer │ ├──────────────────┬──────────────────┬──────────────────────┤ │ Claude Code (MCP)│ N8N Workflows │ External Apps │ └──────────────────┴──────────────────┴──────────────────────┘ │ │ │ │ │ │ ▼ ▼ ▼ ┌─────────────────────────────────────────────────────────────┐ │ Interface Layer │ ├──────────────────────────────┬──────────────────────────────┤ │ MCP Server (Port 8765) │ REST API (Port 8080) │ │ - SSE Connections │ - FastAPI │ │ - MCP Protocol │ - OpenAPI Spec │ │ - Tool Registration │ - Auth Middleware │ └──────────────────────────────┴──────────────────────────────┘ │ │ └────────┬───────────┘ ▼ ┌─────────────────────────────────────────────────────────────┐ │ Core Layer │ │ Mem0 Core Library │ │ - Memory Management - Embedding Generation │ │ - Semantic Search - Relationship Extraction │ │ - Multi-Agent Support - Deduplication │ └─────────────────────────────────────────────────────────────┘ │ ┌───────────────────┼───────────────────┐ ▼ ▼ ▼ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ Vector Store │ │ Graph Store │ │ External LLM │ │ Supabase │ │ Neo4j │ │ OpenAI │ │ (pgvector) │ │ (Cypher) │ │ (Embeddings) │ │ 172.21.0.12 │ │ 172.21.0.x │ │ API Cloud │ └─────────────────┘ └─────────────────┘ └─────────────────┘ ``` ## Design Decisions ### 1. Hybrid Storage Architecture ✅ **Why Multiple Storage Systems?** Each store is optimized for specific query patterns: **Purpose**: Semantic similarity search - Stores 1536-dimensional OpenAI embeddings - HNSW indexing for fast approximate nearest neighbor search - O(log n) query performance - Cosine distance for similarity measurement **Purpose**: Relationship modeling - Entity extraction and connection mapping - Relationship traversal and pathfinding - Visual exploration in Neo4j Browser - Dynamic knowledge graph evolution **Purpose**: Flexible metadata - Schema-less metadata storage - Fast JSON queries with GIN indexes - Eliminates need for separate Redis - Simplifies infrastructure ### 2. MCP Server Implementation **Custom vs. Pre-built** We built a custom MCP server instead of using OpenMemory MCP because: - OpenMemory uses Qdrant (we need Supabase) - Full control over Supabase + Neo4j integration - Exact match to our storage stack ### 3. Docker Networking Strategy **localai Network Integration** All services run on the `localai` Docker network (172.21.0.0/16): ```yaml services: neo4j: 172.21.0.x:7687 api: 172.21.0.x:8080 mcp-server: 172.21.0.x:8765 supabase: 172.21.0.12:5432 (existing) ``` **Benefits:** - Container-to-container communication - Service discovery via Docker DNS - No host networking complications - Persistent IPs via Docker Compose ## Data Flow ### Adding a Memory Client sends conversation messages via MCP or REST API - LLM extracts key facts from messages - Generates embedding vector (1536-dim) - Identifies entities and relationships Stores embedding + metadata in Supabase (pgvector) Creates nodes and relationships in Neo4j Returns memory ID and confirmation to client ### Searching Memories Convert search query to vector using OpenAI Find similar memories in Supabase (cosine similarity) Fetch related context from Neo4j graph Return memories sorted by relevance score ## Performance Characteristics Based on mem0.ai research: Higher accuracy vs baseline OpenAI Compared to full-context approaches Through selective memory retrieval ## Security Architecture ### Authentication - **REST API**: Bearer token authentication - **MCP Server**: Client-specific SSE endpoints - **Tokens**: Stored securely in environment variables ### Data Privacy All data stored locally - no cloud sync or external storage - Supabase instance is local (172.21.0.12) - Neo4j runs in Docker container - User isolation via `user_id` filtering ### Network Security - Services on private Docker network - No public exposure (use reverse proxy if needed) - Internal communication only ## Scalability ### Horizontal Scaling Deploy multiple API containers behind load balancer Dedicated instances per client group Stateless design scales with containers ### Vertical Scaling - **Supabase**: PostgreSQL connection pooling - **Neo4j**: Memory configuration tuning - **Vector Indexing**: HNSW for performance ## Technology Choices | Component | Technology | Why? | |-----------|-----------|------| | Core Library | mem0ai | Production-ready, 26% accuracy boost | | Vector DB | Supabase (pgvector) | Existing infrastructure, PostgreSQL | | Graph DB | Neo4j | Best-in-class graph database | | LLM | OpenAI | High-quality embeddings, GPT-4o | | REST API | FastAPI | Fast, modern, auto-docs | | MCP Protocol | Python MCP SDK | Official MCP implementation | | Containers | Docker Compose | Simple orchestration | ## Phase 2: Ollama Integration **Configuration-driven provider switching:** ```python # Phase 1 (OpenAI) "llm": { "provider": "openai", "config": {"model": "gpt-4o-mini"} } # Phase 2 (Ollama) "llm": { "provider": "ollama", "config": { "model": "llama3.1:8b", "ollama_base_url": "http://172.21.0.1:11434" } } ``` **No code changes required** - just environment variables! ## Monitoring & Observability ### Metrics to Track - Memory operations per second - Average response time - Vector search latency - Graph query complexity - OpenAI token usage ### Logging - Structured JSON logs - Request/response tracking - Error aggregation - Performance profiling Use Prometheus + Grafana for production monitoring ## Deep Dive Resources For complete architectural details, see: - [ARCHITECTURE.md](https://git.colsys.tech/klas/t6_mem0_v2/blob/main/ARCHITECTURE.md) - [PROJECT_REQUIREMENTS.md](https://git.colsys.tech/klas/t6_mem0_v2/blob/main/PROJECT_REQUIREMENTS.md) ## Next Steps Configure vector store Configure graph database Explore endpoints Connect with Claude Code