←back to Blog

How to Design a Persistent Memory and Personalized Agentic AI System with Decay and Self-Evaluation?

How to Design a Persistent Memory and Personalized Agentic AI System with Decay and Self-Evaluation

In this tutorial, we explore how to build an intelligent agent that remembers, learns, and adapts to users over time. We implement a Persistent Memory and Personalization system using simple, rule-based logic to simulate how modern Agentic AI frameworks store and recall contextual information. This step-by-step guide illustrates how persistence transforms a static chatbot into a context-aware, evolving digital companion.

Understanding the Target Audience

The target audience for this content consists of:

  • AI Developers: Professionals looking to enhance their understanding of building intelligent systems.
  • Business Managers: Individuals seeking to integrate AI solutions into their operations for improved efficiency.
  • Students and Researchers: Learners interested in the theoretical and practical aspects of AI memory systems.

Common pain points include:

  • Difficulty in designing AI systems that adequately retain and utilize user information.
  • Challenges in implementing personalization features that enhance user experience.
  • Concerns regarding memory overload and effective information decay mechanisms.

The goals of the audience are:

  • To build more responsive and adaptive AI systems.
  • To understand the underlying mechanics of memory and personalization in AI.
  • To apply these principles in business contexts for better customer engagement.

Interests include:

  • Practical applications of AI in business.
  • Latest trends in AI development.
  • Technologies that support memory and learning in AI systems.

Preferred communication methods are:

  • Technical tutorials with clear code examples.
  • Step-by-step guides that break down complex topics.
  • Access to source codes and further reading materials.

Building the Agent with Persistent Memory

We begin by establishing the foundation for our agent’s long-term memory. The MemoryItem class holds each piece of information, while the MemoryStore employs an exponential decay mechanism to simulate human memory aging.

import math, time, random
from typing import List

class MemoryItem:
   def __init__(self, kind:str, content:str, score:float=1.0):
       self.kind = kind
       self.content = content
       self.score = score
       self.t = time.time()

class MemoryStore:
   def __init__(self, decay_half_life=1800):
       self.items: List[MemoryItem] = []
       self.decay_half_life = decay_half_life

   def _decay_factor(self, item:MemoryItem):
       dt = time.time() - item.t
       return 0.5 ** (dt / self.decay_half_life)

We define methods for adding, searching, and cleaning up old memories. This allows our agent to remember relevant facts while automatically forgetting weak or outdated ones.

   def add(self, kind:str, content:str, score:float=1.0):
       self.items.append(MemoryItem(kind, content, score))

   def search(self, query:str, topk=3):
       scored = []
       for it in self.items:
           decay = self._decay_factor(it)
           sim = len(set(query.lower().split()) & set(it.content.lower().split()))
           final = (it.score * decay) + sim
           scored.append((final, it))
       scored.sort(key=lambda x: x[0], reverse=True)
       return [it for _, it in scored[:topk] if _ > 0]

   def cleanup(self, min_score=0.1):
       new = []
       for it in self.items:
           if it.score * self._decay_factor(it) > min_score:
               new.append(it)
       self.items = new

Designing the Intelligent Agent

Next, we design an intelligent agent that utilizes memory to inform its responses. The Agent class uses a mock language model simulator to adapt replies based on stored preferences and topics.

class Agent:
   def __init__(self, memory:MemoryStore, name="PersonalAgent"):
       self.memory = memory
       self.name = name

   def perceive(self, user_input:str):
       ui = user_input.lower()
       if "i like" in ui or "i prefer" in ui:
           self.memory.add("preference", user_input, 1.5)
       if "topic:" in ui:
           self.memory.add("topic", user_input, 1.2)
       if "project" in ui:
           self.memory.add("project", user_input, 1.0)

   def act(self, user_input:str):
       mems = self.memory.search(user_input, topk=4)
       ctx = [m.content for m in mems]
       answer = self._llm_sim(user_input, ctx)
       self.memory.add("dialog", f"user said: {user_input}", 0.6)
       self.memory.cleanup()
       return answer, ctx

Evaluating Personalization

We enable the agent to evaluate the impact of its memory on personalization. This involves comparing responses with memory against those without.

def evaluate_personalisation(agent:Agent):
   agent.memory.add("preference", "User likes cybersecurity articles", 1.6)
   q = "Recommend what to write next"
   ans_personal, _ = agent.act(q)
   empty_mem = MemoryStore()
   cold_agent = Agent(empty_mem)
   ans_cold, _ = cold_agent.act(q)
   gain = len(ans_personal) - len(ans_cold)
   return ans_personal, ans_cold, gain

Conclusion

We demonstrate how adding memory and personalization makes our agent more human-like, capable of remembering preferences, adapting plans, and forgetting outdated details. Observations indicate that even simple mechanisms such as decay and retrieval significantly enhance the agent’s relevance and response quality.

Ultimately, persistent memory is the foundation of next-generation Agentic AI, allowing continuous learning and intelligent experience tailoring in a fully local, offline setup.

For the complete code and further resources, visit our GitHub page for tutorials, codes, and notebooks. Additionally, follow us on Twitter and join our machine learning community on Reddit.

External illustration

The post How to Design a Persistent Memory and Personalized Agentic AI System with Decay and Self-Evaluation appeared first on MarkTechPost.