System Architecture
Components
API Server
The MemoClaw API runs on Hono with Bun as the runtime. It handles:- Request validation and rate limiting
- Wallet authentication (free tier) and x402 payment verification
- Memory CRUD operations
- LLM-powered extraction and consolidation
- Hybrid recall scoring
Database: PostgreSQL + pgvector
All memories are stored in PostgreSQL with the pgvector extension:- Memory content stored as text with full-text search (tsvector)
- Embeddings stored as 512-dimensional vectors
- HNSW index for fast approximate nearest neighbor search
- GIN index on tsvector for keyword/BM25 matching
Embeddings
Text is converted to vectors using OpenAI’stext-embedding-3-small model (512 dimensions). An LRU cache of 1,000 embeddings avoids redundant API calls for repeated queries.
Payment Layer: x402
MemoClaw uses the x402 protocol for payment-as-authentication:- Free tier: Wallet signature verification (100 free calls)
- Paid tier: USDC micropayments on Base (chain ID 8453) via EIP-3009 or Permit2
- Identity: Your wallet address is extracted from the payment proof — no accounts needed
LLM Processing
Some endpoints use an LLM (OpenAI GPT) for intelligent processing:- Extract (
/v1/memories/extract): Parses conversations into discrete facts - Ingest (
/v1/ingest): Extracts, deduplicates, and relates facts - Consolidate (
/v1/memories/consolidate, LLM mode): Synthesizes merged memories
Data Flow: Store
- Client sends memory content + metadata
- API validates input, verifies payment/auth
- Content is embedded via OpenAI (or cache hit)
- Deduplication check: cosine similarity ≥ 0.95 against existing memories
- Memory + embedding stored in PostgreSQL
- Response with UUID returned
Data Flow: Recall
- Client sends natural language query
- Query embedded via OpenAI (or cache hit)
- pgvector HNSW index finds nearest vectors
- Full-text search (BM25) runs in parallel
- Results scored with 4-signal hybrid formula:
- Vector similarity (55%)
- Keyword match (25%)
- Recency decay (20%)
- Multiplied by importance, access boost, and type decay
- Top-K results returned with signal breakdown