Skip to main content
Long-term memory is an agent’s ability to retain information across sessions, including user preferences, past decisions, and knowledge or rules summarized from conversations. AgentScope implements different long-term memory capabilities as agent middleware. Each long-term memory implementation is a 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 fixed MEMORY.md file, and automatically injects that index into the system prompt. This follows a “progressive disclosure” pattern.
Agentic Memory supports different runtime environments through the backend parameter, such as local, Docker, E2B, and cloud sandboxes. It uses LocalBackend by default.
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:
Each Markdown file follows the frontmatter convention and includes 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:
Agentic Memory has two retrieval paths. First, the agent can autonomously retrieve relevant files based on the prompt and the 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:

ReMe

ReMe is a file-based memory toolkit maintained by the AgentScope team. ReMeMiddleware embeds ReMe in the current process, so no separate service is required. It listens to the agent’s conversations and automatically extracts and writes back memories through ReMe’s auto_memory job after each reply. The agent does not save memories itself, and ReMe does not provide a manual memory-add tool.
The ReMe workspace is specified by workspace_dir and stores memory cards and search indexes. Reusing the same workspace across sessions enables cross-session recall.

Installation

ReMeMiddleware’s dependencies are available as an optional AgentScope extra:

Quick start

You can inject AgentScope chat and embedding models into ReMe. The chat model drives auto_memory extraction; providing an embedding model automatically enables vector search with the default configuration. Replace my_chat_model and my_embedding_model below with your model instances:
Without injected models, ReMe creates its LLM and embedding backends from its own configuration and credentials. The default default configuration uses BM25 keyword search for its file store. For semantic search, pass an embedding_model. Its vector dimensions must match the vector store configured for ReMe; the example uses a 1024-dimensional DashScope embedding model.

Control modes

ReMeMiddleware.Parameters.mode defaults to "both" and controls retrieval only; conversation write-back runs automatically in all three modes. In static_control mode, await memory.list_tools() returns an empty list. In agent_control and both modes, pass the returned tools into Toolkit as shown above. memory_search is query-only; there is no add_memory tool because writes are always handled automatically by the middleware.

Session scope and lifecycle

  • Write-back is scoped by agent.state.session_id. Set a stable ID with AgentState(session_id="...") for a resumable session; the ID does not belong on the middleware configuration.
  • Search spans the entire workspace_dir, rather than only the current session_id. A new agent using the same workspace can therefore recall memories written by an earlier session.
  • One ReMeMiddleware can safely be shared across multiple agents and sessions. The middleware reads each agent’s session_id at hook time. Call await memory.close() explicitly when the application shuts down.
After auto_memory writes a card, ReMe still needs to index it before it becomes searchable. A search issued immediately after write-back may temporarily miss the new card. The examples/long_term_memory/reme example explicitly triggers indexing after write-back so its demonstration is deterministic.

Key parameters

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 your AgentScope chat and embedding models; the middleware 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

The mode 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:
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).
Mem0Middleware requires an async mem0 client (mem0.AsyncMemory or mem0.AsyncMemoryClient). The synchronous Memory / MemoryClient are not supported.

Key parameters

Agent-callable tools

In agent_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. Only content (a list of standalone sentences) is persisted to mem0; thinking stays in the transcript for auditability.
Both tools auto-allow themselves and read user_id / agent_id directly from the middleware instance, so they require no extra wiring beyond adding them to the toolkit.