AI Agent Memory: Using Decay Curves to Stop Context Loss

JamieCrafter Advanced 2h ago Updated Jul 25, 2026 101 views 5 likes 2 min read

LLM context windows are basically "last-in, first-out" buffers, which is a disaster for long-term agent coherence. If an agent learns a critical user preference in the first ten minutes of a conversation, that data eventually gets pushed out by trivial chatter, even if that preference is the most important piece of information in the entire session.

To fix this, I implemented a usage-reinforced decay engine based on the Ebbinghaus forgetting curve. Instead of treating all memories as equal, the system assigns a "strength" score to every piece of stored information.

The Logic Behind the Decay Engine

The goal is to simulate human memory: information that isn't accessed fades away, but information that is frequently retrieved becomes "hardened" and resists deletion.

1. Initial Weight: Every new memory is stored with a base strength value.
2. Temporal Decay: I apply a decay function that reduces the strength score over time.
3. Reinforcement: Whenever the agent retrieves a specific memory via RAG (Retrieval-Augmented Generation), that memory's strength is boosted.
4. Pruning: Once a memory's strength drops below a specific threshold, it's purged from the active context or moved to cold storage.

Implementation Detail

The core of the system relies on a modified decay formula. Instead of a linear drop, I used an exponential decay where the rate is slowed down by the number of times the memory has been accessed.

import math
import time

def calculate_memory_strength(initial_strength, last_accessed, access_count, decay_rate=0.1):
    elapsed_time = time.time() - last_accessed
    # The more times it's accessed, the slower it decays
    effective_decay = decay_rate / (1 + access_count)
    current_strength = initial_strength * math.exp(-effective_decay * elapsed_time)
    return current_strength

This approach transforms the memory from a simple sliding window into a dynamic priority queue. In real-world testing, the agent stopped asking the same clarifying questions every 20 turns because the "core" identity and constraint memories were reinforced enough to survive the pruning cycles.

This is a much more robust AI workflow for anyone building long-term LLM agents who can't just rely on massive context windows to solve the "forgetting" problem.

Help Wanted

All Replies (4)

L
LazyBot Intermediate 10h ago
Might be worth mentioning vector DBs for the stuff that needs to stay permanent.
0 Reply
L
Leo37 Novice 10h ago
had the same issue with my bot forgetting my name after a few pages of chat.
0 Reply
J
Jamie16 Novice 10h ago
@Leo37 Same here. I tried increasing the context window but it just made the bot hallucinate more. Tough to balance.
0 Reply
J
JamieCrafter Advanced 10h ago
I've found that weighting specific metadata tags helps keep key facts from decaying too fast.
0 Reply

Write a Reply

Markdown supported