How to add persistent memory to your AI agent

By Google Cloud Tech

Share:

Key Concepts

  • Persistent Session Service: A storage mechanism that saves conversation history and agent state to a database (disk) rather than RAM, allowing for continuity after application restarts.
  • User Profile Store: A structured database table used to persist long-term user preferences (e.g., dietary restrictions, interests) across different sessions.
  • Agent Tools: Specific functions (recall_user_preference and save_user_preference) that allow the agent to interact with the user profile database.
  • Session ID: A unique identifier used to link a specific user to their conversation history and stored preferences.
  • ADK (Agent Development Kit): The framework used to manage agent memory, session services, and tool integration.

1. Durable Chat via Database Session Service

The primary limitation of in-memory session services is that all data is purged upon application restart. To solve this, the architecture is upgraded to a Database Session Service.

  • Mechanism: Instead of storing state in RAM, the system writes every user message, agent reply, and state change to a persistent storage medium (e.g., a local database file or PostgreSQL).
  • Implementation: The get_or_create_session helper function is used. It attempts to retrieve an existing session by ID; if found, it resumes the conversation; if not, it initializes a fresh session.
  • Pluggability: The agent logic remains decoupled from the storage engine. Developers can swap between:
    • In-memory session service: Fast, but volatile (clears on restart).
    • Database session service: Durable, survives restarts.
    • Vertex AI session service: A managed cloud-based solution for enterprise-scale memory.

2. User Profile Store for Long-Term Personalization

While persistent sessions handle the continuity of a single conversation, a User Profile Store is required to maintain context across different sessions (e.g., a new chat initiated a week later).

  • Data Structure: A small, structured database table where each row corresponds to a user_id and a preference_key (e.g., dietary, favorite_thing, transport_mode).
  • Tooling: The agent is equipped with two specific tools:
    1. recall_user_preference: Reads all saved preferences associated with the current user_id.
    2. save_user_preference: Inserts or updates a key-value pair in the database.

3. Operational Framework: The Agent Instruction Loop

To ensure the agent effectively utilizes the profile store, a specific workflow is implemented:

  1. Recall First: At the start of any conversation, the agent must call recall_user_preference to load existing context.
  2. Personalize and Plan: The agent uses the retrieved data to tailor its responses.
  3. Present and Learn: After providing information, the agent asks the user if there is any new information worth remembering.
  4. Save Last: If the user provides a new fact, the agent calls save_user_preference before concluding the interaction.

4. Real-World Application Example

The video demonstrates a trip-planning scenario:

  • Turn 1: The user asks for a trip plan. The agent recalls nothing, provides a plan, and asks for preferences.
  • Turn 2: The user identifies as a "vegetarian." The agent calls save_user_preference to store this fact.
  • Restart: The application is stopped and restarted.
  • Turn 3 (New Session): The user returns. The agent calls recall_user_preference, identifies the "vegetarian" status, and immediately personalizes the new trip plan accordingly.

5. Synthesis and Conclusion

The transition from short-term, volatile memory to a hybrid system of persistent sessions and structured user profiles allows agents to maintain both conversational flow and long-term user context.

  • Key Takeaway: By decoupling the storage engine from the agent logic, developers can ensure that agents remain "stateful" across restarts and "personalized" across different timeframes.
  • Future Outlook: The next phase of this series will move beyond structured data to a Memory Bank, which will handle unstructured data (media, text, audio) and semantic search to retrieve complex information.

Chat with this Video

AI-Powered

Load the transcript when you're ready to chat so the initial page stays lighter.

Ready to summarize another video?

Summarize YouTube Video