repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | --- |
| #2 | title: Contextual Memory Creation |
| #3 | description: "Add messages with automatic context management - no manual history tracking required" |
| #4 | --- |
| #5 | |
| #6 | ## What is Contextual Memory Creation? |
| #7 | |
| #8 | Contextual memory creation automatically manages message history, allowing you to focus on building AI experiences without manually tracking interactions. Simply send new messages, and Mem0 handles the context automatically. |
| #9 | |
| #10 | <CodeGroup> |
| #11 | ```python Python |
| #12 | # Just send new messages - Mem0 handles the context |
| #13 | messages = [ |
| #14 | {"role": "user", "content": "I love Italian food, especially pasta"}, |
| #15 | {"role": "assistant", "content": "Great! I'll remember your preference for Italian cuisine."} |
| #16 | ] |
| #17 | |
| #18 | client.add(messages, user_id="user123") |
| #19 | ``` |
| #20 | |
| #21 | ```javascript JavaScript |
| #22 | // Just send new messages - Mem0 handles the context |
| #23 | const messages = [ |
| #24 | {"role": "user", "content": "I love Italian food, especially pasta"}, |
| #25 | {"role": "assistant", "content": "Great! I'll remember your preference for Italian cuisine."} |
| #26 | ]; |
| #27 | |
| #28 | await client.add(messages, { user_id: "user123", version: "v2" }); |
| #29 | ``` |
| #30 | </CodeGroup> |
| #31 | |
| #32 | ## Why Use Contextual Memory Creation? |
| #33 | |
| #34 | - **Simple**: Send only new messages, no manual history tracking |
| #35 | - **Efficient**: Smaller payloads and faster processing |
| #36 | - **Automatic**: Context management handled by Mem0 |
| #37 | - **Reliable**: No risk of missing interaction history |
| #38 | - **Scalable**: Works seamlessly as your application grows |
| #39 | |
| #40 | ## How It Works |
| #41 | |
| #42 | ### Basic Usage |
| #43 | |
| #44 | <CodeGroup> |
| #45 | ```python Python |
| #46 | # First interaction |
| #47 | messages1 = [ |
| #48 | {"role": "user", "content": "Hi, I'm Sarah from New York"}, |
| #49 | {"role": "assistant", "content": "Hello Sarah! Nice to meet you."} |
| #50 | ] |
| #51 | client.add(messages1, user_id="sarah") |
| #52 | |
| #53 | # Later interaction - just send new messages |
| #54 | messages2 = [ |
| #55 | {"role": "user", "content": "I'm planning a trip to Italy next month"}, |
| #56 | {"role": "assistant", "content": "How exciting! Italy is beautiful this time of year."} |
| #57 | ] |
| #58 | client.add(messages2, user_id="sarah") |
| #59 | # Mem0 automatically knows Sarah is from New York and can use this context |
| #60 | ``` |
| #61 | |
| #62 | ```javascript JavaScript |
| #63 | // First interaction |
| #64 | const messages1 = [ |
| #65 | {"role": "user", "content": "Hi, I'm Sarah from New York"}, |
| #66 | {"role": "assistant", "content": "Hello Sarah! Nice to meet you."} |
| #67 | ]; |
| #68 | await client.add(messages1, { user_id: "sarah", version: "v2" }); |
| #69 | |
| #70 | // Later interaction - just send new messages |
| #71 | const messages2 = [ |
| #72 | {"role": "user", "content": "I'm planning a trip to Italy next month"}, |
| #73 | {"role": "assistant", "content": "How exciting! Italy is beautiful this time of year."} |
| #74 | ]; |
| #75 | await client.add(messages2, { user_id: "sarah", version: "v2" }); |
| #76 | // Mem0 automatically knows Sarah is from New York and can use this context |
| #77 | ``` |
| #78 | </CodeGroup> |
| #79 | |
| #80 | ## Organization Strategies |
| #81 | |
| #82 | Choose the right approach based on your application's needs: |
| #83 | |
| #84 | ### User-Level Memories (`user_id` only) |
| #85 | |
| #86 | **Best for:** Personal preferences, profile information, long-term user data |
| #87 | |
| #88 | <CodeGroup> |
| #89 | ```python Python |
| #90 | # Persistent user memories across all interactions |
| #91 | messages = [ |
| #92 | {"role": "user", "content": "I'm allergic to nuts and dairy"}, |
| #93 | {"role": "assistant", "content": "I've noted your allergies for future reference."} |
| #94 | ] |
| #95 | |
| #96 | client.add(messages, user_id="user123") |
| #97 | # This allergy info will be available in ALL future interactions |
| #98 | ``` |
| #99 | |
| #100 | ```javascript JavaScript |
| #101 | // Persistent user memories across all interactions |
| #102 | const messages = [ |
| #103 | {"role": "user", "content": "I'm allergic to nuts and dairy"}, |
| #104 | {"role": "assistant", "content": "I've noted your allergies for future reference."} |
| #105 | ]; |
| #106 | |
| #107 | await client.add(messages, { user_id: "user123", version: "v2" }); |
| #108 | // This allergy info will be available in ALL future interactions |
| #109 | ``` |
| #110 | </CodeGroup> |
| #111 | |
| #112 | ### Session-Specific Memories (`user_id` + `run_id`) |
| #113 | |
| #114 | **Best for:** Task-specific context, separate interaction threads, project-based sessions |
| #115 | |
| #116 | <CodeGroup> |
| #117 | ```python Python |
| #118 | # Trip planning session |
| #119 | messages1 = [ |
| #120 | {"role": "user", "content": "I want to plan a 5-day trip to Tokyo"}, |
| #121 | {"role": "assistant", "content": "Perfect! Let's plan your Tokyo adventure."} |
| #122 | ] |
| #123 | client.add(messages1, user_id="user123", run_id="tokyo-trip-2024") |
| #124 | |
| #125 | # Later in the same trip planning session |
| #126 | messages2 = [ |
| #127 | {"role": "user", "content": "I prefer staying near Shibuya"}, |
| #128 | {"role": "assistant", "content": "Great choice! Shibuya is very convenient."} |
| #129 | ] |
| #130 | client.add(messages2, user_id="user123", run_id="tokyo-trip-2024") |
| #131 | |
| #132 | # Different session for work project (separate context) |
| #133 | work_messages = [ |
| #134 | {"role": "user", "content": "Let's discuss the Q4 marketing strategy"}, |
| #135 | {"role": "assistant", "content": "Sure! What are your main goals for Q4?"} |
| #136 | ] |
| #137 | client.add(work_messages, user_id="user123", run_id="q4-marketing") |
| #138 | ``` |
| #139 | |
| #140 | ```javascript JavaScript |
| #141 | // Trip planning session |
| #142 | const messages1 = [ |
| #143 | {"role": "user", "content": "I want to plan a 5-day trip to Tokyo"}, |
| #144 | {"role": "assistant", "content": "Perfect! Let's plan your Tokyo adventure."} |
| #145 | ]; |
| #146 | await client.add(messages1, { user_id: "user123", run_id: "tokyo-trip-2024", version: "v2" }); |
| #147 | |
| #148 | // Later in the same trip planning session |
| #149 | const messages2 = [ |
| #150 | {"role": "user", "content": "I prefer staying near Shibuya"}, |
| #151 | {"role": "assistant", "content": "Great choice! Shibuya is very convenient."} |
| #152 | ]; |
| #153 | await client.add(messages2, { user_id: "user123", run_id: "tokyo-trip-2024", version: "v2" }); |
| #154 | |
| #155 | // Different session for work project (separate context) |
| #156 | const workMessages = [ |
| #157 | {"role": "user", "content": "Let's discuss the Q4 marketing strategy"}, |
| #158 | {"role": "assistant", "content": "Sure! What are your main goals for Q4?"} |
| #159 | ]; |
| #160 | await client.add(workMessages, { user_id: "user123", run_id: "q4-marketing", version: "v2" }); |
| #161 | ``` |
| #162 | </CodeGroup> |
| #163 | |
| #164 | ## Real-World Use Cases |
| #165 | |
| #166 | <Tabs> |
| #167 | <Tab title="Customer Support"> |
| #168 | ```python Python |
| #169 | # Support ticket context - keeps interaction focused |
| #170 | messages = [ |
| #171 | {"role": "user", "content": "My subscription isn't working"}, |
| #172 | {"role": "assistant", "content": "I can help with that. What specific issue are you experiencing?"}, |
| #173 | {"role": "user", "content": "I can't access premium features even though I paid"} |
| #174 | ] |
| #175 | |
| #176 | # Each support ticket gets its own run_id |
| #177 | client.add(messages, |
| #178 | user_id="customer123", |
| #179 | run_id="ticket-2024-001" |
| #180 | ) |
| #181 | ``` |
| #182 | </Tab> |
| #183 | <Tab title="Personal AI Assistant"> |
| #184 | ```python Python |
| #185 | # Personal preferences (persistent across all interactions) |
| #186 | preference_messages = [ |
| #187 | {"role": "user", "content": "I prefer morning workouts and vegetarian meals"}, |
| #188 | {"role": "assistant", "content": "Got it! I'll keep your fitness and dietary preferences in mind."} |
| #189 | ] |
| #190 | |
| #191 | client.add(preference_messages, user_id="user456") |
| #192 | |
| #193 | # Daily planning session (session-specific) |
| #194 | planning_messages = [ |
| #195 | {"role": "user", "content": "Help me plan tomorrow's schedule"}, |
| #196 | {"role": "assistant", "content": "Of course! I'll consider your morning workout preference."} |
| #197 | ] |
| #198 | |
| #199 | client.add(planning_messages, |
| #200 | user_id="user456", |
| #201 | run_id="daily-plan-2024-01-15" |
| #202 | ) |
| #203 | ``` |
| #204 | </Tab> |
| #205 | <Tab title="Educational Platform"> |
| #206 | ```python Python |
| #207 | # Student profile (persistent) |
| #208 | profile_messages = [ |
| #209 | {"role": "user", "content": "I'm studying computer science and struggle with math"}, |
| #210 | {"role": "assistant", "content": "I'll tailor explanations to help with math concepts."} |
| #211 | ] |
| #212 | |
| #213 | client.add(profile_messages, user_id="student789") |
| #214 | |
| #215 | # Specific lesson session |
| #216 | lesson_messages = [ |
| #217 | {"role": "user", "content": "Can you explain algorithms?"}, |
| #218 | {"role": "assistant", "content": "Sure! I'll explain algorithms with math-friendly examples."} |
| #219 | ] |
| #220 | |
| #221 | client.add(lesson_messages, |
| #222 | user_id="student789", |
| #223 | run_id="algorithms-lesson-1" |
| #224 | ) |
| #225 | ``` |
| #226 | </Tab> |
| #227 | </Tabs> |
| #228 | |
| #229 | ## Best Practices |
| #230 | |
| #231 | ### ✅ Do |
| #232 | - **Organize by context scope**: Use `user_id` only for persistent data, add `run_id` for session-specific context |
| #233 | - **Keep messages focused** on the current interaction |
| #234 | - **Test with real interaction flows** to ensure context works as expected |
| #235 | |
| #236 | ### ❌ Don't |
| #237 | - Send duplicate messages or interaction history |
| #238 | - Skip identifiers like `user_id` or `run_id` that scope the memory |
| #239 | - Mix contextual and non-contextual approaches in the same application |
| #240 | |
| #241 | ## Troubleshooting |
| #242 | |
| #243 | | Issue | Solution | |
| #244 | |-------|----------| |
| #245 | | **Context not working** | Ensure each call uses the same `user_id` / `run_id` combo; version is automatic | |
| #246 | | **Wrong context retrieved** | Check if you need separate `run_id` values for different interaction topics | |
| #247 | | **Missing interaction history** | Verify all messages in the interaction thread use the same `user_id` and `run_id` | |
| #248 | | **Too much irrelevant context** | Use more specific `run_id` values to separate different interaction types | |
| #249 | |
| #250 | |
| #251 | <Snippet file="get-help.mdx" /> |
| #252 |