forgetai
"AI has photographic memory but no ability to forget." — Andrej Karpathy
Forgetting is not a bug. It's a feature.
forgetai is a zero-dependency Python library that gives AI applications memory that works like human memory: it decays over time, can be explicitly forgotten, and is retrieved by semantic similarity.
Install
Quick Start
from forgetai import Memory
mem = Memory(ttl="7d", max_size=1000)
mem.remember("user prefers dark mode", importance=0.9)
result = mem.recall("what theme does the user like?")
print(result.text) # "user prefers dark mode"
Why Forgetting Matters
AI systems with perfect memory develop real problems:
- Stereotyping — A user mentions crypto once; the AI brings it up forever.
- Context pollution — Stale facts from weeks ago override fresh context.
- Unbounded growth — Memory stores bloat until they're slow and expensive.
- No prioritization — A passing comment is weighted the same as a core preference.
Human memory solves these problems through forgetting. forgetai brings the same mechanics to AI:
- TTL expiry — Memories have a shelf life. Old ones disappear.
- Importance decay — Every prune cycle reduces importance. Weak memories fade.
- Explicit forgetting — Remove memories by fuzzy match when they're no longer relevant.
- Capacity limits — When full, the least important memory is evicted.
Usage
Remember
mem = Memory(ttl="7d", decay_rate=0.95, threshold=0.85)
# Basic
mem.remember("user prefers dark mode")
# With importance and custom TTL
mem.remember("user prefers dark mode", ttl="30d", importance=0.9)
mem.remember("user asked about crypto once", ttl="7d", importance=0.3)
Recall
# Best match above the similarity threshold
result = mem.recall("what theme does the user like?")
# MemoryResult(text="user prefers dark mode", similarity=0.92, importance=0.9, age="2h")
# Multiple results
results = mem.recall_many("user preferences", top_k=5)
# Override threshold for this query
result = mem.recall("theme", threshold=0.5)
Forget
# Fuzzy match — removes anything similar to "crypto"
removed = mem.forget("crypto")
print(f"Forgot {removed} memories")
Prune
# Decay importance + remove expired + remove low-importance
removed = mem.prune()
Inspect
# See what's about to expire
expiring = mem.expiring("24h")
# Full health check
for m in mem.inspect():
print(f"{m.status:10s} {m.importance:.2f} {m.text}")
# healthy 0.90 user prefers dark mode
# decaying 0.12 mentioned liking jazz
# expiring 0.45 asked about pricing
Persistence
# Export to JSON
snapshot = mem.export_json()
# Save to disk, database, Redis, wherever
with open("memory.json", "w") as f:
f.write(snapshot)
# Restore
with open("memory.json") as f:
mem = Memory.from_json(f.read())
Advanced
Custom Embeddings
The built-in similarity uses character n-gram vectors — zero dependencies, works everywhere. For production, plug in real embeddings:
import openai
client = openai.OpenAI()
def embed(text: str) -> list[float]:
resp = client.embeddings.create(input=text, model="text-embedding-3-small")
return resp.data[0].embedding
mem = Memory(embed_fn=embed, threshold=0.85)
Configuration
| Parameter |
Default |
Description |
ttl |
"7d" |
Default time-to-live for memories |
max_size |
1000 |
Maximum memories stored |
decay_rate |
0.95 |
Importance multiplier per prune cycle |
threshold |
0.85 |
Minimum similarity for recall |
min_importance |
0.05 |
Memories below this are pruned |
embed_fn |
None |
Custom embedding function |
Memory Lifecycle
remember() → stored with importance + TTL
↓
recall() → retrieved by similarity, access count incremented
↓
prune() → importance decayed, expired removed, weak removed
↓
forget() → explicit removal by fuzzy match
Requirements
- Python 3.9+
- Zero dependencies
License
MIT — Built by Freedom Engineers