Skip to main content
Essentially, creating a companion out of LLMs is as simple as a loop. But these loops work great for one type of character without personalization and fall short as soon as you restart the chat. Problem: LLMs are stateless. GPT doesn’t remember conversations. You could stuff everything inside the context window, but that becomes slow, expensive, and breaks at scale. The solution: Mem0. It extracts and stores what matters from conversations, then retrieves it when needed. Your companion remembers user preferences, past events, and history.
In this cookbook we’ll build a fitness companion that:
  • Remembers user goals across sessions
  • Recalls past workouts and progress
  • Adapts its personality based on user preferences
  • Handles both short-term context (today’s chat) and long-term memory (months of history)
By the end, you’ll have a working fitness companion and know how to handle common production challenges.

The Basic Loop with Memory

Max wants to train for a marathon. He starts chatting with Ray, an AI running coach.
Session 1:
Session 2 (next day, app restarted):
Ray remembers Max’s goal across sessions. The app restarted, but the memory persisted. This is the core pattern: retrieve memories, pass them as context, store new exchanges.
Ray remembers. Restart the app, and the goal persists. From here on, we’ll focus on just the Mem0 API calls.

Organizing Memory by Type

Separating Temporary from Permanent

Max mentions his knee hurts. That’s different from his marathon goal - one is temporary, the other is long-term.
Categories vs Metadata:
  • Categories: AI-assigned by Mem0 based on content (you can’t force them)
  • Metadata: Manually set by you for forced tagging
Define custom categories at the project level. Mem0 will automatically tag memories with relevant categories based on content:
Categories vs Metadata: Categories are AI-assigned by Mem0 based on content semantics. You define the palette, Mem0 picks which ones apply. If you need guaranteed tagging, use metadata instead.
Now when you add memories, Mem0 automatically assigns the appropriate categories:
Mem0 reads the content and intelligently picks which categories apply. You define the palette, it handles the tagging.Important: You cannot force specific categories. Mem0’s platform decides which categories are relevant based on content. If you need to force-tag something, use metadata instead:

Filtering by Category

Retrieve just constraints for workout planning:
Ray can plan workouts that avoid aggravating Max’s knee, without pulling in race goals or other unrelated memories.

Filtering What Gets Stored

The Problem

Run the basic loop for a week and check what’s stored:
Without filters, Mem0 stores everything: greetings, filler, and casual chat. This pollutes retrieval: instead of pulling “marathon goal,” you get “lol ok.” Set custom instructions to keep memory clean.
Noise. Greetings and filler clutter the memory.

Custom Instructions

Tell Mem0 what matters:
Now chat again:
Expected output: Only 2 memories stored: the marathon goal and trail preference. The greeting “hey how’s it going” was filtered out automatically. Custom instructions are working.
Only meaningful facts. Filler gets dropped automatically.

Agent Memory for Personality

Why Agents Need Memory Too

Max prefers direct feedback, not motivational fluff. Ray needs to remember how to communicate - that’s agent memory, separate from user memory. Store agent personality:
Retrieve agent style alongside user memories:
Expected behavior: Ray’s responses are now data-driven and direct. The agent memory stored the coaching style preference, so future responses adapt automatically without Max having to repeat his preference.
No “Great job!” or “Keep it up!” - just data. Ray adapts to Max’s preference.

Managing Short-Term Context

When to Store in Mem0

Don’t send every single message to Mem0. Keep recent context in memory, let Mem0 handle the important long-term facts.
Last 10 messages in your app’s buffer. Important facts in Mem0. Faster, cheaper, still works.

Time-Bound Memories

Auto-Expiring Facts

Max tweaks his ankle. It’ll heal in two weeks - the memory should expire too.
Store expires_on in metadata and periodically clean up expired memories. Ray stops asking about the ankle once it’s removed.

Putting It All Together

Here’s the Mem0 setup combining everything:
Week 1 - Store goals and preferences:
Week 3 - Temporary injury with expiration:
Retrieve for context:
Ray remembers goals, preferences, and personality. Handles temporary injuries. Works across sessions.

Common Production Patterns

Episodic Stories with run_id

Training for Boston is different from training for New York. Separate the memory threads:
Each race gets its own episodic boundary. No cross-contamination.

Importing Historical Data

Max has 6 months of training logs to backfill:

Handling Contradictions

Max changes his goal from sub-4 to sub-3:45:
Update instead of creating duplicates.

Multiple Agents

Max works with Ray for running and Jordan for strength training:
Each coach maintains separate personality memory while sharing user context.

Filtering by Date

Prioritize recent training over old data:

Metadata Tagging

Tag workouts by type:

Pruning Old Memories

Delete irrelevant memories:

What You Built

A companion that:
  • Persists across sessions - Mem0 storage
  • Filters noise - custom instructions
  • Organizes by type - categories
  • Adapts personality - agent_id
  • Stays fast - short-term buffer
  • Handles temporal facts - expiration
  • Scales to production - batching, metadata, pruning
This pattern works for any companion: fitness coaches, tutors, roleplay characters, therapy bots, creative writing partners.
Start with 2-3 categories max (e.g., goals, constraints, preferences). More categories dilute tagging accuracy. You can always add more later after seeing what Mem0 extracts.

Production Checklist

Before launching:
  • Set custom instructions for your domain
  • Define 2-3 categories (goals, constraints, preferences)
  • Add expiration strategy for time-bound facts
  • Implement error handling for API calls
  • Monitor memory quality (Mem0 dashboard or get_all / Qdrant when local)
  • Clear test data from production project

Partition Memories by Entity

Keep companions from leaking context by combining user, agent, and session scopes.

Tag Support Memories

Organize customer context to keep assistants responsive at scale.
Using Mem0? Star us on GitHub to help more developers discover memory for AI apps.