Skip to content
Membase
Membase x Dify: How to Add Long-Term Memory to Dify Agents and Workflows

Membase x Dify: How to Add Long-Term Memory to Dify Agents and Workflows

Research
MembaseMembase Team
View the original post on Medium

A customer tells your Dify support bot that their team wants reports every Monday, sent to the finance channel. A week later they open a new conversation, and the bot asks the same questions again. Dify keeps the current conversation in context, but nothing carries over to the next one.

The Membase plugin fixes that. It brings long-term memory to Dify agents and workflows, so what a user said last week is there today, with the source of every memory cited. The plugin is live on Dify Marketplace and Verified by Dify.

Quick answer

  1. Create a Memory and a developer key in the Membase app.
  2. Install Membase from Dify Marketplace.
  3. Authorize the plugin with your key.
  4. Add Search memory before your LLM, and Save memory for when the user asks to remember something.
  5. Test it in a new conversation: the answer should cite the Memory it came from.

Membase x Dify: a chatflow searches Membase before the LLM answers, saves only when the user asks, and other AI tools read the same memory over MCP

What memory does Dify have today?

Dify gives you three ways to keep context. Each is good at one job:

Built-in memoryConversation variablesKnowledge base
What it keepsRecent messages of the current conversationValues your flow sets during a conversationDocuments you upload
How long it lastsThe current conversationThe current conversationUntil you change the documents
Learns from usersNoOnly what your flow writesNo
Good forFollow-up questionsForm state, counters, flagsProduct docs, policies, FAQs

None of them remembers what a user told your app in an earlier conversation. That is the gap long-term memory fills: preferences, decisions and facts about a user that should still be known next week, in any conversation or app.

Why Membase for Dify agents?

  1. Memory that outlasts the conversation. What a user says today is there next week, in a new conversation or another Dify app.
  2. Saves only what the user asks to keep. The save tool tells the model to save only when the user asks to remember something, so memory does not fill with guesses.
  3. Every recalled passage cites its source. Each passage names the Memory it came from, so the answer can tell the user where it learned a fact.
  4. Control over what each app can do. A Read key can only search. A Read & write key can also save. You choose which Memories each key reaches, and you can revoke a key at any time.
  5. One memory across Dify and your other AI tools. ChatGPT, Claude, Claude Code, Cursor and Codex read the same memory over MCP.

How the Membase plugin works

The plugin adds three tools to Dify. They work like any other tool node, with no code:

ToolWhat it does
Search memoryFinds the passages that answer a question, across every Memory the key reaches, most relevant first. Each passage names the Memory it came from.
Save memorySaves one fact, in the user's words, to the key's Memory. Only when the user asks.
Get user profileReads the profile facts: standing statements about who the user is, newest first.

Each tool returns a readable text output and a json output. In the text output, every passage looks like [Memory name] content, so an LLM node can quote the source directly.

There is no delete tool. Deleting memory is done by the account owner in the Membase app.

How to add long-term memory to a Dify agent

Prerequisites

  • A Dify workspace that can install plugins, on Dify Cloud or self-hosted.
  • A free Membase account at app.membase.com, with a working model under AI Setup.
  • For self-hosted Dify: outbound HTTPS to api.app.membase.com on port 443.

Step 1: Create a Memory and a key

In the Membase app, create a Memory, for example “Customer notes”. Then open Connect › Developer keys and create a key:

  • Choose Read & write if the app should save memories, or Read if it should only search.
  • Choose which Memories the key may reach. A key that should save must reach exactly one Memory, which becomes its write target.
  • Tick the profile if the app should read it.

Step 2: Install the plugin

In Dify, open Plugins › Marketplace, search for Membase and install it. The plugin carries the Verified by Dify badge.

Step 3: Authorize it

Open Plugins › Membase › Authorize (or Tools › Membase) and enter:

FieldRequiredValue
Membase API keyYesThe key from Step 1, starting with mbk_
Base URLNoLeave empty for the Membase cloud

Dify checks the key against the Membase API when you save. A mistyped, expired or revoked key is rejected at this step.

Step 4: Add memory to an agent

In an Agent app, add Search memory, Get user profile and, if the key can write, Save memory to the agent’s tools.

The tool descriptions already tell the model when to use each one: read the profile once near the start, search before answering anything that depends on what the user said before, and save only when the user asks to remember something. Add one line to the instructions:

Cite the Memory name when you use a memory.

Step 5: Or build a memory loop in a chatflow

For more control, build the loop yourself:

  1. A Search memory tool node, with the user’s message as query. You can set limit from 1 to 50 passages.
  2. An LLM node that receives the search node’s text output as context.
  3. Optionally, a Save memory node on a branch that runs only when the user asks to remember something, with the fact as content.

Keep Dify’s built-in memory switched on in the LLM node for the current conversation. Membase adds what came before it.

Verify that memory is working

  1. Start a conversation and say:

    Remember that Acme wants weekly reports on Mondays, sent to the finance channel.
    
  2. Wait a short while. Membase accepts the note at once and learns it in the background.

  3. Open a new conversation and ask:

    When and where should I send Acme's report?
    
  4. The answer should say Monday, in the finance channel, and cite [Customer notes].

  5. Open Claude or Cursor connected to the same Memory and ask the same question. The answer is the same, because the fact lives in Membase, not in Dify.

What you can build

  • Customer support assistant. Save each customer’s preferences and past resolutions when the agent is asked to. Search them before drafting a reply, so customers never repeat themselves.
  • Account notes for sales and success teams. Save decisions from each call. Before the next call, the agent recalls what was agreed and who asked for what.
  • Personal research assistant. Save the sources, conclusions and open questions you ask it to keep. Pick up next week where you left off, in Dify or in Claude.

Multi-user apps: One key per user with Dify’s MCP tool

Dify stores plugin credentials per workspace. So every end user of an app that uses the plugin reads, and can write, the same Membase account: the one that owns the key. That suits a personal assistant or an internal tool. It does not suit an app with many end users, who would all see each other’s memory.

For a multi-user app, give each end user their own Membase key and use Dify’s built-in MCP tool instead of the plugin:

  1. In Dify, open Tools › MCP and add an MCP server (HTTP).

  2. Server URL: https://api.app.membase.com/mcp-http

  3. Under the advanced options, add this header:

    Authorization: Bearer {{request.headers.X-Membase-Key}}
    
  4. Your backend calls the Dify app API with each user’s own key in an X-Membase-Key header. Dify fills in the placeholder at call time, so each run reaches only that user's memory.

The placeholder is filled only for runs started by an HTTP request to the app. The console’s debug preview has no such header.

Common mistakes

The key reaches several Memories, so nothing is saved

A key can save only when it reaches exactly one Memory. If it reaches several, Search memory works but Save memory reports that there is no write target. Give writer apps a key with one Memory in reach.

Searching right after saving

Membase learns new notes in the background. A search a second after a save may not find the note yet. Wait a short while, then search.

Sharing one key across all users

The plugin uses one key per workspace, so all end users share one memory. For an app with many users, use the MCP setup above.

No working model in the Membase account

Membase needs a model under AI Setup in the Membase app. Without one, searches report the Memory as unavailable, which is different from “no memory found”.

The first search is slow

The first search after a quiet spell can take up to a minute while the account’s memory wakes. Later calls are faster.

Key takeaways

  1. Dify’s built-in memory keeps the current conversation. Membase keeps what should be known next week.
  2. Search before the LLM, and save only when the user asks.
  3. Every recalled passage names the Memory it came from, so answers can cite their source.
  4. Use a Read key for apps that should only search, and a Read & write key with one Memory for apps that save.
  5. For apps with many end users, give each user their own key through Dify’s MCP tool.

Next steps

  1. Create a free key in the Membase app under Connect › Developer keys.
  2. Install Membase from Dify Marketplace (Plugins › Marketplace).
  3. Follow the steps above, then run the test in “Verify that memory is working”.

The plugin source, its README and privacy policy are on GitHub.

Resources

Get started

For developers

Videos

Help

Frequently Asked Questions

Does Dify have long-term memory?
Dify's built-in memory keeps the recent messages of the current conversation, and conversation variables last for the conversation too. For memory that lasts across conversations and apps, add a memory plugin such as Membase.
What is the difference between Dify's built-in memory and Membase?
Built-in memory keeps recent messages within one conversation. Membase keeps long-term memory that lasts until you delete it, and is shared across conversations, Dify apps and other AI tools.
Do I need to write code?
No. The plugin adds tools you drop into an agent or a chatflow. Code is only needed on your backend for the multi-user MCP setup.
Can each user of my Dify app have their own memory?
Yes. Give each user their own Membase key and connect Membase through Dify's MCP tool, passing the key in a request header. The plugin alone shares one memory per workspace.
Can the agent forget something?
There is no delete tool in Dify. The account owner deletes memories in the Membase app.
Is the Membase plugin free?
Membase is free during the beta. Paid plans are coming soon.
Back to all posts