Python SDK for Memtrace — LLM-agnostic memory layer for AI agents
Project description
Memtrace Python SDK
Python client for Memtrace — LLM-agnostic memory layer for AI agents.
Installation
pip install memtrace
Quick Start
from memtrace import Memtrace
client = Memtrace("http://localhost:9100", "mtk_your_api_key")
# Store a memory
client.remember("agent_1", "User prefers dark mode")
# Recall recent memories
memories = client.recall("agent_1", since="24h")
for m in memories.memories:
print(f"[{m.time}] {m.content}")
# Log a decision
client.decide("agent_1", "Use PostgreSQL", "Better JSON support for metadata")
Async Support
from memtrace import AsyncMemtrace
async with AsyncMemtrace("http://localhost:9100", "mtk_your_api_key") as client:
await client.remember("agent_1", "User prefers dark mode")
memories = await client.recall("agent_1")
Full API
Memory Operations
from memtrace import Memtrace, AddMemoryRequest, ListOptions, SearchQuery
client = Memtrace("http://localhost:9100", "mtk_...")
# Add a single memory with full control
mem = client.add_memory(AddMemoryRequest(
agent_id="agent_1",
session_id="sess_1",
memory_type="episodic",
event_type="observation",
content="User clicked the settings button",
tags=["ui", "navigation"],
importance=0.7,
))
# Add multiple memories in a batch
memories = client.add_memories([
AddMemoryRequest(agent_id="agent_1", memory_type="episodic", event_type="general", content="First"),
AddMemoryRequest(agent_id="agent_1", memory_type="episodic", event_type="general", content="Second"),
])
# List with filters
result = client.list_memories(ListOptions(
agent_id="agent_1",
memory_type="decision",
since="7d",
limit=50,
order="desc",
))
# Search with structured query
result = client.search_memories(SearchQuery(
agent_id="agent_1",
memory_types=["episodic", "decision"],
content_contains="dark mode",
min_importance=0.5,
))
Agent Management
from memtrace import Memtrace, RegisterAgentRequest
client = Memtrace("http://localhost:9100", "mtk_...")
# Register an agent
agent = client.register_agent(RegisterAgentRequest(
name="my-agent",
description="Handles customer support",
config={"model": "gpt-4"},
))
# Get agent details
agent = client.get_agent("agent_1")
# Get agent memory stats
stats = client.get_agent_stats("agent_1")
print(f"Total memories: {stats.memory_count}")
print(f"Active sessions: {stats.active_sessions}")
Session Management
from memtrace import Memtrace, CreateSessionRequest, ContextOptions
client = Memtrace("http://localhost:9100", "mtk_...")
# Create a session
session = client.create_session(CreateSessionRequest(
agent_id="agent_1",
metadata={"task": "onboarding"},
))
# Get LLM-formatted context
ctx = client.get_session_context(session.id, ContextOptions(
since="2h",
include_types=["episodic", "decision"],
max_tokens=4000,
))
print(ctx.context) # Markdown-formatted for LLM consumption
# Close the session
client.close_session(session.id)
Error Handling
from memtrace import Memtrace, MemtraceError, AuthenticationError, NotFoundError, ConflictError
client = Memtrace("http://localhost:9100", "mtk_...")
try:
agent = client.get_agent("nonexistent")
except NotFoundError:
print("Agent not found")
except AuthenticationError:
print("Invalid API key")
except ConflictError:
print("Duplicate resource")
except MemtraceError as e:
print(f"API error ({e.status_code}): {e.message}")
Development
cd sdks/python
pip install -e ".[dev]"
pytest -v
ruff check src/ tests/
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
memtrace_sdk-0.1.0.tar.gz
(8.8 kB
view details)
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 memtrace_sdk-0.1.0.tar.gz.
File metadata
- Download URL: memtrace_sdk-0.1.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93e47934419862c9ab1567e1b3bf6edbeeda9c7776d9ca42250b9833a155e1d0
|
|
| MD5 |
ae18119a68c77a5f28e43be73b59a4c3
|
|
| BLAKE2b-256 |
30b79ecebf07b8b5697faccac446c938cd547a44c7b12dbacf80a6b6f19002bc
|
File details
Details for the file memtrace_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: memtrace_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cac8d81c02129ca985ee581e5ba74ea90ac2897e519a1abf37801fea0e9665c5
|
|
| MD5 |
c6c1dfe274dd39b6b416c757ced43e84
|
|
| BLAKE2b-256 |
9749871164c808e158980a92d7fa17faadf9f61d02a122c60b5474cd7b22d707
|