Manage long-term memory in a memory store, including writing, searching, retrieving, listing, updating, and deleting.
Prerequisites
-
A memory store has been created with the input type
messagesand an output type that includesstructured. See Memory store management. -
Agent Storage SDK installed: Python ≥
1.0.10, TypeScript ≥0.0.13
Write memory
Write conversation messages or text to the memory store. The service saves the original messages as short-term memory and asynchronously extracts structured long-term memory.
-
scope.appIdis required and cannot be*; whentenantId/agentId/runIdare empty, they are filled with__default__. Wildcard characters are not allowed in write operations. -
messagessupports up to 20 items with a total size ≤ 32,000 bytes; alternatively, you can usetext(≤ 32,000 bytes), but not both. -
By default, the operation is asynchronous (
sync=false): original messages are immediately available for query, while structured memory is produced in the background. Save the returnedrequestIdand callGetMemoryTaskto track the extraction task. -
Memory stores that include
fileoutput only support asynchronous writes;sync=trueonly applies to stores withoutfileoutput, otherwise the errorINVALID_PARAMETERis returned.
Console
-
Sign in to the Agent Storage console, open the target memory store, go to the Long-term memory page, and click Write memory.
-
Fill in the Scope (application, tenant, agent, session).
-
Enter conversation messages (role + content) or plain text.
-
Click OK. The service asynchronously extracts structured memory.
SDK
Python
from tablestore_agent_storage import AgentStorageClient
client = AgentStorageClient(
api_key="<your-api-key>",
ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
ots_instance_name="<instance-name>",
)
resp = client.add_memories({
"memoryStoreName": "agent_memory",
"scope": {
"appId": "app-001",
"tenantId": "user-001",
"agentId": "assistant",
"runId": "session-001",
},
"messages": [
{"role": "user", "content": "I like drinking Americano coffee"},
{"role": "assistant", "content": "Got it. I've noted your preference."},
],
})
print(f"requestId: {resp.get('requestId')}, status: {resp.get('status')}")
TypeScript
import { AgentStorageClient } from '@tablestore/agent-storage';
const client = new AgentStorageClient({
apiKey: '<your-api-key>',
endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
instanceName: '<instance-name>',
});
const resp = await client.addMemories({
memoryStoreName: 'agent_memory',
scope: {
appId: 'app-001',
tenantId: 'user-001',
agentId: 'assistant',
runId: 'session-001',
},
messages: [{ role: 'user', content: 'I like drinking Americano coffee' }],
});
console.log(resp.requestId, resp.status);
API
Call AddMemories to write memory. For complete fields, see Write memories.
Search memory
Use natural language to search structured memory and service-generated memory files, returning results sorted by relevance.
-
The search targets structured memory and service-generated memory files; directly managed file memory is not included in search scope (read via the path in File memory or create one via Knowledge compilation (Wiki)).
-
querysupports up to 8,192 Unicode characters;scope.appIdis required and cannot be*. Other fields must be explicitly set to*to act as wildcards.
Console
-
Open the target memory store, go to the Memory search page, and set specific values for the application and tenant (agent and session can be set to "All").
-
Enter the query text in Search query , set Top K, and optionally enable reranking or expand Advanced search.
-
Click Search. Results are displayed by category (structured memory, file memory, and search evidence), including relevance score, similarity score, and recall method.
SDK
Python
from tablestore_agent_storage import AgentStorageClient
client = AgentStorageClient(
api_key="<your-api-key>",
ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
ots_instance_name="<instance-name>",
)
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,
"enableRerank": True,
})
for hit in result.get("results", []):
unit = hit.get("unit", {})
print(f"[score={hit.get('score'):.4f}] {unit.get('text', '')[:80]}")
TypeScript
import { AgentStorageClient } from '@tablestore/agent-storage';
const client = new AgentStorageClient({
apiKey: '<your-api-key>',
endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
instanceName: '<instance-name>',
});
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,
enableRerank: true,
});
for (const hit of result.results) {
console.log(`[score=${hit.score.toFixed(4)}] ${hit.unit?.text?.slice(0, 80)}`);
}
API
Call SearchMemories to search memory. For complete fields, see Search Memories.
List memory
Paginate through the long-term memory list under a specified Scope.
-
scopesupports left-prefix wildcard matching (the right side must also be*or empty); a single request scans up to 100 Scopes. -
limitdefaults to 50 and supports a maximum of 100;nextTokenmust be used with the samescope+filtercombination.
Console
On the Long-term memory page, set the Scope. The list displays memory content, type, creation time, and actions.
SDK
Python
from tablestore_agent_storage import AgentStorageClient
client = AgentStorageClient(
api_key="<your-api-key>",
ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
ots_instance_name="<instance-name>",
)
resp = client.list_memories({
"memoryStoreName": "agent_memory",
"scope": {"appId": "app-001", "tenantId": "user-001", "agentId": "*", "runId": "*"},
"limit": 50,
})
for m in resp.get("memories", []):
print(f"[{m.get('type_label')}] {m.get('text', '')[:60]} (id={m.get('id')})")
TypeScript
import { AgentStorageClient } from '@tablestore/agent-storage';
const client = new AgentStorageClient({
apiKey: '<your-api-key>',
endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
instanceName: '<instance-name>',
});
const resp = await client.listMemories({
memoryStoreName: 'agent_memory',
scope: { appId: 'app-001', tenantId: 'user-001', agentId: '*', runId: '*' },
limit: 50,
});
for (const m of resp.memories) {
console.log(`[${m.type_label}] ${m.text?.slice(0, 60)} (id=${m.id})`);
}
API
Call ListMemories to list memory. For complete fields, see List memories.
Retrieve memory
Retrieve the details of a specified long-term memory.
Console
In the Long-term memory list, click View on the target memory to see its ID, content, search text, Scope, type, salience, and version.
SDK
Python
from tablestore_agent_storage import AgentStorageClient
client = AgentStorageClient(
api_key="<your-api-key>",
ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
ots_instance_name="<instance-name>",
)
resp = client.get_memory({
"memoryStoreName": "agent_memory",
"memoryId": "<memory-id>",
"scope": {"appId": "app-001", "tenantId": "user-001", "agentId": "assistant", "runId": "session-001"},
})
m = resp.get("memory", {})
print(f"Type: {m.get('type_label')}, Content: {m.get('text')}, Salience: {m.get('salience')}")
TypeScript
import { AgentStorageClient } from '@tablestore/agent-storage';
const client = new AgentStorageClient({
apiKey: '<your-api-key>',
endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
instanceName: '<instance-name>',
});
const resp = await client.getMemory({
memoryStoreName: 'agent_memory',
memoryId: '<memory-id>',
scope: { appId: 'app-001', tenantId: 'user-001', agentId: 'assistant', runId: 'session-001' },
});
const m = resp.memory;
console.log(m.type_label, m.text, m.salience);
API
Call GetMemory to retrieve memory. For complete fields, see Query memory.
Update memory
Update the text, metadata, and other fields of a specified long-term memory.
-
metadatais updated by overwrite; passing{}clears all metadata. -
effectiveAtandtimePrecisionmust be passed together.
Console
The console does not support editing individual long-term memory. Use the SDK or API to perform updates.
SDK
Python
from tablestore_agent_storage import AgentStorageClient
client = AgentStorageClient(
api_key="<your-api-key>",
ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
ots_instance_name="<instance-name>",
)
client.update_memory({
"memoryStoreName": "agent_memory",
"memoryId": "<memory-id>",
"scope": {"appId": "app-001", "tenantId": "user-001", "agentId": "assistant", "runId": "session-001"},
"text": "The user likes drinking latte",
"metadata": {"source": "profile"},
})
TypeScript
import { AgentStorageClient } from '@tablestore/agent-storage';
const client = new AgentStorageClient({
apiKey: '<your-api-key>',
endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
instanceName: '<instance-name>',
});
await client.updateMemory({
memoryStoreName: 'agent_memory',
memoryId: '<memory-id>',
scope: { appId: 'app-001', tenantId: 'user-001', agentId: 'assistant', runId: 'session-001' },
text: 'The user likes drinking latte',
metadata: { source: 'profile' },
});
API
Call UpdateMemory to update memory. For complete fields, see Update a memory.
Delete memory
Deleting a single long-term memory is irreversible.
Console
The console does not support deleting individual long-term memory. Use the SDK or API to perform deletion. To clear all data in a store, delete the entire memory store.
SDK
Python
from tablestore_agent_storage import AgentStorageClient
client = AgentStorageClient(
api_key="<your-api-key>",
ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
ots_instance_name="<instance-name>",
)
client.delete_memory({
"memoryStoreName": "agent_memory",
"memoryId": "<memory-id>",
"scope": {"appId": "app-001", "tenantId": "user-001", "agentId": "assistant", "runId": "session-001"},
})
TypeScript
import { AgentStorageClient } from '@tablestore/agent-storage';
const client = new AgentStorageClient({
apiKey: '<your-api-key>',
endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
instanceName: '<instance-name>',
});
await client.deleteMemory({
memoryStoreName: 'agent_memory',
memoryId: '<memory-id>',
scope: { appId: 'app-001', tenantId: 'user-001', agentId: 'assistant', runId: 'session-001' },
});
API
Call DeleteMemory to delete memory. For complete fields, see Delete memory.