Knowledge Bases

Schema Design Patterns for Temporal Knowledge Graphs in AI Agent Memory

As AI agents evolve from simple chatbots to autonomous entities with long-term memory, the challenge of storing and retrieving context becomes paramount. Traditional key-value stores or vector databases often fail to capture the nuanced evolution of facts over time. To solve this, we turn to Temporal Knowledge Graphs (TKGs). These structures allow agents to understand not just what is true, but when it became true, when it stopped being true, and how confidence levels have shifted.

Designing an effective schema for a TKG requires balancing expressiveness with query performance. Below, we explore three critical patterns for modeling temporal data in agent memory systems.

1. The Interval-Based Fact Model

The most fundamental pattern involves explicitly storing valid time intervals for every assertion. Instead of treating a fact as a static boolean, we model it as a tuple: (Subject, Predicate, Object, Start_Time, End_Time, Confidence).

This approach is ideal for discrete state changes, such as an agent remembering that a user prefers Python (from Jan 2022 to Jun 2023) and then switched to Rust (from Jun 2023 to present).

{
  "id": "fact_9821",
  "subject": "user_001",
  "predicate": "preferred_language",
  "object": "python",
  "valid_from": "2022-01-15T10:00:00Z",
  "valid_to": "2023-06-01T00:00:00Z",
  "confidence": 0.95
}

Pro Tip: Ensure that when a new fact invalidates an old one, the system automatically updates the valid_to timestamp of the previous entry. This prevents contradictory "current" states from coexisting in query results.

2. Event-Centric Nodes for Granular Context

Interval models can become cluttered if facts change rapidly or are derived from specific interactions. A more robust pattern is to treat events as first-class nodes in the graph. The event holds the temporal context, and the fact is an edge connecting the event to the subject/object.

This is particularly useful for AI agents that need to trace why a belief exists. For example, the fact that "User is interested in Hiking" is linked to an event node "Conversational_turn_452" which occurred on 2024-05-12. This allows the agent to retrieve not just the fact, but the original conversation context if re-validation is needed.

class TemporalEvent:
    def __init__(self, event_id, timestamp, source_interaction):
        self.id = event_id
        self.timestamp = timestamp
        self.source = source_interaction

    def extract_fact(self, subject, predicate, object, duration):
        return Fact(
            subject=subject,
            predicate=predicate,
            object=object,
            valid_at=self.timestamp,
            decay_rate=duration
        )

3. Probabilistic Decay and Confidence Schemas

Agent memory is rarely 100% certain. Advanced TKG schemas incorporate a decay_function or confidence_score that decreases over time if not reinforced. This mimics human biological memory.

By storing the last_verified_at timestamp alongside the initial_observed_at, agents can prioritize recent, verified facts over stale ones. The schema should support a "soft delete" rather than a hard delete, allowing agents to re-engage with potentially forgotten facts if context re-emerges.

SELECT * FROM facts
WHERE subject = 'user_001'
AND valid_from <= NOW()
AND (valid_to IS NULL OR valid_to > NOW())
ORDER BY confidence DESC, last_verified_at DESC;

Conclusion

Designing temporal schemas for AI agents is less about choosing the right database engine and more about defining the semantics of time in your specific domain. Whether you opt for explicit intervals, event-centric nodes, or probabilistic decay, the goal remains the same: to give your agent a memory that respects the passage of time. By implementing these patterns, you enable agents that are not only knowledgeable but also contextual, adaptive, and historically aware.

Share: