Memory Consolidation

Updated at:
Copy as MD

Memory Consolidation organizes raw memories collected across multiple conversations into more complete and stable conclusions. It helps agents reduce redundant information and conflicts between old and new states, and more quickly understand user preferences, long-term habits, and task status.

Problems addressed

  • The same preference is scattered across multiple memories, and the agent cannot determine which value to use.

  • Contradictory information coexists because old preferences are not cleaned up.

  • Fragmented knowledge cannot form a complete user profile.

Key capabilities

  • Merge duplicate information: Consolidates memories that are expressed multiple times with similar meanings into a single, more complete conclusion.

  • Resolve state changes: Combines old and new information to form the current effective state, reducing interference from outdated preferences.

  • Complete user profiles: Associates related information from different conversations to give the agent a more coherent understanding.

  • Retain source evidence: Consolidation results can be traced back to the original memories and conversations that support them for verification and auditing.

  • Participate in recall directly: Consolidation results are searched together with raw memories. Applications do not need to change their existing retrieval methods.

Use cases

  • Users repeatedly express preferences across conversations, such as dietary choices, travel habits, device settings, and communication preferences.

  • Customer service, education, or enterprise agents need to form stable user states and task conclusions from long-term interactions.

  • The number of memories keeps growing, and you need to reduce duplicate content to improve the information density of recall results.

  • Old and new information coexists, and you need the agent to prioritize the current user state while retaining historical evidence.

How to use

Mode

Trigger

Description

Automatic

Asynchronously triggered after each POST /memories

Set MEM0_ENABLE_CONSOLIDATION=true.

Manual API call

POST /consolidate?user_id=xxx

No need to enable automatic mode.

You can also trigger consolidation manually from the Tools > Consolidation page in the console. Enter the User ID or Agent ID (at least one is required) and click Trigger Consolidation.

The system consolidates multiple related raw memories into a more complete observation without losing the original evidence. You can use the refined conclusions directly, or review the related memories and original conversations when verification is needed.

Example

The following example writes three travel-related raw memories, triggers consolidation, and checks the result.

Step 1: Configure the service endpoint and authentication

export MEMORY_URL="https://api-longmemory-cn-beijing.opentrust.net"
export MEMORY_API_KEY="<your API key>"
export USER_ID="demo-user-001"

Step 2: Write three raw memories

curl -sS -X POST "${MEMORY_URL}/memories" \
  -H "Authorization: Token ${MEMORY_API_KEY}" \
  -H "Content: application/json" \
  -d '{"messages":[{"role":"user","content":"I always prefer aisle seats when flying for business trips."}],"user_id":"${USER_ID}","async_mode":false}'
curl -sS -X POST "${MEMORY_URL}/memories" \
  -H "Authorization: Token ${MEMORY_API_KEY}" \
  -H "Content: application/json" \
  -d '{"messages":[{"role":"user","content":"I prefer morning flights for business travel."}],"user_id":"${USER_ID}","async_mode":false}'
curl -sS -X POST "${MEMORY_URL}/memories" \
  -H "Authorization: Token ${MEMORY_API_KEY}" \
  -H "Content: application/json" \
  -d '{"messages":[{"role":"user","content":"Departing after 7 PM affects my sleep, so I usually avoid evening flights."}],"user_id":"${USER_ID}","async_mode":false}'

All three calls return event: "ADD", which indicates that the raw memories are written.

Step 3: Trigger memory consolidation

curl -sS -X POST \
  "${MEMORY_URL}/consolidate?user_id=${USER_ID}" \
  -H "Authorization: Token ${MEMORY_API_KEY}"

This call processes three raw memories and generates one observation:

{
  "memories_processed": 3,
  "observations_created": 1,
  "observations_updated": 0,
  "observations_deleted": 0,
  "status": "completed"
}

memories_processed indicates the number of raw memories involved in the consolidation. observations_created indicates the number of new consolidation conclusions.

Step 4: View the consolidation results

curl -sS \
  "${MEMORY_URL}/memories?user_id=${USER_ID}" \
  -H "Authorization: Token ${MEMORY_API_KEY}"

The query returns one observation and three raw memories:

{
  "results": [
    {
      "memory": "The user prefers morning flights for business travel and usually avoids flights departing after 7 PM because they affect sleep.",
      "fact_type": "observation"
    },
    {
      "memory": "The user usually avoids flights departing after 7 PM because they affect sleep.",
      "fact_type": "raw"
    },
    {
      "memory": "The user prefers morning flights for business travel.",
      "fact_type": "raw"
    },
    {
      "memory": "The user always prefers aisle seats when flying for business trips.",
      "fact_type": "raw"
    }
  ],
  "total": 4
}

Records with fact_type set to observation are consolidation results. The raw memories are retained for tracing and verification. In this example, the system merged two flight time preferences into one observation, while the seat preference remains as a separate topic.

Best practices

  • Use automatic mode for ongoing services: Allows new memories to participate in consolidation continuously in the background.

  • Trigger manually for large volumes of historical memories: Consolidate memories by user or agent scope in a batch.

  • Accumulate before refining: Multiple related memories produce more valuable conclusions. A single memory is usually kept as-is.

  • Combine with real-time updates: Use real-time updates for explicit state changes, and leave cross-session summarization to memory consolidation.

  • Retain raw memories: consolidation improves understanding and recall efficiency. Raw records remain available for tracing and auditing.