A reference implementation of cognitive memory architectures for AI agents, built with Next.js and the Vercel AI SDK.
📖 Companion blog post: How Agents Actually Remember Things: The 4 Pillars of AI Memory
- Working Memory: Context-window management (
src/store/chat-store.ts) - Semantic Memory: System prompt dynamic rule injection (
src/app/api/chat/route.ts) - Procedural Memory: Progressive skill loading via tool calling (
src/lib/memory/skills.ts,src/lib/skills-registry.ts) - Episodic Memory: Live, turn-by-turn conversational state distillation (
src/lib/memory/episodic.ts)
- Next.js 16 (App Router, Turbopack, React Compiler)
- Vercel AI SDK v7 (
streamText,useChat) - OpenRouter (Model gateway API)
- Zustand + Persist (Local storage state persistence)
- Zod (Request boundary validation)
- shadcn/ui + Base UI (Primitive component layouts)
- tokenlens (Token and API cost calculations)
- skills.sh (Live procedural skill registry)
Requires Bun.
git clone https://github.com/devyanshyadav/ai-memory-lab.git
cd ai-memory-lab
bun install
cp .env.example .env.localAdd your OpenRouter API key to .env.local:
OPENROUTER_API_KEY=sk-or-v1-...Run the development server:
bun devOpen http://localhost:3000.
src/
├── app/
│ ├── api/
│ │ ├── chat/route.ts # System prompt builder and main stream endpoint
│ │ └── skills-registry/ # search & import API routes
│ └── page.tsx # UI components and message submission
├── components/
│ ├── memory-inspector.tsx # Resizable memory debugger panel
│ └── skill-importer.tsx # Skill registry search UI
├── lib/
│ ├── memory/
│ │ ├── skills.ts # Procedural skill schemas
│ │ └── episodic.ts # Episodic memory consolidation logic
│ ├── skills-registry.ts # skills.sh API wrapper
│ └── chat-schema.ts # Type definitions & Zod schemas
└── store/
└── chat-store.ts # Zustand state persistence
bun dev # start dev server
bun build # compile production bundle
bun start # run production bundle
bun lint # run Biome checks
bun format # run Biome formattingDevyansh Yadav — devyanshyadav.com · GitHub
