
Memory Expiration Strategies: Cleaning the Mind
Learn how to manage the 'infinite growth' problem in agent memory. Master TTL, importance-based pruning, and memory summarization.
Memory Expiration Strategies: Cleaning the Mind
A vector database that grows forever becomes a liability.
- Noise: Old, irrelevant memories make it harder to find the "Signal."
- Cost: You pay for every GB of vector storage.
- Latency: Searching through millions of "Hi" and "How are you?" messages slows down every request.
In this lesson, we learn how to "Prune" the agent's mind using Expiration Strategies.
1. Time-to-Live (TTL): The Standard Pruning
The simplest way is to delete memories that are too old.
- Application: "If a conversation happened more than 90 days ago, it is likely obsolete."
The Meta-Filtering approach:
Instead of deleting, you can simply Filter queries: where={"timestamp": {"$gt": ...}}. Periodically, a background job can delete the records that are no longer reachable by the filter.
2. Importance-Based Retention
Not all memories are created equal.
- "What is your name?" -> Low Importance.
- "I am allergic to peanuts." -> High Importance.
The Score Metric: Have the LLM assign an importance_score (1-10) during storage.
The Policy: "Only delete items with an importance score less than 3 after 30 days. Keep items greater than 8 forever."
3. The "Decay" Function
A sophisticated agent uses a Decay Score influenced by both Recency and Frequency.
Final_Score = Base_Importance * (0.5 ^ Years_Passed) * (Number_of_Retrievals)
If a memory is old but it is "Recalled" every day, it resets its decay timer. This is how humans preserve core identity while forgetting yesterday's lunch.
4. Implementation: The Cleanup Worker (Python)
Python Code: Periodic Memory Pruning
def prune_agent_memory(collection, importance_threshold=2, days_old=30):
cutoff_timestamp = time.time() - (days_old * 86400)
# Identify candidates for deletion
results = collection.get(
where={
"$and": [
{"timestamp": {"$lt": cutoff_timestamp}},
{"importance": {"$lt": importance_threshold}}
]
}
)
if results['ids']:
collection.delete(ids=results['ids'])
print(f"Pruned {len(results['ids'])} obsolete memories.")
5. Summary and Key Takeaways
- Avoid Infinite Growth: Set hard policies for data retention.
- Importance Metadata: Scoring memories at the point of ingestion is mandatory for scaling.
- Filter Before Deleting: Use queries to "Soft-Delete" first to ensure system stability.
- Decay Logic: Reward "Frequency of access" to keep important old knowledge alive.
Exercise: The Memory Auditor
- Look at a log of 100 interactions.
- Identify 5 memories that you think the agent should keep "Forever."
- Identify 20 memories that should be deleted after 24 hours.
- The Question: What single word or piece of metadata would allow an automated script to distinguish between these two groups?