Skip to main content
This document covers practical usage of memory modules in AgentScope, including short-term memory backends and long-term memory integrations. The memory module in AgentScope is responsible for:
  • storing messages, and
  • managing messages with marks across different storage implementations.
A mark is a string label associated with each message. It is commonly used to categorize, filter, and retrieve messages based on context or purpose. This mechanism supports high-level memory management in agents. For example, in ReActAgent, hint messages are typically stored with mark hint, and memory-aware workflows (such as compression pipelines) can be organized around marks.
The memory module focuses on storage and management. Algorithmic logic (for example, compression strategy) is implemented at the agent layer.
For conceptual background, see Context and Memory.
In AgentScope, memory can be viewed in two layers:
  1. Short-term memory (MemoryBase implementations) for current conversation/session state
  2. Long-term memory (LongTermMemoryBase implementations) for cross-session persistence and retrieval

Short-Term Memory

Short-term memory stores Msg objects and supports optional marks (for example: hint, summary, tool_result) for filtering and lifecycle management. Built-in short-term memory implementations:

Common API

All short-term memory classes inherit from MemoryBase and expose a unified async API:

InMemoryMemory: Basic Usage

InMemoryMemory is the easiest option for quick testing and local prototyping.

AsyncSQLAlchemyMemory: Basic Usage

AsyncSQLAlchemyMemory works with either an async engine or an async session.

AsyncSQLAlchemyMemory as Context Manager

When used as an async context manager, session cleanup is handled automatically.

SQLAlchemy Memory with FastAPI Pooling

In production, create and reuse an async engine/session maker with pooling:

RedisMemory: Basic Usage

RedisMemory is suitable for distributed services and horizontally scaled workers.

Redis Memory with FastAPI Pooling

Use a global Redis connection pool and create RedisMemory per request:

Customizing Short-Term Memory

To build a custom short-term memory backend, inherit from MemoryBase and implement the required methods:

Long-Term Memory

AgentScope provides long-term memory abstractions for cross-session persistence and retrieval.

Long-Term Memory Modes in ReActAgent

ReActAgent supports three long-term memory modes: When mode is agent_control or both, tool functions such as record_to_memory and retrieve_from_memory are registered in the toolkit.

Mem0LongTermMemory: Basic Usage

ReMePersonalLongTermMemory: Usage Patterns

ReMePersonalLongTermMemory supports both tool-style APIs and direct APIs:

Integrating Long-Term Memory with ReActAgent

For agent_control mode, add explicit instructions in the agent’s system prompt specifying when to record and retrieve memory. Without clear instructions, the agent may not use memory tools optimally.

Customizing Long-Term Memory

To implement your own long-term memory backend, inherit from LongTermMemoryBase and implement methods according to your target mode: If your backend supports all methods, it can be used in both mode.

Further Reading

Agent

Understand the agent’s core methods and the ReAct paradigm.

Tool

Learn how to extend agents with native functions, MCP, and skills.