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

Developer Nikolay Gusev described how he found the best solution for AI agent memory. Instead of the convoluted MemPalace, which produced 58k duplicates, he built a simple system of 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 Overengineering 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 lookup of semantically similar memories
Text search with the `tokenbf_v1` indexfull-text search across memory
Partitioning by user_iduser data isolation is built into the database

Memory is one of the hardest problems in building autonomous AI agents. Developer Nikolay Gusev spent months looking for a workable solution and explains how he arrived at something both simple and scalable.

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 clever on paper, but in practice it produced 58 thousand duplicate fragments and turned into an impassable 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 cut the stack down to four components: MEMORY.md (the main store), USER.md (user context), SQLite state.db (state), HippoRAG (search) and wiki files (structured data). But even that felt excessive. The right move was to write `findings_to_wiki`, a simple 300-line Python script that automatically saves structured analyses (marked with headings such as `## Findings`) to markdown files and converts them into wiki pages. No frameworks, no ORMs, just files and text.

Scaling: ClickHouse

When there is a single agent, file storage works perfectly. But supporting 100+ users at once calls for a real database. This is where ClickHouse 24.x comes in. Key features:

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

ClickHouse runs analytics and search across millions of records in milliseconds. For agent memory, that is more than enough.

What this means

The main takeaway: don't start with scaling. Solve the problem simply (files, markdown, 300 lines of code), then scale only if you need to. Nikolay showed that the best engineering path runs 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 clever on paper, but in practice it produced 58 thousand duplicate fragments and turned into an impassable 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 cut the stack down to four components: MEMORY.md (the main store), USER.md (user context), SQLite state.db (state), HippoRAG (search) and wiki files (structured data). But even that felt excessive. The right move was to write `findings_to_wiki`, a simple 300-line Python script that automatically saves structured analyses (marked with headings such as `## Findings`) to markdown files and converts them into wiki pages. No frameworks, no ORMs, just files and text.

What are the key details on Scaling: ClickHouse?

When there is a single agent, file storage works perfectly. But supporting 100+ users at once calls for a real database. This is where ClickHouse 24.x comes in. Key features: Vector search via the `cosineDistance()` function: fast lookup of semantically similar memories Text search with the `tokenbf_v1` index: full-text search across memory Partitioning by user_id: user data isolation is built into the database TTL for automatic deletion: old records are removed without manual intervention ClickHouse runs analytics and search across millions of records in milliseconds. For

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.
✦ Unified AI Workspace · Nomad

Test Frontier LLMs & Autonomous Agents in One Workspace

Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.

Try Nomad AI →Launch Nomad Agent
What do you think?
Loading comments…