How to build AI agent memory
Context graphs · 7 min read
An agent without memory re-derives the world every turn. The naive fixes — replay the transcript, summarise it, stuff documents into context — all degrade as the agent runs longer. Durable agent memory is a database problem, and because remembering means connecting (who said what, what depends on what, what changed), it's specifically a graph problem.
Why transcripts don't scale
Replaying conversation history is the default memory model, and it fails in three ways at once. Cost: context grows with every turn, and you pay for all of it on each call. Recall: the fact you need is buried in turn 14 of a session from last week, and attention over a very long context is unreliable. Structure: a transcript records what was said, not what is true — nothing marks which statements were corrected, superseded or acted on.
Summarisation trades those problems for a lossier one: the summary keeps what seemed important at summarisation time, not what the next question will need.
Memory is a graph problem
What an agent actually needs to remember is relational. The user's preference relates to a project; the incident relates to a service, which depends on two others; today's decision supersedes last month's. Store those as typed relationships and recall becomes a query: "everything known about the entities in play, newest first, with sources." No similarity guessing, no replay — a traversal that returns the same answer every time it's asked.
A schema that works
Four node types cover most agent-memory needs. Entity: the durable things — people, services, accounts, projects. Fact: a statement with an observed_at timestamp, linked ABOUT the entities it concerns. Source: where the fact came from — a conversation, document or API call — linked by SOURCED_FROM. Session: the run that produced the observations, so memory can be scoped or expired per session.
Corrections don't delete: a new fact with a SUPERSEDES edge to the old one preserves the history while queries default to the newest. That gives you time-travel and auditability for free.
The write path: observe, extract, MERGE
After each meaningful step, the agent (or a cheap extraction pass) turns what happened into entities and facts and writes them with MERGE — get-or-create semantics, so observing the same entity twice converges instead of duplicating. Writes in CognoDB are ACID, so a fact and its edges land atomically.
MERGE (e:Entity {name: $entity})
CREATE (f:Fact {statement: $statement, observed_at: datetime()})
MERGE (s:Source {id: $source_id, kind: $source_kind})
CREATE (f)-[:ABOUT]->(e)
CREATE (f)-[:SOURCED_FROM]->(s)The read path: traverse the neighbourhood
At the start of a turn, resolve the entities the query mentions (exact match, or CognoDB's built-in BM25 full-text search for fuzzy names), then walk outward: facts about those entities, their sources, and the entities one hop further that recent facts connect to. The result is a compact, citable context block whose size is set by the traversal you wrote — not by how long the agent has been alive.
CALL db.index.fulltext.queryNodes('entities', $q) YIELD node AS e
MATCH (e)<-[:ABOUT]-(f:Fact)-[:SOURCED_FROM]->(src)
WHERE NOT (f)<-[:SUPERSEDES]-(:Fact)
RETURN e.name, f.statement, src.kind, f.observed_at
ORDER BY f.observed_at DESC
LIMIT 30One agent, many agents
Memory topology is an architectural choice, and instances cheap enough to provision per agent make both patterns practical. Isolated: one CognoDB instance per agent, session or tenant — separate credentials, separate failure domains, nothing to leak between customers. Shared: a fleet of agents reading and writing one graph as a blackboard, where one agent's observation is immediately traversable by the others.
Either way the agent connects with any Bolt driver, or queries its memory directly through the built-in MCP server — the agent reads the schema and writes its own Cypher, and the query it ran is the citation for what it recalled.
Questions
Common questions.
What's the best way to give an AI agent persistent memory?
Store observations as structured data, not transcript text: entities, timestamped facts about them, and sources, connected as a graph. Recall becomes a bounded traversal instead of replaying history, so cost stays flat as the agent runs longer and every recalled fact carries provenance.
How is graph memory different from vector memory?
Vector memory retrieves past text by semantic similarity — good for "have I seen something like this?" Graph memory retrieves by relationship — "what do I know about this entity, and how do I know it?" Similarity can't follow dependencies or supersedence; a traversal can. Many stacks use both, with the graph as the system of record.
How do agents share memory without interfering?
Give collaborating agents one shared graph (a blackboard) and give mutually-untrusted agents separate instances. CognoDB instances provision in seconds with their own credentials, so per-agent or per-tenant isolation is a real database boundary, not a filter in queries.
How does the agent handle corrections — facts that change?
Write the new fact with a SUPERSEDES edge to the old one rather than deleting. Recall queries filter to non-superseded facts by default, while the full history stays traversable for auditing and for questions like "what did we believe at the time?"
Does this require a special agent framework?
No. Any framework that can call a database works: run Cypher over Bolt with the official Neo4j drivers, or point an MCP-capable agent (Claude, Cursor and others) at CognoDB's built-in MCP server and let it query memory directly.
Keep reading
Context graphs
What is a context graph?
A context graph is the connected, queryable record of what an AI system currently knows: entities, facts, sources and time. How it differs from a knowledge graph, and how agents read and write one.
ReadContext graphs
Cut LLM token costs
Context-stuffing costs grow with your knowledge base; graph retrieval stays bounded by the neighbourhood you traverse. The measured numbers: 202,285 tokens per query down to 2,668, at 2,000 entities.
ReadGraphRAG
What is GraphRAG?
GraphRAG grounds an LLM by traversing a context graph instead of retrieving text chunks by similarity alone. Here's how it works, why it improves multi-hop answers, and how to build it.
ReadStart now
~98.7%
token efficiency at 2,000 entities (see the footnotes above)
Try the ideas on a real graph.
A free instance takes about a minute and no card. Every Cypher snippet on this page runs against it unchanged.
First-graph path
LiveCreate a free instance
No card. Ready in about a minute.
Connect your driver
bolt+s:// URI into the driver you already use.
Write two MERGEs
That's the entire shape of agent memory.
Point an agent at it
One MCP config block. No integration code.