Drop-in context management API. Models your conversation as a semantic graph and retrieves only the most relevant messages at inference time — cutting token usage without losing coherence.
from graphmem import GraphMemClient gm = GraphMemClient("https://api.graphmem.dev", api_key="gm_sk_...") conv_id = gm.conversations.create()["conversation_id"] # Store a message pair mid = gm.memory.add_prompt(conv_id, user_message) ctx = gm.memory.recall(conv_id, query=user_message) reply = your_llm.chat(ctx["messages"]) gm.memory.add_response(mid, conv_id, reply)
Every message is embedded and wired into a semantic graph. At inference time, GraphMem traverses that graph to surface the most relevant context — automatically.
The default retrieval path. For every query, GraphMem runs a multi-set graph traversal across your conversation history and returns a token-budgeted, timestamp-ordered context window — ready to inject into your LLM.
result = gm.memory.recall( conv_id, query = "user's latest message", k = 5, strategy = "semantic", always_include_recent = 2, max_tokens = 2000, ) # result.messages → ordered list # result.total_tokens → token count # result.strategy_used # inject directly into your LLM response = llm.chat( context=result.messages, query=user_message, )
As your conversation grows, GraphMem clusters messages into topics using Louvain community detection. These topic APIs give you fine-grained control over what goes into your LLM's context — and how many tokens you spend.
topics.relevance() then
topics.messages() for each top-K topic in one call.
The most token-efficient retrieval path GraphMem offers.
Use when your conversation has matured enough to have multiple
distinct topics and you want maximum context precision.
status() to skip resolved topics entirely
# Two-stage: topic → messages ctx = gm.topics.retrieve( conv_id, query = user_message, top_n = 3, max_tokens = 1500, ) # Or use summary instead of raw messages for topic in gm.topics.list(conv_id): summary = gm.topics.summary(topic["topic_id"]) status = gm.topics.status(topic["topic_id"])
Install the SDK, create a conversation, start storing and recalling.
1 · Install
pip install graphmem-sdk
2 · Use
from graphmem import GraphMemClient gm = GraphMemClient( "https://api.graphmem.dev", api_key="gm_sk_...", ) # Create a conversation (one per user session) conv_id = gm.conversations.create()["conversation_id"] # Your agent loop while True: user_msg = input("You: ") # 1. Store the prompt, get memory_id back mid = gm.memory.add_prompt(conv_id, user_msg) # 2. Recall relevant context from the graph ctx = gm.memory.recall(conv_id, query=user_msg, max_tokens=2000) # 3. Call your LLM with the relevant context reply = your_llm.chat(context=ctx["messages"], query=user_msg) # 4. Store the response to complete the memory node gm.memory.add_response(mid, conv_id, reply) print(f"Agent: {reply}")