Forgetting and Expiring Data
Agents should not store data indefinitely. Old state should expire automatically to free resources, protect privacy, and prevent the agent from acting on stale information.
6 min•By Priygop Team•Updated 2026
Expiry Strategies
- TTL (Time to Live): each stored item has an expiry timestamp. Expired items are automatically deleted.
- LRU eviction: when memory is full, the least recently used items are removed first
- Session cleanup: all session state is deleted when a session ends or times out
- Result expiry: web search results expire after a few hours since information becomes outdated
- User preference retention: user preferences are kept for months or years
- Audit log retention: audit logs may be retained indefinitely for compliance purposes
TTL Memory Store
TTL Memory Store
from datetime import datetime, timedelta
from typing import Any, Optional
class TTLMemory:
"""Memory store where items expire after a set time."""
def __init__(self):
self._store = {} # key -> (value, expires_at)
def write(self, key: str, value: Any, ttl_seconds: int = 3600):
expires_at = datetime.now() + timedelta(seconds=ttl_seconds)
self._store[key] = (value, expires_at)
def read(self, key: str) -> Optional[Any]:
if key not in self._store:
return None
value, expires_at = self._store[key]
if datetime.now() > expires_at:
del self._store[key] # Clean up expired entry
return None
return value
def cleanup(self):
"""Remove all expired items."""
now = datetime.now()
expired = [k for k, (_, exp) in self._store.items() if now > exp]
for key in expired:
del self._store[key]
return len(expired)
@property
def active_count(self):
self.cleanup()
return len(self._store)
mem = TTLMemory()
mem.write("search_results", ["Django", "FastAPI"], ttl_seconds=300) # 5 min
mem.write("user_pref_format", "bullets", ttl_seconds=86400) # 24 hours
print("Search results:", mem.read("search_results"))
print("Active items:", mem.active_count)Key Takeaways
- Agents should not store data indefinitely.
- TTL (Time to Live): each stored item has an expiry timestamp. Expired items are automatically deleted.
- LRU eviction: when memory is full, the least recently used items are removed first
- Session cleanup: all session state is deleted when a session ends or times out