DEV Community

Benni Alencar
Benni Alencar

Posted on Originally published at benni-os.net

Bi-Temporal Knowledge Graphs for AI Agents: Cutting Context Blowout by 74% in Python

Standard vector RAG (Retrieval-Augmented Generation) for AI agents suffers from two fatal flaws:

  1. Temporal Confusion: If a user changed their email address three times over 6 months, vector cosine similarity frequently retrieves the old, invalid email address.
  2. Context Window Blowout: Stuffing entire conversational transcripts into prompt context burns tens of thousands of tokens and causes catastrophic forgetting.

To solve this, we engineered agent-memory-mesh — a local-first Bi-Temporal Knowledge Graph for autonomous agents.


Bi-Temporal Time Axes Explained

Every memory fact in agent-memory-mesh is indexed along two independent dimensions:

  • Valid Time: When the fact was actually true in the real world.
  • Transaction Time: When the agent recorded the fact into memory.

This eliminates historical contradictions: the agent knows what was true in March 2026 vs what is true today.


74% Lossless Context Compression

Instead of storing raw text transcripts, conversational turns are distilled into semantic graph triples:

(Subject, Predicate, Object, ValidTimeRange, Confidence, MerkleHash)
Enter fullscreen mode Exit fullscreen mode

This achieves a measured 74% reduction in token storage while preserving 100% factual accuracy.


Quick Usage in Python

Install via pip:

pip install agent-memory-mesh
Enter fullscreen mode Exit fullscreen mode
from agent_memory_mesh import MemoryMesh

mesh = MemoryMesh.create_local("./agent_memory.db")

# Store fact with automatic bi-temporal stamps
mesh.remember(
    subject="User104",
    predicate="auth_token_format",
    object_val="Ed25519",
    confidence=1.0
)

# Query current point-in-time truth
fact = mesh.query(subject="User104", predicate="auth_token_format")
print(f"Verified Fact: {fact.value} (Recorded: {fact.transaction_time})")
Enter fullscreen mode Exit fullscreen mode

Sovereign Ecosystem & Open Source

Top comments (0)