Memory for an AI Agent: From Complexity to 300 Lines of Code

Developer Nikolay Gusev explained how he found an optimal memory solution for AI agents. Instead of the complex MemPalace with 58k duplicates, he proposes a simple system built with 300 lines of code and markdown files. For scaling, he suggests ClickHouse with vector search and partitioning.

AI-processed from Habr AI; edited by Hamidun News
Memory for an AI Agent: From Complexity to 300 Lines of Code
Source: Habr AI. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceHabr AI ↗
Editor-in-Chief
Key metric #1Vector search via the `cosineDistance()` function — fast search for semantically similar memories
Key metric #2Text search with `tokenbf_v1` index — full-text search across memory
Key metric #3Partitioning by user_id — user data isolation built into the database

AI agent memory is one of the most complex problems when building autonomous systems. Developer Nikolay Gusev spent months searching for a working solution and shared how he achieved simplicity and scalability at the same time.

MemPalace and the first mistake

It all started with an ambitious idea — MemPalace, a memory system inspired by the architecture of medieval palaces. It looked smart on paper, but in practice created 58 thousand duplicate fragments and turned into an impenetrable maze. Too many layers of abstraction, too much magic. The agent got lost in its own memory.

The path to simplicity

After MemPalace, the author simplified the stack to four components: MEMORY.md (main database), USER.md (user context), SQLite state.db (state), HippoRAG (search) and wiki files (structured data). But even this seemed excessive. The right move was writing `findings_to_wiki`, a simple 300-line Python script that automatically saves structured analyses (marked with headings like `## Findings`) into markdown files and transforms them into wiki pages. No frameworks, no ORM, just files and text.

Scaling: ClickHouse

When there's only one agent, file storage works perfectly. But if you need to support 100+ users simultaneously, you need a real database. This is where ClickHouse 24.x comes into play. Key features:

  • Vector search via the `cosineDistance()` function — fast search for semantically similar memories
  • Text search with `tokenbf_v1` index — full-text search across memory
  • Partitioning by user_id — user data isolation built into the database
  • TTL for automatic deletion — old records are deleted without manual intervention

ClickHouse performs analytics and search across millions of records in milliseconds. For agent memory, this is more than sufficient.

What this means

The key takeaway: don't start with scaling. Solve the problem simply (files, markdown, 300 lines of code), then scale only if needed. Nikolay demonstrated that the best engineering path is from complexity to simplicity, not the other way around.

Story

Key Questions Answered

What are the key details on MemPalace and the first mistake?

It all started with an ambitious idea — MemPalace, a memory system inspired by the architecture of medieval palaces. It looked smart on paper, but in practice created 58 thousand duplicate fragments and turned into an impenetrable maze. Too many layers of abstraction, too much magic. The agent got lost in its own memory.

What are the key details on The path to simplicity?

After MemPalace, the author simplified the stack to four components: MEMORY.md (main database), USER.md (user context), SQLite state.db (state), HippoRAG (search) and wiki files (structured data). But even this seemed excessive. The right move was writing `findings_to_wiki`, a simple 300-line Python script that automatically saves structured analyses (marked with headings like `## Findings`) into markdown files and transforms them into wiki pages. No frameworks, no ORM, just files and text.

What are the key details on Scaling: ClickHouse?

When there's only one agent, file storage works perfectly. But if you need to support 100+ users simultaneously, you need a real database. This is where ClickHouse 24.x comes into play. Key features: Vector search via the `cosineDistance()` function — fast search for semantically similar memories Text search with `tokenbf_v1` index — full-text search across memory Partitioning by user_id — user data isolation built into the database TTL for automatic deletion — old records are deleted without manual intervention ClickHouse performs analytics and search across millions of re

ZK
Zhemal Khamidun — CPO at Alpina Digital, product manager of AlpinaGPT. Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Hands-On AI Engineering · Hamidun Academy

Build & Ship Production AI Agents Without Boilerplate Code

Start your free 72-hour trial at Hamidun Academy (academy.hamidun.com) — interactive lessons on Claude Code & Cursor, live Tech Radar, and production agent templates.

Start 72h Free Trial →Vibe-Coding Intensive
What do you think?
Loading comments…