Skip to main content

Store User Preference

from memoclaw import MemoClaw

client = MemoClaw()

# After user mentions a preference
client.store(
    "User prefers dark mode",
    importance=0.8,
    tags=["preferences", "ui"],
)

Store After Correction

# User corrects you - high importance
client.store(
    "Correction: The database connection uses port 5433, not 5432",
    importance=0.95,
    tags=["corrections"],
)

Recall Before Responding

# Before assuming user preferences
memories = client.recall("user's theme preference")
if memories.memories:
    theme = memories.memories[0].content
    print(f"User prefers: {theme}")

Filter by Namespace

# Project-specific recall
memories = client.recall(
    "database configuration",
    namespace="project-api",
    limit=10,
)

Batch Import

# Import multiple memories at once
memories = [
    {"content": "Uses PostgreSQL 15", "importance": 0.9},
    {"content": "Deploys to Railway", "importance": 0.8},
    {"content": "Team of 3 developers", "importance": 0.7},
]

result = client.store_batch(memories)
print(f"Stored {result.count} memories")

Extract Facts from Conversation

# Automatically extract facts from chat
result = client.ingest(
    messages=[
        {"role": "user", "content": "I prefer dark mode and use vim."},
        {"role": "assistant", "content": "Got it!"},
        {"role": "user", "content": "My timezone is PST."},
    ],
    auto_relate=True,
)
print(f"Extracted {result.facts_extracted} facts")
print(f"Created {result.relations_created} relations")

Proactive Memory Suggestions

# Get memories that need attention
suggested = client.suggested(category="stale", limit=5)

for memory in suggested.memories:
    print(f"- {memory.content[:50]}... (importance: {memory.importance})")

Memory Relationships

# Create relationship between memories
client.create_relation(
    memory_id="uuid-1",
    target_memory_id="uuid-2",
    relation_type="related_to",
)

# List all relations for a memory
relations = client.list_relations("uuid-1")

Async Usage

import asyncio
from memoclaw import AsyncMemoClaw

async def main():
    async with AsyncMemoClaw() as client:
        # Store concurrently
        await asyncio.gather(
            client.store("Preference 1", importance=0.8),
            client.store("Preference 2", importance=0.7),
        )
        
        # Recall
        results = await client.recall("preferences")

asyncio.run(main())

Error Handling

from memoclaw import MemoClaw, NotFoundError, RateLimitError, ValidationError

try:
    client.delete("some-id")
except NotFoundError:
    print("Memory already deleted")
except RateLimitError:
    print("Rate limited - wait and retry")
except ValidationError as e:
    print(f"Validation error: {e.message}")

Rate Limit Handling

import time
from memoclaw import MemoClaw, RateLimitError

client = MemoClaw()

def recall_with_backoff(query, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.recall(query)
        except RateLimitError:
            if attempt == max_retries - 1:
                raise
            wait_time = 2 ** attempt  # 1, 2, 4 seconds
            time.sleep(wait_time)