{"id":886,"date":"2026-05-11T15:34:38","date_gmt":"2026-05-11T07:34:38","guid":{"rendered":"https:\/\/connectword.dpdns.org\/?p=886"},"modified":"2026-05-11T15:34:38","modified_gmt":"2026-05-11T07:34:38","slug":"a-coding-implementation-to-build-agent-native-memory-infrastructure-with-memori-for-persistent-multi-user-and-multi-session-llm-applications","status":"publish","type":"post","link":"https:\/\/connectword.dpdns.org\/?p=886","title":{"rendered":"A Coding Implementation to Build Agent-Native Memory Infrastructure with Memori for Persistent Multi-User and Multi-Session LLM Applications"},"content":{"rendered":"<p>In this tutorial, we implement how<a href=\"https:\/\/github.com\/MemoriLabs\/Memori\"> <strong>Memori<\/strong><\/a> serves as an agent-native memory infrastructure layer for building more persistent, context-aware LLM applications. We start by setting up Memori in a Google Colab environment and connecting it to both synchronous and asynchronous OpenAI clients, so that every model call can automatically pass through the memory layer. We then move on to practical examples that show how user data is stored, retrieved, and separated across different identities, agent roles, and sessions. We also test streaming responses, async calls, and a small customer-support agent workflow to understand how memory behaves in realistic multi-turn applications. By the end of the tutorial, we gain a clear understanding of how Memori helps us build AI agents that do not treat each conversation in isolation but instead retain useful context across interactions.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">import subprocess, sys\ndef _pip(*pkgs):\n   subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", \"-q\", *pkgs])\n_pip(\"memori&gt;=3.3.0\", \"openai&gt;=1.40.0\", \"nest_asyncio\")\nimport os, getpass, time, uuid, asyncio\nimport nest_asyncio; nest_asyncio.apply()\nif not os.getenv(\"OPENAI_API_KEY\"):\n   os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OPENAI_API_KEY: \")\nif not os.getenv(\"MEMORI_API_KEY\"):\n   v = getpass.getpass(\"MEMORI_API_KEY (leave blank for rate-limited tier): \")\n   if v.strip():\n       os.environ[\"MEMORI_API_KEY\"] = v.strip()\n   else:\n       print(\"\u2192 No MEMORI_API_KEY set. Continuing with rate-limited tier.\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We install Memori, OpenAI, and Nest AsyncIO so the tutorial runs smoothly inside Google Colab. We load the required Python modules and prepare the notebook to handle async execution without runtime issues. We also collect the OpenAI API key and optional Memori API key, allowing the workflow to run either with authenticated Memori access or the rate-limited tier.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">from memori import Memori\nfrom openai import OpenAI, AsyncOpenAI\nclient       = OpenAI()\nasync_client = AsyncOpenAI()\nmem = Memori()\nmem.llm.register(client)\nmem.llm.register(async_client)\nMODEL        = \"gpt-4o-mini\"\nWRITE_DELAY  = 6\ndef ask(prompt, system=None):\n   msgs = []\n   if system: msgs.append({\"role\": \"system\", \"content\": system})\n   msgs.append({\"role\": \"user\", \"content\": prompt})\n   r = client.chat.completions.create(model=MODEL, messages=msgs)\n   return r.choices[0].message.content\ndef banner(t): print(\"n\" + \"=\"*78 + f\"n {t}n\" + \"=\"*78)<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We import Memori and create both synchronous and asynchronous OpenAI clients for different LLM interaction patterns. We register both clients with Memori so that memory can automatically intercept and enrich chat completion calls. We also define a reusable ask() helper and a banner() function to keep the tutorial output clean and organized.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">banner(\"Part 1 \u2014 Basic memory: facts persist across turns\")\nmem.attribution(entity_id=\"alice@example.com\", process_id=\"personal-assistant\")\nask(\"My name is Alice. I love hiking, Italian food, and I'm allergic to peanuts.\")\ntime.sleep(WRITE_DELAY)\nprint(\"[Alice]\", ask(\"What do you know about me? Be specific.\"))\nbanner(\"Part 2 \u2014 Multi-tenant memory: Bob's facts don't leak into Alice's recall\")\nmem.attribution(entity_id=\"bob@example.com\", process_id=\"personal-assistant\")\nask(\"I'm Bob. Vegetarian, write Rust for a living, live in Berlin.\")\ntime.sleep(WRITE_DELAY)\nmem.attribution(entity_id=\"alice@example.com\", process_id=\"personal-assistant\")\nprint(\"[Alice]\", ask(\"What's my favorite cuisine and any dietary issues?\"))\nmem.attribution(entity_id=\"bob@example.com\", process_id=\"personal-assistant\")\nprint(\"[Bob]  \", ask(\"Which programming language do I write professionally?\"))<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We begin by testing basic memory persistence: Alice shares personal facts, and the model later recalls them. We then switch to Bob and store a separate set of details to demonstrate multi-tenant memory isolation. We return to Alice and Bob separately to confirm that each user\u2019s facts remain scoped to the correct entity.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">banner(\"Part 3 \u2014 Same user, different agent personas via process_id\")\nmem.attribution(entity_id=\"alice@example.com\", process_id=\"fitness-coach\")\nask(\"Goal: sub-25-minute 5K by June. Currently I run 30 minutes flat.\")\ntime.sleep(WRITE_DELAY)\nmem.attribution(entity_id=\"alice@example.com\", process_id=\"meal-planner\")\nask(\"Prefer low-carb dinners on weekdays.\")\ntime.sleep(WRITE_DELAY)\nmem.attribution(entity_id=\"alice@example.com\", process_id=\"fitness-coach\")\nprint(\"[fitness-coach]\", ask(\"Remind me of my running goal.\"))\nmem.attribution(entity_id=\"alice@example.com\", process_id=\"meal-planner\")\nprint(\"[meal-planner] \", ask(\"Suggest tonight's dinner.\"))\nbanner(\"Part 4 \u2014 Sessions group related turns\")\nmem.attribution(entity_id=\"alice@example.com\", process_id=\"personal-assistant\")\nproject_session = f\"project-fastapi-{uuid.uuid4().hex[:8]}\"\nmem.set_session(project_session)\nask(\"Notes: building a FastAPI app called 'Lighthouse', Python 3.12, \"\n   \"deploying to Fly.io.\")\ntime.sleep(WRITE_DELAY)\nask(\"Decision: SQLAlchemy + Alembic for the data layer.\")\ntime.sleep(WRITE_DELAY)\nmem.new_session()\nask(\"Random aside: I just adopted a puppy named Mochi.\")\ntime.sleep(WRITE_DELAY)\nmem.set_session(project_session)\nprint(\"[project session]\",\n     ask(\"Summarize what we've decided about Lighthouse so far.\"))<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We show how the same user can have different memories across different agent personas using separate process_id values. We store Alice\u2019s fitness goal under a fitness coach and her dinner preference under a meal planner, then verify that each agent recalls only its relevant context. We also create a project-specific session for a FastAPI app and show how session management keeps related project decisions separate from unrelated personal details.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">banner(\"Part 5 \u2014 Streaming\")\nmem.attribution(entity_id=\"alice@example.com\", process_id=\"personal-assistant\")\nstream = client.chat.completions.create(\n   model=MODEL,\n   messages=[{\"role\": \"user\",\n              \"content\": \"In two sentences, what do you remember about me?\"}],\n   stream=True,\n)\nprint(\"[stream] \", end=\"\")\nfor chunk in stream:\n   d = chunk.choices[0].delta.content\n   if d: print(d, end=\"\", flush=True)\nprint(); time.sleep(WRITE_DELAY)\nbanner(\"Part 6 \u2014 Async LLM calls\")\nasync def async_demo():\n   r = await async_client.chat.completions.create(\n       model=MODEL,\n       messages=[{\"role\": \"user\",\n                  \"content\": \"What dietary restriction do I have? (asked async)\"}],\n   )\n   return r.choices[0].message.content\nprint(\"[async]\", asyncio.run(async_demo()))\nbanner(\"Part 7 \u2014 Mini support agent across multiple sessions\")\ndef support(user_id, prompt):\n   mem.attribution(entity_id=user_id, process_id=\"support-bot\")\n   return ask(prompt, system=(\n       \"You are a calm, helpful customer support agent. \"\n       \"Use what you remember about the user. If you don't know, say so.\"\n   ))\nUSER = \"charlie@example.com\"\nmem.attribution(entity_id=USER, process_id=\"support-bot\")\nmem.new_session()\nprint(\"[support T1]\", support(USER,\n   \"Hi! I'm Charlie, on the Pro plan. Email: charlie@example.com. \"\n   \"Billing question for next month.\"))\ntime.sleep(WRITE_DELAY)\nmem.new_session()\nprint(\"[support T2]\", support(USER,\n   \"Hey, me again. What plan am I on and what's my email of record?\"))\nbanner(\"Done. Open https:\/\/app.memorilabs.ai to inspect memories, \"\n      \"or use Memori BYODB to point at your own Postgres.\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p>We test Memori with streaming responses to confirm that memory continues working when tokens arrive incrementally. We then run an asynchronous OpenAI call and verify that the async client can also access stored user context. Also, we built a mini support-agent flow that remembers Charlie\u2019s plan and email across separate sessions, demonstrating how Memori supports realistic, long-term customer interactions.<\/p>\n<p>In conclusion, we built and tested a complete Memori-powered memory workflow for LLM agents. We saw how Memori stores basic user preferences, keeps Alice\u2019s and Bob\u2019s memories isolated, and allows the same user to maintain different memories across separate agent personas, such as a fitness coach and a meal planner. We also explored how sessions help us group project-specific conversations, while unrelated details stay outside the active session context. Beyond basic recall, we verified that Memori continues to work with streaming outputs, asynchronous OpenAI calls, and a mini support-agent scenario where a user\u2019s plan and email are remembered across new conversations. Also, we created a practical foundation for building personalized AI assistants, support bots, workflow agents, and multi-agent systems that remember important context while keeping memory organized, scoped, and reusable.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p>Check out\u00a0the\u00a0<strong><a href=\"https:\/\/github.com\/Marktechpost\/AI-Agents-Projects-Tutorials\/blob\/main\/Agentic%20AI%20Memory\/memori_agent_native_memory_infrastructure_tutorial_Marktechpost.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">Full Codes with Notebook here<\/a>.\u00a0<\/strong>Also,\u00a0feel free to follow us on\u00a0<strong><a href=\"https:\/\/x.com\/intent\/follow?screen_name=marktechpost\" target=\"_blank\" rel=\"noreferrer noopener\"><mark>Twitter<\/mark><\/a><\/strong>\u00a0and don\u2019t forget to join our\u00a0<strong><a href=\"https:\/\/www.reddit.com\/r\/machinelearningnews\/\" target=\"_blank\" rel=\"noreferrer noopener\">150k+ ML SubReddit<\/a><\/strong>\u00a0and Subscribe to\u00a0<strong><a href=\"https:\/\/www.aidevsignals.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">our Newsletter<\/a><\/strong>. Wait! are you on telegram?\u00a0<strong><a href=\"https:\/\/t.me\/machinelearningresearchnews\" target=\"_blank\" rel=\"noreferrer noopener\">now you can join us on telegram as well.<\/a><\/strong><\/p>\n<p>Need to partner with us for promoting your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar etc.?\u00a0<strong><a href=\"https:\/\/forms.gle\/MTNLpmJtsFA3VRVd9\" target=\"_blank\" rel=\"noreferrer noopener\"><mark>Connect with us<\/mark><\/a><\/strong><\/p>\n<p>The post <a href=\"https:\/\/www.marktechpost.com\/2026\/05\/11\/a-coding-implementation-to-build-agent-native-memory-infrastructure-with-memori-for-persistent-multi-user-and-multi-session-llm-applications\/\">A Coding Implementation to Build Agent-Native Memory Infrastructure with Memori for Persistent Multi-User and Multi-Session LLM Applications<\/a> appeared first on <a href=\"https:\/\/www.marktechpost.com\/\">MarkTechPost<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we implement&hellip;<\/p>\n","protected":false},"author":1,"featured_media":29,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=886"}],"version-history":[{"count":0,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/886\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/media\/29"}],"wp:attachment":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}