Skip to main content

Handle time-sensitive memories

Temporary states in long-term memory expire over time. Use the timestamp carried by each memory so the model perceives when it was recorded, avoiding treating stale information as current fact.

Much of what long-term memory stores is a temporary state rather than a permanent fact: an injury recovery period, a short-term dietary restriction, exam preparation, a business trip. If your application injects only memory text into the context, the model cannot tell a state recorded yesterday from one recorded six months ago, and will answer as if an expired state were still true. This page describes a field-tested approach: use the timestamp carried by each memory so the model perceives how old it is when answering.

The memory timestamp

Every memory node carries a timestamp field (Unix time in seconds), returned by Search memory, List memories, and Get memory node:
{
  "memory_node_id": "7cf9b14c4e5b42b183bfb5630b69151d",
  "content": "The user is recovering from an ankle sprain and avoids high-intensity lower-body training",
  "timestamp": 1794365139,
  "created_at": 1794365141
}
  • timestamp is the message time (when the server received that turn of conversation); created_at is when the memory was written. In real-time integrations they are close; rely on timestamp.
  • timestamp is recorded by the server and cannot be set by the caller. To backfill historical conversations while keeping original dates, write the original date into the memory text itself (for example, "(2026-09-01) The user ...").
After retrieval, format each timestamp as a readable date and inject it into the system prompt together with today's date and an explicit staleness instruction:
  1. When memories are recalled, read each node's timestamp and format it as a date (for example, 2026-10-12).
  2. Prefix each injected memory with [Recorded on YYYY-MM-DD].
  3. In the system instruction, state today's date and explicitly ask the model to judge memory freshness, for example: "Each memory carries its recording date; judge against today's date whether it is still valid, and do not treat clearly expired life-state memories as current facts."
Adding the timestamp without the freshness instruction is not enough: most models ignore the date and still answer as if the memory were current. Use both together.
Retrieval defaults to min_score=0.3 (pro plan), which can filter out relevant memories with modest scores. If recall returns empty, lower min_score first to troubleshoot; see Search memory.
from datetime import datetime
import requests

MEM_BASE = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v2/apps/memory"
LLM_URL = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions"
HEADERS = {"Authorization": "Bearer $DASHSCOPE_API_KEY", "Content-Type": "application/json"}

# 1. Retrieve memories
resp = requests.post(f"{MEM_BASE}/memory_nodes/search", headers=HEADERS, json={
    "messages": [{"role": "user", "content": user_query}],
    "user_id": user_id,
}).json()

# 2. Format each timestamp as a date prefix
lines = []
for node in resp.get("memory_nodes", []):
    day = datetime.fromtimestamp(node["timestamp"]).strftime("%Y-%m-%d")
    lines.append(f"[Recorded on {day}] {node['content']}")

# 3. Inject today's date and the freshness instruction into the system prompt
today = datetime.now().strftime("%Y-%m-%d")
system_prompt = (
    f"You are a fitness coaching assistant. Today is {today}. The following long-term memories "
    "about this user each carry a recording date; judge against today's date whether each is still "
    "valid, and do not treat clearly expired life-state memories as current facts:\n" + "\n".join(lines)
)

answer = requests.post(LLM_URL, headers=HEADERS, json={
    "model": "qwen3.6-plus",
    "messages": [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_query},
    ],
}).json()

Before and after

With a memory written six weeks ago, "The user is recovering from an ankle sprain and avoids high-intensity lower-body training", the user asks: "I want to schedule squats today. Is that OK?"
Injection styleModel behavior
Memory text onlyRefuses as a current fact: "Not recommended; you are in recovery."
Text + recording dateStill answers as if current; the date goes unused
Text + recording date + freshness instructionRecognizes the memory as expired: "That record is from 6 weeks ago; a typical sprain recovery has ended. You can start with light weights."

Combine with memory expiration

Timestamp injection handles freshness at the model-reasoning layer. If a class of memory should become unrecallable after a period, configure memory expiration on the memory rule (7 days / 30 days / 180 days / never; see Configure memory rules). Expired memories no longer participate in retrieval. The two mechanisms stack: expiration enforces hard expiry, while the timestamp lets the model reason about memories that are still within their validity period.

Scope

  • Applies to applications that orchestrate memory injection themselves through the API. When integrating through the console Agent Harness or the workflow plugin, the platform handles memory injection and this approach does not apply.
  • Tune the wording of the freshness instruction to your business, and regression-test it against your production model.