Seamless Client Integration for Automatic Memory
Register synchronous and asynchronous OpenAI clients with Memori using mem.llm.register(client) and mem.llm.register(async_client). This intercepts all chat.completions.create calls to inject relevant memories as context, eliminating manual retrieval logic. Setup in Colab: pip install memori>=3.3.0 openai>=1.40.0 nest_asyncio, set OPENAI_API_KEY (required) and optional MEMORI_API_KEY for non-rate-limited access. Use gpt-4o-mini as the model and a 6-second WRITE_DELAY after each call to ensure memory persistence. Result: Every LLM interaction becomes stateful, recalling facts like "Alice loves hiking, Italian food, allergic to peanuts" across turns via simple mem.attribution(entity_id="[email protected]", process_id="personal-assistant") before prompting.
Multi-Tenant Isolation via Entity, Process, and Session Scoping
Scope memories hierarchically: entity_id (e.g., user email) isolates users—Bob's "vegetarian, Rust developer, Berlin" doesn't leak to Alice. process_id separates agent roles for one user: Alice's "sub-25-minute 5K goal" stays with fitness-coach, while "low-carb dinners" is siloed to meal-planner. Sessions group turns: mem.set_session("project-fastapi-abc123") captures "FastAPI app 'Lighthouse', Python 3.12, Fly.io, SQLAlchemy + Alembic", excluding unrelated "puppy named Mochi" from mem.new_session(). Recall by re-attributing and setting session: Agent summarizes project decisions accurately. Trade-off: Rate-limited tier suffices for demos; paid key needed for production scale.
Production-Ready Features: Streaming, Async, and Workflows
Streaming works out-of-box: stream=True on client.chat.completions.create pulls Alice's facts incrementally without breaking memory flow. Async calls via AsyncOpenAI recall restrictions like peanut allergy seamlessly: await async_client.chat.completions.create(...). Build multi-session agents like support bots: mem.attribution(entity_id="[email protected]", process_id="support-bot"); new mem.new_session() per turn still remembers "Pro plan, email protected" across interactions. System prompt: "You are a calm, helpful customer support agent. Use what you remember about the user." Inspect memories at https://app.memorilabs.ai or use BYODB for Postgres. Full notebook: https://github.com/Marktechpost/AI-Agents-Projects-Tutorials/blob/main/Agentic%20AI%20Memory/memori_agent_native_memory_infrastructure_tutorial_Marktechpost.ipynb. This scales to personalized assistants, multi-agent systems, and customer support retaining context long-term.