Graph Memory in Agents: Relationships That Persist

Graph Memory in Agents: Relationships That Persist

Give your AI a persistent memory. Learn how to use a Knowledge Graph to store conversation history and user preferences, allowing your agent to 'remember' complex relationships across different sessions.

Graph Memory in Agents: Relationships That Persist

Most AI agents have "Goldfish Memory." Even if you have a 1-hour conversation, the agent treats every session as a fresh start (or at best, a flat text history). But real human memory is a Graph. We remember that "Sudeep mentioned he likes Coffee," and we link "Coffee" to "Morning Routine."

In this lesson, we will move beyond "Retrieving Knowledge" and into "Storing Memory." We will learn how to use our Graph Database to store User Profiles, Conversation Entities, and Persistent Preferences. We will see how this allows an agent to say: "Last week you mentioned the Tokyo budget was tight. Should I check the current status of that?"


1. The Strategy: Conversational Extraction

When a user speaks, the agent doesn't just answer—it performs an Upsert (Update/Insert).

  • User: "My name is Sudeep and I'm a developer at Google."
  • Agent Extraction:
    • MERGE (p:User {id: 'session_123'}) SET p.name = 'Sudeep'
    • MERGE (c:Org {name: 'Google'})
    • MERGE (p)-[:WORKS_AT]->(c)

Benefit: The next time Sudeep logs in, the agent performs a Neighborhood Expansion on the session_123 node. It immediately "Knows" who he is and where he works without him re-introducing himself.


2. Modeling "Personal Context" vs. "Global Knowledge"

In a production Memory Graph, we separate the "Truth" from the "User Insight."

  • Layer 1 (The World): (Google)-[:LOCATION]->(USA)
  • Layer 2 (The Memory): (Sudeep)-[:STATED_INTEREST]->(Google)

By linking the User Node to the Global Entity Node, the agent can bridge the gap. "Since you're interested in Google (Memory), you might want to know that they just opened a new office in USA (Global Knowledge)."


3. Handling Forgetting and Updates

Memory isn't static. If Sudeep says "I no longer work at Google," the graph must reflect this.

  • The Event Chain: Instead of deleting the edge, we add an endDate or a [:WAS_MEMBER_OF] relationship (See Module 4, Lesson 3 for Temporal Graphs).

This allows the agent to maintain Contextual History: "I see you USED to work at Google, but you mentioned you're job hunting now. Should I find similar companies?"

graph TD
    U((Current Session)) -->|Write| M[(Graph Memory)]
    M -->|Linked To| GK[(Global Knowledge)]
    
    subgraph "The Persistent Agent"
    M --- GK
    end
    
    U -->|Read| M
    M -->|Context| A[Agent Reasoning]
    
    style M fill:#f4b400,color:#fff
    style GK fill:#4285F4,color:#fff

4. Implementation: Writing Memory to Neo4j in Python

Let's look at a simple LangChain tool that an agent can call to "Remember" a fact.

from langchain.tools import tool

@tool
def remember_fact(entity: str, relationship: str, target: str):
    """Saves a new fact about the user to the persistent memory graph."""
    query = f"""
    MERGE (u:User {{id: 'current_user'}})
    MERGE (t:Entity {{name: '{target}'}})
    MERGE (u)-[r:{relationship}]->(t)
    SET r.last_updated = timestamp()
    """
    graph.query(query)
    return f"I have remembered that {entity} {relationship} {target}."

# The Agent uses this tool whenever it detects a 'Personal Fact' 
# in the conversation.

5. Summary and Exercises

Graph memory is the key to Personalized AI.

  • Extraction Agents identifies personal facts during chat.
  • Upsert Logic ensures the user node is always current.
  • Global Linking connects personal interests to broad data.
  • Persistence allows for multi-session continuity.

Exercises

  1. Memory Schema: What 3 relationship types would you create for a "Health Insurance Agent" to remember about a client? (e.g., [:SUFFERS_FROM], [:LIVES_IN]).
  2. Privacy Challenge: How would you handle a user saying "Forget everything you know about me"? Is it a DETACH DELETE of the User node?
  3. Visualization: Draw a graph linking "You" to 3 of your favorite "Skills" and 2 "Previous Jobs." This is essentially what your agent's memory would look like.

In the next lesson, we will move to the "Advanced" orchestrator: Multi-Step Reasoning with LangGraph.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn