Temporal Context Graphs for Agent Memory

Agentic AI Systems advanced Building with AI ~35 min read

Overview

Agent memory becomes unreliable when it is modeled as a flat pile of text chunks. Conversations, tickets, customer records, and operational events change over time: a user switches roles, a policy is superseded, a customer preference becomes stale, and a fact may be true during one interval but false later. A temporal context graph stores memory as entities, relationships, facts, and source episodes with explicit time and provenance so an agent can ask not only “what is true?” but also “when was it true?”

Graphiti, the open-source engine behind Zep’s temporal knowledge graph architecture, is a representative implementation of this pattern. It continuously integrates structured and unstructured data into a graph, tracks validity windows for facts, preserves provenance back to raw episodes, and supports hybrid retrieval that combines semantic search, keyword matching, and graph traversal.

This is distinct from ordinary GraphRAG over a mostly static document corpus. A temporal context graph is designed for evolving agent memory: it must accept frequent incremental updates, retain history instead of overwriting it blindly, and retrieve context that reflects the user’s current question and the relevant time horizon.

Core Concepts

Episodes as Provenance

An episode is the raw input that produced graph facts: a chat turn, email, support ticket, CRM row, log event, or imported document. Episodes are not discarded after extraction. They provide the audit trail for why the agent believes a fact and allow retrieved graph context to cite or reconstruct the original evidence.

Entities and Temporally Valid Facts

Entities represent durable things such as people, products, teams, accounts, services, and concepts. Facts are typed relationships between entities, often stored as edges. The temporal graph adds validity windows: when the fact became true, when it was superseded, and which episode introduced or changed it.

Key Concept: Memory Should Preserve Superseded Truth

If an account owner changes from Sam to Priya, deleting Sam’s old ownership loses useful history. A temporal graph closes the old edge’s validity interval and opens a new edge. The current answer uses Priya, while historical questions can still recover that Sam owned the account during the previous period.

How It Works

Incremental Ingestion

Each new episode is parsed into candidate entities and facts. The system resolves entities against existing nodes, adds new nodes when needed, and updates relationship edges. Unlike batch GraphRAG indexing, the graph must support frequent small writes because agent memory changes during normal interaction.

def ingest_episode(graph, episode):
    episode_id = graph.store_episode(
        text=episode.text,
        source=episode.source,
        observed_at=episode.observed_at,
    )

    extracted = extract_entities_and_facts(episode.text)

    for entity in extracted.entities:
        graph.upsert_entity(
            canonical_name=entity.name,
            entity_type=entity.type,
            evidence_episode=episode_id,
        )

    for fact in extracted.facts:
        subject = graph.resolve_entity(fact.subject)
        object_ = graph.resolve_entity(fact.object)
        graph.upsert_temporal_edge(
            subject=subject,
            predicate=fact.predicate,
            object=object_,
            valid_from=fact.valid_from or episode.observed_at,
            evidence_episode=episode_id,
        )

Hybrid Retrieval

Retrieval combines multiple signals. Semantic search finds episodes and entity summaries similar to the query. Keyword search catches exact names, identifiers, and error strings. Graph traversal expands from matched entities to related facts, neighboring entities, and supporting episodes. A reranker or scoring function then selects the compact context that should enter the agent’s prompt.

Practical Applications

Long-Term Personalization

Agents that work with users across months need to remember preferences, projects, constraints, and changes. A temporal graph can distinguish “the user used to prefer weekly reports” from “the user now prefers daily exception alerts” while preserving both facts for questions about history.

Enterprise Support and Operations

Support and operations agents need to connect customers, incidents, systems, owners, runbooks, and prior resolutions. Temporal edges let the agent answer questions such as who owned a service during a past incident or which policy version was active when a ticket was filed.

Implementation Patterns

Bitemporal Edge Records

Robust implementations separate event time from ingestion time. Event time records when a fact was true in the world. Ingestion time records when the system learned or revised that fact. This distinction matters when delayed imports, corrections, or backfilled records arrive after the real-world event.

CREATE TABLE memory_edges (
    edge_id TEXT PRIMARY KEY,
    subject_id TEXT NOT NULL,
    predicate TEXT NOT NULL,
    object_id TEXT NOT NULL,
    valid_from TIMESTAMP NOT NULL,
    valid_to TIMESTAMP,
    known_from TIMESTAMP NOT NULL,
    known_to TIMESTAMP,
    evidence_episode_id TEXT NOT NULL,
    confidence REAL NOT NULL
);

Supersession Instead of Overwrite

When a new fact conflicts with an old fact about the same subject and predicate, close the old edge’s validity interval and insert a new edge. The graph keeps historical continuity while making current-state retrieval straightforward.

Performance Considerations

Common Pitfalls

Further Reading