Knowledge graph memory system for AI agents
Project description
TraceMem Core
Knowledge graph memory system for AI agents. Stores coding interactions in a hybrid graph + vector store for contextual recall.
Installation
pip install tracemem-core
Requirements
- Python 3.12+
- OpenAI API key (for embeddings, or provide a custom embedder)
- No external services required — uses embedded Kuzu (graph) and LanceDB (vectors) by default
Optional: Neo4j 5.x for remote graph storage (pip install tracemem-core[neo4j])
Quick Start
from tracemem_core import TraceMem, TraceMemConfig, Message, ToolCall
config = TraceMemConfig(
graph_store="kuzu", # "kuzu" (embedded, default) or "neo4j"
# home=Path("~/.tracemem"), # storage directory (default: ~/.tracemem)
)
async with TraceMem(config=config) as tm:
# Store a conversation
await tm.add_message("conv-1", Message(role="user", content="Fix the auth bug in login.py"))
await tm.add_message("conv-1", Message(
role="assistant",
content="I'll check the authentication code...",
tool_calls=[ToolCall(id="c1", name="read_file", args={"path": "src/login.py"})],
))
await tm.add_message("conv-1", Message(
role="tool", content="def login(): ...", tool_call_id="c1",
))
# Search similar past interactions
results = await tm.search("authentication issues")
for r in results:
print(r.text, r.score)
# Get full trajectory from a user message
trajectory = await tm.get_trajectory(results[0].node_id)
for step in trajectory.steps:
print(step.node_type, step.text[:80], step.tool_uses)
Configuration
All settings use the TRACEMEM_ prefix:
export OPENAI_API_KEY="sk-..." # Required for embeddings
export TRACEMEM_GRAPH_STORE="kuzu" # kuzu (default) or neo4j
export TRACEMEM_HOME="~/.tracemem" # Storage directory
export TRACEMEM_EMBEDDING_MODEL="text-embedding-3-small"
export TRACEMEM_RERANKER="rrf" # rrf (default) or linear
TraceMemConfig Options
| Option | Type | Default | Description |
|---|---|---|---|
graph_store |
"kuzu" | "neo4j" |
"kuzu" |
Graph backend |
home |
Path |
~/.tracemem |
Storage directory for graph + vectors |
embedding_model |
str |
"text-embedding-3-small" |
OpenAI embedding model |
embedding_dimensions |
int |
1536 |
Embedding vector dimensions |
openai_api_key |
str |
None |
OpenAI API key (or use env var) |
reranker |
str |
"rrf" |
Reranker strategy (rrf or linear) |
neo4j_uri |
str |
"bolt://localhost:7687" |
Neo4j URI (only if graph_store="neo4j") |
neo4j_user |
str |
"neo4j" |
Neo4j username |
neo4j_password |
str |
"password" |
Neo4j password |
neo4j_database |
str |
"neo4j" |
Neo4j database name |
namespace |
str |
None |
Namespace for multi-user isolation (Neo4j) |
Graph Schema
Node Types
| Node | Key Properties | Description |
|---|---|---|
UserText |
id, text, conversation_id, turn_index |
User messages (searchable via vector store) |
AgentText |
id, text, tool_uses, conversation_id, turn_index |
Agent responses and tool invocations |
ResourceVersion |
id, uri, content_hash |
Snapshot of a resource at a point in time |
Resource |
id, uri, current_content_hash |
Canonical resource identity (deduplicated by URI) |
Relationship Types
Only 3 relationship types:
| Type | From → To | Description |
|---|---|---|
MESSAGE |
UserText ↔ AgentText | Conversation flow (chronological) |
TOOL_USE |
AgentText → ResourceVersion | Agent used a tool that touched this resource |
VERSION_OF |
ResourceVersion → Resource | Links snapshot to canonical resource |
API Reference
TraceMem
Must be used as an async context manager:
async with TraceMem(config=config, embedder=embedder, resource_extractor=extractor) as tm:
...
Parameters:
config(TraceMemConfig, optional): Configuration settingsembedder(Embedder, optional): Custom embedder (defaults to OpenAI)resource_extractor(ResourceExtractor, optional): Custom resource URI extractor
await tm.add_message(conversation_id, message)
Add a single message to the knowledge graph.
await tm.import_trace(conversation_id, messages)
Import a full conversation from a list of Messages.
await tm.search(query, config=None)
Search for similar past interactions via vector similarity.
await tm.get_trajectory(node_id)
Get the full trajectory from a UserText node through all agent responses until the next user message.
Data Models
from tracemem_core import Message, ToolCall
Message(role="user", content="Fix the bug")
Message(role="assistant", content="...", tool_calls=[ToolCall(id="c1", name="read_file", args={"path": "auth.py"})])
Message(role="tool", content="file contents...", tool_call_id="c1")
Resource Extraction
The DefaultResourceExtractor extracts URIs from tool call arguments:
- File arguments:
path,file_path,filepath,file,filename - URL arguments:
url,uri,endpoint
ToolCall(name="read_file", args={"path": "src/auth.py"}) # → file://src/auth.py
ToolCall(name="fetch", args={"url": "https://api.example.com"}) # → https://api.example.com
Custom Extractor
from tracemem_core import ResourceExtractor
class MyExtractor:
def extract(self, tool_name: str, args: dict) -> str | None:
if tool_name == "query_database":
return f"db://{args.get('table')}"
return None
async with TraceMem(resource_extractor=MyExtractor()) as tm:
...
Custom Embedder
from tracemem_core import Embedder, TraceMem
class MyEmbedder:
@property
def dimensions(self) -> int:
return 768
async def embed(self, text: str) -> list[float]:
return [0.1] * 768
async def embed_batch(self, texts: list[str]) -> list[list[float]]:
return [await self.embed(t) for t in texts]
async with TraceMem(embedder=MyEmbedder()) as tm:
...
Adapters
LangChain
from tracemem_core.adapters.langchain import LangChainAdapter
adapter = LangChainAdapter()
messages = adapter.convert(langchain_messages)
await tm.import_trace("conv-123", messages)
Requires langchain-core: pip install langchain-core
Documentation
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tracemem_core-0.1.3.tar.gz.
File metadata
- Download URL: tracemem_core-0.1.3.tar.gz
- Upload date:
- Size: 98.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72eac3faa02eccaa9ca37b52f3b40fdcb5498b5249890f34101250f62ac8281b
|
|
| MD5 |
4e4dab7a1f449813dab66228f37ada40
|
|
| BLAKE2b-256 |
8cbd850067068afc4a29d120434878fc8e87f8396f29bef78f74550b82b7bd41
|
Provenance
The following attestation bundles were made for tracemem_core-0.1.3.tar.gz:
Publisher:
release.yml on itay1542/tracemem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tracemem_core-0.1.3.tar.gz -
Subject digest:
72eac3faa02eccaa9ca37b52f3b40fdcb5498b5249890f34101250f62ac8281b - Sigstore transparency entry: 929998568
- Sigstore integration time:
-
Permalink:
itay1542/tracemem@48891e67853235afac963ba4591bdcd77a790ca4 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/itay1542
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@48891e67853235afac963ba4591bdcd77a790ca4 -
Trigger Event:
pull_request
-
Statement type:
File details
Details for the file tracemem_core-0.1.3-py3-none-any.whl.
File metadata
- Download URL: tracemem_core-0.1.3-py3-none-any.whl
- Upload date:
- Size: 41.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a004237d4fe621fdd3e89fb2d7be89f364b56f0ad36b49cae33b909afe7b4df
|
|
| MD5 |
a0a520de58e9d2d591155d9e77425e20
|
|
| BLAKE2b-256 |
784a424aa2b65e433d62398fce8ac18c221a6281d72cc8aa3ccebad4e20ed419
|
Provenance
The following attestation bundles were made for tracemem_core-0.1.3-py3-none-any.whl:
Publisher:
release.yml on itay1542/tracemem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tracemem_core-0.1.3-py3-none-any.whl -
Subject digest:
9a004237d4fe621fdd3e89fb2d7be89f364b56f0ad36b49cae33b909afe7b4df - Sigstore transparency entry: 929998584
- Sigstore integration time:
-
Permalink:
itay1542/tracemem@48891e67853235afac963ba4591bdcd77a790ca4 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/itay1542
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@48891e67853235afac963ba4591bdcd77a790ca4 -
Trigger Event:
pull_request
-
Statement type: