Inspiration

I kept hitting the same wall with AI assistants: they don't really remember me. The usual fix is to save every past message in a vector database and search it on every turn. I built that, and it held up for about a week. Then the database got big, old facts and new facts came back with equal confidence, and when I told the assistant something had changed, it would still hand me the outdated version.

What was missing wasn't more storage. It was forgetting. Human memory keeps what you use and quietly drops what you don't, and it updates when the world changes. I wanted to build that.

What it does

Engram is a memory engine for an AI agent. Four ideas do the work.

Memories decay. Every memory has a stability value, and its strength follows the Ebbinghaus forgetting curve. Recall a memory and it gets stronger, the way spaced repetition works. Memories you keep using stick around. The rest fade.

Facts become beliefs, and beliefs get superseded, not deleted. A cheap model pass turns each message into (subject, predicate, object) facts, like (user, is allergic to, shellfish). When a new fact contradicts an old one, the model decides whether it replaces the old belief, sits alongside it, or gets dropped. A replaced belief keeps its dates and a link to what replaced it, so the agent knows both what is true now and what used to be true.

Faded memories get consolidated. A periodic sleep cycle compresses stale episodes into a short summary and archives the originals, so storage levels off instead of growing forever.

Recall runs under a fixed token budget. Each candidate memory is scored on relevance, importance, and how well it has survived over time, then the best ones are packed under a hard token limit. The memory context stays the same size whether the agent has a day of history or a year.

How I built it

Everything runs on Qwen models through Alibaba Cloud Model Studio. qwen3.7-plus writes the replies, qwen-flash handles the high-volume work of fact extraction, belief revision, and summaries, and text-embedding-v4 produces the 512-dimensional memory embeddings. The backend is FastAPI, memory lives in a single SQLite file with a bi-temporal belief table, and the frontend is a plain web UI that shows the recall trace, the belief ledger, and a "timewarp" clock so decay and consolidation, which normally take weeks, can be watched in minutes.

Challenges I ran into

Tuning the forgetting felt like the whole project for a while. Too aggressive and useful facts vanished. Too gentle and nothing ever faded. Belief revision was the other hard part. My first version only updated a belief when the new fact used the exact same wording, so "I moved to Berlin" and "I live in Lisbon" both stayed marked as current, which is plainly wrong. I fixed it by comparing embeddings so the system notices when two differently phrased facts are about the same thing, then letting the model make the final call.

Accomplishments I'm proud of

I built a benchmark to check whether any of this actually helps. Two agents, the same Qwen model, three conversations spread weeks apart, then seven questions whose answers depend on the earlier sessions. The plain agent with no memory scored zero out of seven. Engram got all seven, using under 300 tokens of memory per answer. Answering with an out-of-date fact counts as wrong, so the score also measures whether belief revision works.

What I learned

Forgetting is a feature, not a bug. Once memory can fade and update, retrieval gets easier because there is less junk to sort through. I also learned that letting a small, cheap model decide contradictions is more reliable than any set of rules I tried to write by hand.

What's next

Swapping the single SQLite file for ApsaraDB RDS so it can run across more than one instance, and giving the agent tools so it can decide when to commit something to long-term memory instead of extracting facts from every message.

Built With

Share this project:

Updates