Skip to main content

Cross-session memory store for agents, backed by Memgraph

Project description

Sessions Graph

Sessions Graph is the Context Graph component for session context and cross-session recall. It is the authority on (:Session) nodes in the Context Graph family. It stores free-form text assertions — called Memories — written explicitly by agents, and makes them searchable in future sessions.

Requires Memgraph ≥ 3.6 (text search is stable from that release).

Installation

pip install sessions-graph

To use with Agent Context Graph:

pip install sessions-graph[agent-context-graph]

Quick start

from sessions_graph import SessionsGraph

graph = SessionsGraph()   # connects via MEMGRAPH_HOST / MEMGRAPH_PORT env vars
graph.setup()           # creates constraints and the text index (run once)

# Write a memory
mem = graph.save_memory(
    user_id="alice",
    content="Prefers Python over TypeScript",
    session_id="s-abc123",   # optional — links memory to a session for provenance
)

# Retrieve all memories for a user
memories = graph.get_memories("alice")

# Search memories by content (full-text, powered by Tantivy)
results = graph.search_memories("alice", "Python")

# Update or delete
graph.update_memory(mem.memory_id, "Prefers Python, especially for data tooling")
graph.delete_memory(mem.memory_id)

Integration with Agent Context Graph

Wire the SessionsGraphConnector into an AgentLink to get automatic session provenance — the connector tracks the active session_id and user_id from SessionStartEvent so you can reference them when saving memories.

from sessions_graph import SessionsGraph
from sessions_graph.connector import SessionsGraphConnector
from agent_context_graph import AgentLink
from agent_context_graph.adapters.claude import ClaudeAdapter

graph = SessionsGraph()
graph.setup()

connector = SessionsGraphConnector(graph)
link = AgentLink()
link.add_connector(connector)

adapter = ClaudeAdapter(
    link,
    session_id="s-abc123",
    session_kwargs={"user_id": "alice"},
)

# During the session, save memories via the Python API:
graph.save_memory(
    user_id=connector.active_user_id,
    content="User works primarily in the ai-toolkit repository",
    session_id=connector.active_session_id,
)

Graph schema

(:User {user_id})
    └─[:HAS_MEMORY]─▶ (:Memory {memory_id, user_id, content, created_at})
                              ▲
              [:PRODUCED_MEMORY]
                              │
                      (:Session {session_id})   ← shared idempotent coordination point

Text search

Sessions Graph uses Memgraph text search (powered by Tantivy) for search_memories. The text index is created on setup():

CREATE TEXT INDEX memory_content_index ON :Memory(content);

Searches run as:

CALL text_search.search_all('memory_content_index', 'Python')
YIELD node AS m, score
WHERE m.user_id = 'alice'
RETURN m.content, score
ORDER BY score DESC
LIMIT 10;

The query string follows Tantivy query syntax.

API reference

Method Description
setup() Create constraints and text index. Run once on first use.
drop() Remove all Memory-related constraints and indexes.
save_memory(user_id, content, *, session_id, memory_id) Persist a new Memory. Returns the stored Memory object.
get_memories(user_id) Return all Memories for a user, newest first.
search_memories(user_id, query, *, limit=10) Full-text search over Memory content.
update_memory(memory_id, content) Replace the content of an existing Memory. Returns None if not found.
delete_memory(memory_id) Remove a Memory and all its relationships.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sessions_graph-0.1.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sessions_graph-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file sessions_graph-0.1.0.tar.gz.

File metadata

  • Download URL: sessions_graph-0.1.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sessions_graph-0.1.0.tar.gz
Algorithm Hash digest
SHA256 877c3a55d1595c0b3dc31796a8ffa731e1343916c0a56325279f8017c47b94ad
MD5 d8687b386d9ad2a47c49fe9918dc9557
BLAKE2b-256 f3fb13d44d4a95cb281be5ca50ca70fc1e1a872c9caca5cde7d6ff18c87cddb8

See more details on using hashes here.

File details

Details for the file sessions_graph-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sessions_graph-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sessions_graph-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12a06449142e70e65fe7f9adcf8cef732737af2d07697f22a7f0f91bcf04b94d
MD5 f9595a8541c48c6ac455f0f814e7b278
BLAKE2b-256 3bfeb621815bf17658d2195522ae29c4953119f32fb85c65f779a820ab7c251f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page