MemoryClient API Reference
The main entry point for all memory operations.
Overview
MemoryClient manages the Neo4j connection and provides access to all three memory types.
from neo4j_agent_memory import MemoryClient, MemorySettings
settings = MemorySettings(
neo4j={"uri": "bolt://localhost:7687", "password": "password"}
)
async with MemoryClient(settings) as client:
# Access memory types
client.short_term # ShortTermMemory
client.long_term # LongTermMemory
client.reasoning # ReasoningMemory
Constructor
MemoryClient(
settings: MemorySettings,
embedder: Embedder | None = None,
extractor: EntityExtractor | None = None,
resolver: EntityResolver | None = None,
)
Parameters
| Parameter | Type | Description |
|---|---|---|
|
|
Configuration settings (required) |
|
|
Custom embedder (optional, created from settings if not provided) |
|
|
Custom extractor (optional, created from settings if not provided) |
|
|
Custom resolver (optional, created from settings if not provided) |
Properties
| Property | Type | Description |
|---|---|---|
|
|
Access to conversation and message operations |
|
|
Access to entity, preference, and fact operations |
|
|
Access to reasoning trace operations |
|
|
Current configuration settings |
|
|
Access the underlying Neo4j graph client for custom Cypher queries |
Methods
connect
Establish connection to Neo4j.
await client.connect()
Called automatically when using async with.
|
close
Close the Neo4j connection.
await client.close()
Called automatically when exiting async with.
|
graph
Access the underlying Neo4jClient for custom Cypher queries alongside memory operations.
This allows applications to query domain-specific data stored in the same Neo4j database without creating a separate database connection.
async with MemoryClient(settings) as client:
# Run custom Cypher queries on the same connection used for memory
results = await client.graph.execute_read(
"MATCH (c:Customer {id: $id}) RETURN c.name AS name",
{"id": "CUST-001"}
)
# Write operations
await client.graph.execute_write(
"CREATE (n:MyNode {name: $name})",
{"name": "example"}
)
The Neo4jClient provides:
-
execute_read(query, parameters)- Run read-only Cypher queries -
execute_write(query, parameters)- Run write Cypher queries -
execute_batch(queries)- Run multiple queries in a single transaction -
vector_search(…)- Run vector similarity searches
|
Raises |
get_context
Get combined context from all memory types for LLM prompts.
context = await client.get_context(
query: str,
session_id: str | None = None,
user_id: str | None = None,
limit: int = 10,
include_short_term: bool = True,
include_long_term: bool = True,
include_reasoning: bool = True,
)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
|
|
required |
Search query for semantic matching |
|
|
|
Filter to specific session |
|
|
|
Filter to specific user |
|
|
|
Maximum items per memory type |
|
|
|
Include recent messages |
|
|
|
Include entities and preferences |
|
|
|
Include similar reasoning traces |
get_stats
Get memory statistics.
stats = await client.get_stats()
Returns
@dataclass
class MemoryStats:
short_term: ShortTermStats
long_term: LongTermStats
reasoning: ReasoningStats
@dataclass
class ShortTermStats:
conversation_count: int
message_count: int
@dataclass
class LongTermStats:
entity_count: int
entity_counts_by_type: dict[str, int]
preference_count: int
fact_count: int
@dataclass
class ReasoningStats:
trace_count: int
step_count: int
tool_call_count: int
Context Manager
The recommended way to use MemoryClient is as an async context manager:
async with MemoryClient(settings) as client:
# Connection is established
await client.short_term.add_message(...)
# Connection is closed automatically
This ensures proper connection handling and resource cleanup.
Thread Safety
MemoryClient is not thread-safe. Create separate instances for concurrent access:
# In async context - OK (single thread)
async with MemoryClient(settings) as client:
await asyncio.gather(
client.short_term.add_message(...),
client.long_term.add_entity(...),
)
# For multiple threads - create separate clients
async def worker(settings):
async with MemoryClient(settings) as client:
await client.short_term.add_message(...)