MiddlewareBase subclass that non-invasively handles memory injection, retrieval, and write-back.
AgentScope currently supports the following long-term memory implementations, with more under development:
Agentic Memory
Agentic Memory is AgentScope’s native long-term memory implementation. It provides long-term memory through Markdown file reads, writes, and retrieval. At runtime, the agent autonomously creates Markdown memory files, maintains an index of all memory files in a fixedMEMORY.md file, and automatically injects that index into the system prompt. This follows a “progressive disclosure” pattern.
At runtime, the agent uses the built-in Read, Write, and Edit tools to create, access, and modify long-term memory. A typical file structure looks like this:
name, description, and type fields for later retrieval and injection:
MEMORY.md stays short. It serves only as an index and is automatically injected into the system prompt. For example:
MEMORY.md index. Second, when reply / reply_stream is called, the middleware starts an async task that asks an LLM to select relevant Markdown files, then checks before later reasoning steps whether that task has finished and injects the retrieved results as a HintBlock.
Note that retrieval is asynchronous: injection happens at checkpoints before reasoning starts inside the reasoning-acting loop, and the exact timing depends on retrieval latency. If the current reply does not enter a later reasoning round, such as when the model produces no tool calls, the retrieved long-term memory may not be injected into that reply. The workflow is:
Use Agentic Memory in different environments as follows:
Mem0
Mem0Middleware is a drop-in long-term memory backend powered by mem0. It works with both mem0.AsyncMemory (open-source) and mem0.AsyncMemoryClient (hosted Platform). With mem0.AsyncMemory (open-source), it can route mem0’s own memory extraction and embedding through your existing AgentScope models — so mem0 needs no separate provider key.
Installation
Mem0Middleware’s dependencies are available as an optional extra in AgentScope:
Quick start
The fastest path is to pass a dedicated AgentScope chat model and an embedding model to the middleware; it builds an open-source mem0 store internally and wires both extraction and embedding through them.Mem0Middleware contributes its search_memory / add_memory tools through list_tools(), which the agent does not call automatically. To make the tools available to the agent, collect them yourself and pass them into the toolkit — Toolkit(tools=await mw.list_tools()). In static_control mode list_tools() returns an empty list.Control modes
Themode parameter decides how the agent interacts with mem0. It defaults to "both", matching AgentScope 1.x’s ReActAgent.long_term_memory_mode.
Construction paths
Mem0Middleware supports three ways to wire up the mem0 backend:
- AgentScope models
- Models + custom config
- Pre-built client
Pass AgentScope models and let the middleware build an open-source
AsyncMemory internally (mem0’s default Qdrant store). The embedding model’s dimensions must match the vector store (the default Qdrant expects 1536).Key parameters
Agent-callable tools
Inagent_control and both modes, the middleware contributes two tools the model can invoke on demand:
search_memory(keywords, limit=5)— retrieves memories using a list of short, targeted keywords. Each keyword is issued as an independent query; results are merged and deduplicated.add_memory(thinking, content)— records durable facts. Onlycontent(a list of standalone sentences) is persisted to mem0;thinkingstays in the transcript for auditability.
user_id / agent_id directly from the middleware instance, so they require no extra wiring beyond adding them to the toolkit.