---
id: 9665
lang: en
title: "Memory for an AI Agent: From Overengineering to 300 Lines of Code"
published_at: "2026-05-17 20:15:39"
source_name: "Habr AI"
source_url: "https://habr.com/ru/articles/1032630/?utm_campaign=1032630&utm_source=habrahabr&utm_medium=rss"
canonical_url: "https://news.hamidun.com/en/news/9665"
categories: ["autonomous"]
publisher: "AI News by Jemal Hamidun (news.hamidun.com)"
---

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

> **Краткая суть (BLUF):** 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.

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.

---
- **Первоисточник:** [Habr AI](https://habr.com/ru/articles/1032630/?utm_campaign=1032630&utm_source=habrahabr&utm_medium=rss)
- **Каноническая ссылка:** https://news.hamidun.com/en/news/9665
- **Экосистема автора:** [Бесплатный интенсив по вайбкодингу — предзапись в Нике](https://t.me/vibecodeguidebot?start=agent_intensive_news_md) · [Nomad AI](https://nomadnet.ai) · [Telegram @jemal_hamidun](https://t.me/jemal_hamidun)
