Quick start
Memory Storage supports structured memory and file memory. Use structured memory to extract and retrieve long-term memories from conversations, or use file memory to manage Markdown or UTF-8 memory files directly.
Prerequisites
Before you begin, make sure that you have:
An active Tablestore service, a Tablestore instance in the China (Beijing) or China (Hangzhou) region, and the instance endpoint and name
AccessKey credentials (an AccessKey ID and an AccessKey Secret) or an API key. For API key creation, see API key management.
File memory
If you already have memory files or want to organize an agent memory directory yourself, use the Agent Storage SDK to write and read files directly. The following Python example installs the SDK, creates a file memory store, writes a file, and reads it back.
Install the Agent Storage SDK. File memory requires Python SDK 1.0.10 or later.
pip install tablestore-agent-storageInitialize the client, create a file memory store, and then write and read a file.
from tablestore_agent_storage import AgentStorageClient client = AgentStorageClient( api_key="<your-api-key>", ots_endpoint="https://<instance>.cn-beijing.ots.aliyuncs.com", ots_instance_name="<instance-name>", ) scope = { "appId": "app-001", "tenantId": "user-001", "agentId": "assistant", "runId": "session-001", } client.create_memory_store({ "memoryStoreName": "agent_files", "storageMode": "filemem", }) client.add_item({ "memoryStoreName": "agent_files", "scope": scope, "path": "/profile/preferences.md", "content": "# User preferences\n\n- Enjoys Americano coffee\n- Prefers concise responses\n", }) item = client.get_item({ "memoryStoreName": "agent_files", "scope": scope, "path": "/profile/preferences.md", }) print(item["content"])
For client initialization and additional usage, see Agent Storage SDK.
Structured memory
Structured memory lets you use the CLI, Agent Storage SDK, Tablestore native SDKs, or AI agent frameworks to create memory stores, add conversations, and retrieve long-term memories. Choose one access method.
Console
The AgentStorage console provides a visual interface for creating a memory store, adding and retrieving memories, viewing short-term and long-term memories, and organizing memories with Dream — no coding required.
Log on to the AgentStorage console, select a region, and create an AgentStorage instance. You can then create a memory store and add and retrieve memories in the console.
For the complete procedure, see Console guide.
CLI
Use the CLI to configure credentials, manage memory stores, add memories, and verify retrieval from the command line.
Install the CLI. Node.js 18 or later is required.
npm install -g @tablestore/tablestore-agent-cli tablestore-agent-cli versionConfigure AccessKey credentials, the region, and the Tablestore instance.
tablestore-agent-cli configure set access_key_id '<AccessKey ID>' tablestore-agent-cli configure set access_key_secret '<AccessKey Secret>' tablestore-agent-cli configure set region 'cn-beijing' tablestore-agent-cli configure set ots_endpoint 'https://<instance>.cn-beijing.ots.aliyuncs.com' tablestore-agent-cli configure set ots_instance_name '<instance-name>'Diagnose the Memory Storage configuration and connectivity.
tablestore-agent-cli doctor memoryCreate a structured memory store.
tablestore-agent-cli memory create \ --store agent_memory \ --description "Agent long-term memory store"Add a memory and wait for memory extraction to finish.
tablestore-agent-cli memory add \ --store agent_memory \ --app-id app-001 \ --tenant-id user-001 \ --agent-id assistant \ --run-id session-001 \ --text "The user enjoys coffee and prefers concise responses." \ --syncRetrieve long-term memories. Set
agentIdandrunIdto*to search across agents and sessions.tablestore-agent-cli memory search \ --store agent_memory \ --app-id app-001 \ --tenant-id user-001 \ --agent-id '*' \ --run-id '*' \ --query "What beverages does the user like?" \ --top-k 5View short-term memories for the current session. Short-term memory queries require the complete four-level Scope and do not support wildcards.
tablestore-agent-cli memory msg-list \ --store agent_memory \ --app-id app-001 \ --tenant-id user-001 \ --agent-id assistant \ --run-id session-001
--sync waits for memory extraction to finish. Index visibility might be delayed. If the first search returns no results, try again later.
For more CLI commands and memory store operations, see Agent Storage CLI and Memory store operations.
Agent Storage SDK
The following Python and TypeScript examples use an API key to initialize the client, create a memory store, add a memory, and run a search.
Python
Install the SDK:
pip install tablestore-agent-storageMinimal example:
from tablestore_agent_storage import AgentStorageClient
client = AgentStorageClient(
api_key="<your-api-key>",
ots_endpoint="https://<instance>.cn-beijing.ots.aliyuncs.com",
ots_instance_name="<instance-name>",
)
scope = {
"appId": "app-001",
"tenantId": "user-001",
"agentId": "assistant",
"runId": "session-001",
}
# 1. Create a memory store
client.create_memory_store({"memoryStoreName": "agent_memory"})
# 2. Add a memory
client.add_memories({
"memoryStoreName": "agent_memory",
"scope": scope,
"text": "The user enjoys coffee and prefers concise responses.",
"sync": True,
})
# 3. Run a semantic search
result = client.search_memories({
"memoryStoreName": "agent_memory",
"scope": {"appId": "app-001", "tenantId": "user-001", "agentId": "*", "runId": "*"},
"query": "What beverages does the user like?",
"topK": 5,
})
for item in result.get("results", []):
unit = item["unit"]
print(f"[{item['score']:.4f}] {unit['text']}")TypeScript
Install the SDK:
npm install @tablestore/agent-storageMinimal example:
import { AgentStorageClient } from '@tablestore/agent-storage';
const client = new AgentStorageClient({
apiKey: '<your-api-key>',
endpoint: 'https://<instance>.cn-beijing.ots.aliyuncs.com',
instanceName: '<instance-name>',
});
const scope = {
appId: 'app-001',
tenantId: 'user-001',
agentId: 'assistant',
runId: 'session-001',
};
// 1. Create a memory store
await client.createMemoryStore({ memoryStoreName: 'agent_memory' });
// 2. Add a memory
await client.addMemories({
memoryStoreName: 'agent_memory',
scope,
text: 'The user enjoys coffee and prefers concise responses.',
sync: true,
});
// 3. Run a semantic search
const result: any = await client.searchMemories({
memoryStoreName: 'agent_memory',
scope: { appId: 'app-001', tenantId: 'user-001', agentId: '*', runId: '*' },
query: 'What beverages does the user like?',
topK: 5,
});
for (const item of result.results ?? []) {
console.log(`[${item.score.toFixed(4)}] ${item.unit.text}`);
}Tablestore native SDK
Tablestore native SDKs are available for Python and Node.js. Use them to add Memory Storage to existing Tablestore applications. The native SDKs currently support only AccessKey authentication.
Python
Install the SDK. tablestore 6.4.7 or later is required.
pip install tablestoreMinimal example:
from tablestore import OTSClient
client = OTSClient(
"https://<instance>.cn-beijing.ots.aliyuncs.com",
"<AccessKey ID>",
"<AccessKey Secret>",
"<instance-name>",
)
scope = {
"appId": "app-001",
"tenantId": "user-001",
"agentId": "assistant",
"runId": "session-001",
}
# 1. Create a memory store
client.create_memory_store({"memoryStoreName": "agent_memory"})
# 2. Add a memory
client.add_memories({
"memoryStoreName": "agent_memory",
"scope": scope,
"text": "The user enjoys coffee and prefers concise responses.",
"sync": True,
})
# 3. Run a semantic search
result = client.search_memories({
"memoryStoreName": "agent_memory",
"scope": {"appId": "app-001", "tenantId": "user-001", "agentId": "*", "runId": "*"},
"query": "What beverages does the user like?",
"topK": 5,
})
for item in result.get("results", []):
unit = item["unit"]
print(f"[{item['score']:.4f}] {unit['text']}")Node.js
Install the SDK. tablestore 5.6.5 or later is required.
npm install tablestoreMinimal example:
const TableStore = require("tablestore");
const client = new TableStore.Client({
accessKeyId: "<AccessKey ID>",
secretAccessKey: "<AccessKey Secret>",
endpoint: "https://<instance>.cn-beijing.ots.aliyuncs.com",
instancename: "<instance-name>",
});
async function main() {
const scope = {
appId: "app-001",
tenantId: "user-001",
agentId: "assistant",
runId: "session-001",
};
await client.createMemoryStore({ memoryStoreName: "agent_memory" });
await client.addMemories({
memoryStoreName: "agent_memory",
scope,
text: "The user enjoys coffee and prefers concise responses.",
sync: true,
});
const result = await client.searchMemories({
memoryStoreName: "agent_memory",
scope: { appId: "app-001", tenantId: "user-001", agentId: "*", runId: "*" },
query: "What beverages does the user like?",
topK: 5,
});
for (const item of result.results ?? []) {
console.log(`[${item.score.toFixed(4)}] ${item.unit.text}`);
}
}
main().catch(console.error);For more information about the native SDKs, see Python SDK and Node.js SDK.
AI agent framework
To use Memory Storage with AI agent frameworks such as OpenClaw, Hermes, and Claude, install the corresponding plugin. The plugin lets the agent read from and write to memory stores without manual SDK calls.
For integration steps, see Agent ecosystem integration.