Ahmed Doghri Logo Image
Ahmed Doghri

agentmem

Bounded, self-consolidating long-term memory for LLM agents that stays under a fixed budget instead of growing forever.

agentmem GitHub repository README

Your Agent Doesn't Need to Remember "ok sounds good"

Most "agent memory" is a vector store with no opinions: dump every turn in, hope retrieval sorts out the treasure from the small talk later. That's not memory, that's a junk drawer with embeddings, and it grows forever whether or not anything in it is actually worth keeping.

agentmem treats memory like an actual budget, because real agents run under one. Writes are salience-gated (small talk gets bounced at the door), retrieval blends similarity, recency, and importance, and when the store fills up it consolidates redundant memories into summaries before evicting the weakest ones.

Technical Architecture

Zero dependencies, zero API keys, with a one-method interface for swapping in the real thing:

Salience gate: scores each write and drops filler before it ever lands in the store, instead of relying on retrieval to filter it out later.

Near-exact dedup: a repeated fact reinforces an existing memory (uses go up, salience goes up) rather than creating a duplicate entry.

Decay-aware retrieval: ranks by similarity plus recency plus importance, and retrieving a memory itself strengthens it against future decay.

Consolidation before eviction: when the store hits capacity, related memories get fused into a summary before the weakest items are evicted, instead of just dropping the oldest entry blindly.

Challenges & Solutions

The hardest part wasn't the salience gate, it was building a benchmark that makes the budget-versus-recall tradeoff impossible to hand-wave away. It would have been easy to cherry-pick a capacity where the numbers look great. Instead, the benchmark streams 200 salient facts past the agent with distractor chatter mixed in, caps memory at a fixed slot count, then quizzes on a random sample, so raising or lowering the capacity argument visibly moves recall in a way anyone can reproduce and verify.

I also had to resist over-engineering the default embedder. A deterministic hashing embedder isn't as accurate as a real model, but it means the whole benchmark runs with zero API keys and zero network calls, which was worth more for a reference implementation than squeezing out a slightly higher recall number.

Impact & Results

Streaming 200 facts past the agent with a 32-slot memory cap, the store consolidates down to exactly 32 items and holds 52% recall@5 on a random quiz sample, a number that climbs toward 100% as the capacity argument increases, making the actual tradeoff visible instead of hidden behind a single cherry-picked configuration.

7 tests pass in CI across Python 3.9 through 3.13, and the package ships a `py.typed` marker so downstream users get real type hints, not just runtime behavior.

Tools Used

Python
Hashing Embeddings
Decay-Aware Retrieval
pytest
Ruff
GitHub Actions CI
PyPI Packaging