Storing Memories
Learn how to store user memories with proper scoping, domains, and data organization.
Storing memories is the first step in using the Memory Scope API. When you create a memory, you specify:
- Scope: The category of memory (preferences, constraints, etc.)
- Domain: Optional sub-category for organization
- Value: The actual memory data as JSON
- Source: Where the memory came from
- TTL: Optional expiration time
Basic Example
from memory_scope import MemoryScopeClient
client = MemoryScopeClient(api_key="your-api-key")
# Store a user preference
memory = client.create_memory(
user_id="user123",
scope="preferences",
domain="food",
source="explicit_user_input",
value_json={
"likes": ["pizza", "sushi"],
"dislikes": ["broccoli"]
}
)
print(f"Created memory: {memory.id}")The scope determines the category and policy rules for your memory. Choose the scope that best fits your use case:
preferencesUser likes, dislikes, and general preferencesExample: Food preferences, music taste, favorite colors
constraintsRules, boundaries, and limitationsExample: Dietary restrictions, budget limits, availability windows
communicationCommunication style and tone preferencesExample: Preferred tone, emoji usage, formality level
accessibilityAccessibility requirements and preferencesExample: High contrast mode, font size, screen reader preferences
scheduleTime-based availability and schedulingExample: Work hours, meeting preferences, timezone
attentionFocus and attention-related preferencesExample: Do not disturb settings, focus mode, notification preferences
See Scopes documentation for detailed information about each scope.
Using Domains
Domains are optional sub-categories that help organize memories within a scope. They allow you to group related memories together.
# Store food preferences
client.create_memory(
user_id="user123",
scope="preferences",
domain="food", # Domain for food-related preferences
source="explicit_user_input",
value_json={"likes": ["pizza", "sushi"]}
)
# Store music preferences
client.create_memory(
user_id="user123",
scope="preferences",
domain="music", # Domain for music-related preferences
source="explicit_user_input",
value_json={"likes": ["jazz", "classical"]}
)
# Both are in the "preferences" scope but different domainsSetting TTL (Time-to-Live)
You can set an expiration time for memories using the ttl_days parameter. Memories will automatically expire after the specified number of days.
# Store a temporary preference that expires in 7 days
client.create_memory(
user_id="user123",
scope="preferences",
domain="food",
source="explicit_user_input",
ttl_days=7, # Expires in 7 days
value_json={"likes": ["seasonal_special"]}
)
# Store a long-term preference (no expiration)
client.create_memory(
user_id="user123",
scope="preferences",
domain="food",
source="explicit_user_input",
# ttl_days not specified - no expiration
value_json={"likes": ["pizza"]}
)The source field indicates where the memory came from. Common values include:
explicit_user_input- User directly provided this informationinferred- Information was inferred from user behaviorsystem- System-generated or default valueimport- Imported from another system
The source field is useful for debugging, compliance, and understanding data provenance.
- Use Consistent user_id: Always use the same user_id format across all operations for the same user.
- Choose Appropriate Scope: Select the scope that best fits your use case. This affects policy enforcement and merging behavior.
- Use Domains for Organization: Domains help organize memories within a scope. Use them to group related memories together.
- Set TTL for Temporary Data: Use ttl_days for temporary memories that should expire automatically (e.g., seasonal preferences, temporary constraints).
- Include Source Information: Always include accurate source information for compliance and debugging.
- Don't Worry About Duplicates: The API handles normalization and deduplication automatically. You can create multiple memories and they'll be merged when read.
- Use Structured Data: Store data in a structured format (JSON) that makes sense for your use case. The API will handle the merging logic.