Python SDK
Native Python client for programmatic memory management. Use it in custom agents, scripts, or anywhere you need memory access from Python.
Installation
pip install cortex-ai-mem
Quick Start
from cortex_memory import CortexClient
client = CortexClient(api_key="ctx_your_key_here")
# Save a memory
memory = client.save(
content="User prefers Python and dark mode",
tags=["preference"],
importance=0.8
)
# Search memories
results = client.search("user preferences", limit=5)
for memory in results:
print(f"{memory.content} (score: {memory.score})")
# Update a memory
client.update(
memory_id="mem_abc123",
content="User prefers Python, dark mode, and VS Code"
)
# Delete a memory
client.delete(memory_id="mem_abc123")Configuration
The client can be configured via constructor arguments or environment variables:
| Parameter | Env Variable | Description |
|---|---|---|
| api_key | CORTEX_API_KEY | Your API key (required) |
| base_url | CORTEX_API_URL | API base URL (default: https://cortex-mem.com/api) |
API Reference
client.save(content, tags=None, agent_id=None, importance=0.5, session_id=None)
Save a new memory. Returns the created memory object.
client.search(query, limit=10, tags=None, agent_id=None)
Search memories by semantic similarity. Returns a list of memory objects with scores.
client.update(memory_id, content=None, tags=None)
Update an existing memory's content or tags.
client.delete(memory_id)
Permanently delete a memory by ID.
Error Handling
from cortex_memory import CortexClient, CortexError
client = CortexClient(api_key="ctx_your_key_here")
try:
results = client.search("user preferences")
except CortexError as e:
print(f"API error: {e.status_code} - {e.message}")