An AI that remembers user preferences and past conversations automatically.Without Memory:
// Day 1await llm.generate("I'm vegan and allergic to peanuts");// Day 2await llm.generate("Give me a recipe");// AI: "What dietary restrictions do you have?" β
With Memory:
// Day 1await generateWithMemory({ prompt: "I'm vegan", userId: "alice" });// Day 2await generateWithMemory({ prompt: "Give me a recipe", userId: "alice" });// AI: "Here's a vegan recipe: ..." β Remembered automatically
// First conversation: User shares preferenceconst response1 = await generateTextWithMemory({ model: "openai:gpt-4", prompt: "I'm vegan and allergic to peanuts", userId: "alice", sessionId: "profile_setup"});console.log(response1.text);// Output: "Got it! I'll remember you're vegan and have a peanut allergy."// Later: Different session, AI remembersconst response2 = await generateTextWithMemory({ model: "openai:gpt-4", prompt: "Give me a dinner recipe", userId: "alice", sessionId: "cooking_monday"});console.log(response2.text);// Output: "Here's a vegan stir-fry without peanuts: ..."// β Remembered from different conversation!
Memory works across:
Different sessions (profile_setup β cooking_monday)
import AlchemystAI from '@alchemystai/sdk';const client = new AlchemystAI({ apiKey: process.env.ALCHEMYST_AI_API_KEY,});// Update specific memoryawait client.v1.context.memory.update({ userId: "alice", sessionId: "profile_setup", messageId: "msg_001", content: "Updated: I'm vegan and gluten-free"});// Delete a specific conversationawait client.v1.context.memory.delete({ userId: "alice", sessionId: "profile_setup"});// Delete ALL memories for a user (use with caution!)await client.v1.context.memory.delete({ userId: "alice"});console.log("β Memory updated/deleted");
# Update specific memoryalchemyst.v1.context.memory.update( user_id="alice", session_id="profile_setup", message_id="msg_001", content="Updated: I'm vegan and gluten-free")# Delete a specific conversationalchemyst.v1.context.memory.delete( user_id="alice", session_id="profile_setup")# Delete ALL memories for a user (use with caution!)alchemyst.v1.context.memory.delete( user_id="alice")print("β Memory updated/deleted")
Handle group chats where multiple users participate in the same thread:
TypeScript
Python
// User 1 starts discussionawait generateTextWithMemory({ model: "openai:gpt-4", prompt: "What are React hooks best practices?", userId: "alice", sessionId: "team_discussion_001"});// User 2 joins same discussionawait generateTextWithMemory({ model: "openai:gpt-4", prompt: "Can you elaborate on useEffect?", userId: "bob", sessionId: "team_discussion_001" // β Same session = shared context});// User 1 continues - AI has full thread contextawait generateTextWithMemory({ model: "openai:gpt-4", prompt: "What about custom hooks?", userId: "alice", sessionId: "team_discussion_001"});// AI has full thread context regardless of who asks
# User 1 starts discussionchat_with_memory( prompt="What are React hooks best practices?", user_id="alice", session_id="team_discussion_001")# User 2 joins same discussionchat_with_memory( prompt="Can you elaborate on useEffect?", user_id="bob", session_id="team_discussion_001" # β Same session = shared context)# User 1 continues - AI has full thread contextchat_with_memory( prompt="What about custom hooks?", user_id="alice", session_id="team_discussion_001")# AI has full thread context regardless of who asks
Key insight: Using the same sessionId across different userId values creates a shared memory space for team conversations.
Symptoms: AI doesnβt remember past conversations.Causes:
Threshold too high
Wrong userId/sessionId
Memory wasnβt stored correctly
Fixes:1. Lower threshold:
similarityThreshold: 0.6 // Instead of 0.9
2. Verify exact same IDs:
// IDs must match EXACTLY (case-sensitive)userId: "user_123" // β Not "user_124" or "User_123"sessionId: "chat_456" // β Not "chat_457" or "Chat_456"
Found memories: 2Memory content: [ { content: "User: I'm vegan\nAssistant: Got it!" }, { content: "User: Give me a recipe\nAssistant: Here's a vegan..." }]
Too much irrelevant context
Symptoms: AI references unrelated past conversations or gets confused.Causes:
Threshold too low
Mixing unrelated conversations in same session
Fixes:1. Raise threshold:
similarityThreshold: 0.85 // More strict
2. Use separate sessions by topic:
// β Good - separate by topicsessionId: "physics_homework"sessionId: "cooking_recipes"sessionId: "movie_recommendations"// β Bad - everything mixedsessionId: "general_chat"
3. Limit memory retrieval:
// Only retrieve last 5 memories instead of 10limit: 5
Memory storage failures
Error Message:
{ "error": "Failed to store memory", "code": "STORAGE_ERROR"}
Common Causes:
Invalid API key
Rate limit exceeded
Content too large
Fixes:1. Verify API key:
console.log("API Key set:", !!process.env.ALCHEMYST_AI_API_KEY);// Should output: API Key set: true
2. Check rate limits:
Free tier: 100 operations/day
Pro tier: Unlimited
3. Reduce content size:
// Keep memory entries under 10KB eachconst content = longText.slice(0, 10000); // Truncate if needed
// β Good - descriptive and structuredsessionId: "support_ticket_2024_02_001"sessionId: "recipe_planning_vegan_week_5"sessionId: "project_alpha_sprint_3_planning"// β Bad - hard to debugsessionId: "session1"sessionId: "chat"sessionId: "abc123"