Brain Architecture
Your AI's memory should live on YOUR hardware. Not someone else's cloud.
A sovereign brain is persistent memory that you own completely -- stored in SQLite on your machine, searchable, structured, and accumulating wisdom across every interaction. No cloud dependency. No monthly fees. No data leaving your network.
What you'll learn
- Why SQLite is the ideal sovereign brain database
- The key-value brain schema: categories, keys, values, and timestamps
- Building search and retrieval into your brain
- Boot sequences that give your AI instant context
Why SQLite for a Sovereign Brain
SQLite is the most deployed database in the world. It is a single file on your disk -- no server process, no network port, no configuration. Your entire brain lives in one file that you can copy, backup, and move between machines.
Zero dependencies. No database server to install, configure, or maintain. No Docker containers. No cloud services. The database is a file. Your application reads and writes it directly.
Zero latency. Reads from a local SQLite file take microseconds. No network round-trip. No connection pooling. Your AI's boot sequence that reads 50 keys completes in under 10 milliseconds.
Zero cost. No monthly bill. No storage fees. No bandwidth charges. A brain with 10,000 entries takes a few megabytes of disk space. You will never outgrow a single machine for brain storage.
Full portability. Copy the file to another machine and your brain moves with it. Backup to a USB drive. Sync between machines with rsync. No vendor lock-in. No export/import headaches.
The Brain Schema
A sovereign brain needs a simple, flexible schema that supports fast reads, hierarchical organization, and full-text search:
-- Create the brain table
CREATE TABLE IF NOT EXISTS brain (
key TEXT PRIMARY KEY, -- Hierarchical key: "identity.user"
value TEXT NOT NULL, -- The actual content (JSON or plain text)
category TEXT NOT NULL, -- Top-level grouping: identity, directive, system
tags TEXT DEFAULT '', -- Comma-separated tags for flexible querying
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Index for fast category lookups
CREATE INDEX IF NOT EXISTS idx_brain_category ON brain(category);
-- Full-text search index
CREATE VIRTUAL TABLE IF NOT EXISTS brain_fts USING fts5(
key, value, category, tags,
content='brain',
content_rowid='rowid'
);
-- Example entries
INSERT INTO brain (key, value, category) VALUES
('identity.user', 'Business owner, runs an AI consulting firm', 'identity'),
('identity.preferences', 'Concise responses, no jargon, action-oriented', 'identity'),
('directive.autonomy', 'L4: Act within guardrails, surface for spending and legal', 'directive'),
('directive.voice', 'Professional but warm, direct, no corporate speak', 'directive'),
('system.infrastructure', 'M3 Mac, Ollama, SQLite brain, Vercel for web', 'system'),
('session.active_work', 'Building email automation agent', 'session'),
('session.next_steps', '1. Test email sending 2. Add template system', 'session');Read and Write Operations
The brain API is deliberately simple -- read a key, write a key, search by text, list by category:
// brain.js -- Sovereign brain operations
import Database from 'better-sqlite3';
const db = new Database('./brain.db');
// Read a single key
function read(key) {
const row = db.prepare('SELECT value FROM brain WHERE key = ?').get(key);
return row ? row.value : null;
}
// Write or update a key
function write(key, value, category) {
db.prepare(`
INSERT INTO brain (key, value, category, updated_at)
VALUES (?, ?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(key) DO UPDATE SET
value = excluded.value,
updated_at = CURRENT_TIMESTAMP
`).run(key, value, category);
}
// Search by text (full-text search)
function search(query) {
return db.prepare(`
SELECT key, value, category FROM brain_fts
WHERE brain_fts MATCH ?
ORDER BY rank LIMIT 10
`).all(query);
}
// List all keys in a category
function listCategory(category) {
return db.prepare(
'SELECT key, value FROM brain WHERE category = ?'
).all(category);
}
// Boot: read all critical context
function boot() {
const identity = listCategory('identity');
const directives = listCategory('directive');
const session = listCategory('session');
const system = listCategory('system');
return { identity, directives, session, system };
}This lesson is for Pro members
Unlock all 355+ lessons across 36 courses with Academy Pro. Founding members get 90% off — forever.
Already a member? Sign in to access your lessons.