# Cross-Session User Memory Retrieval and Personalization Strategies in AI Agents

> Discover effective strategies for cross-session user memory retrieval and AI agent personalization. Learn how to maintain context and enhance user experience across conversations.

- Repository: [Bojie Li/ai-agent-book](https://github.com/bojieli/ai-agent-book)
- Tags: deep-dive
- Published: 2026-07-20

---

**The bojieli/ai-agent-book repository implements a layered memory architecture using `AworldMemory` with vector-based storage, automatic summarization, and user-specific retrieval APIs to maintain context and personalize interactions across disconnected conversation sessions.**

This article examines how the AWorld framework solves the challenge of cross-session user memory retrieval and personalization. By combining persistent vector embeddings with filtered search capabilities, the system allows AI agents to recall user profiles, facts, and previous experiences even when conversations span multiple independent sessions.

## Layered Memory Architecture with MemoryFactory

The foundation of cross-session persistence lies in the `MemoryFactory` class defined in [`chapter8/gaia-experience/AWorld/aworld/memory/main.py`](https://github.com/bojieli/ai-agent-book/blob/main/chapter8/gaia-experience/AWorld/aworld/memory/main.py). This factory creates a concrete `AworldMemory` implementation based on the `MemoryConfig` supplied (e.g., `provider="aworld"`). 

The factory caches the instance so the same memory store is reused across sessions. According to lines 155-167 in [`main.py`](https://github.com/bojieli/ai-agent-book/blob/main/main.py), calling `MemoryFactory.init()` establishes a singleton instance that persists for the application lifecycle, ensuring that memory data remains accessible regardless of when new sessions begin.

```python
from aworld.memory.main import MemoryFactory, MemoryConfig

# Initialize once at startup

MemoryFactory.init()                     # uses default InMemoryMemoryStore

memory = MemoryFactory.instance()        # shared instance for all sessions

```

## Short-Term and Long-Term Memory Separation

The system maintains a strict separation between **short-term** and **long-term** memory tiers. Short-term memory holds raw messages, summaries, and tool-call history for the current active session only. 

Long-term memory activation occurs through `post_process_long_terms` when a new message is added and the `enable_long_term` flag is true (lines 66-78 in [`main.py`](https://github.com/bojieli/ai-agent-book/blob/main/main.py)). This process aggregates user-level facts, profiles, and agent experience into persistent stores that survive session termination.

```python

# Adding a message triggers both short-term storage and long-term processing

from aworld.memory.models import MemoryHumanMessage, MessageMetadata

msg = MemoryHumanMessage(
    content="I love hiking in the Alps.",
    metadata=MessageMetadata(
        user_id="user_42",
        session_id="sess_1",
        task_id="task_a",
        role="user"
    )
)
await memory.add(msg, agent_memory_config=agent_cfg)  # Triggers long-term extraction if enabled

```

## Vector Embeddings and Persistent Storage

Every `MemoryItem` containing an `embedding_text` field is automatically converted to vector embeddings and saved to a vector database via `_save_to_vector_db` (lines 442-466 in [`main.py`](https://github.com/bojieli/ai-agent-book/blob/main/main.py)). 

Retrieval queries undergo the same embedding transformation. The `search` method (lines 752-760) executes similarity searches against the vector DB, enabling semantic retrieval of relevant historical data even when users phrase queries differently across sessions.

## Cross-Session Retrieval APIs

The memory class exposes high-level async methods that filter by `user_id` or `agent_id` regardless of the current `session_id`:

- **`retrival_user_profile(user_id, ...)`** – Fetches stored user profiles where `memory_type='user_profile'`
- **`retrival_facts(user_id, ...)`** – Retrieves factual snippets tied to a specific user
- **`retrival_agent_experience(agent_id, ...)`** – Fetches agent-centric experience from previous interactions

All three methods delegate to the generic `search` method with appropriate filters (lines 51-76 in [`main.py`](https://github.com/bojieli/ai-agent-book/blob/main/main.py)), ensuring that retrieval scope spans across all historical sessions for that specific user.

```python

# Retrieve user profile across all previous sessions

profile_items = await memory.retrival_user_profile(
    user_id="user_42",
    user_input="What gear should I bring for my next hike?",
    limit=3,
    threshold=0.7
)

# Direct fact retrieval for cross-session context

facts = await memory.retrival_facts(
    user_id="user_42",
    user_input="Tell me about my previous trips.",
    limit=5
)

```

## Context Filtering and Similarity Thresholds

When retrieving cross-session data, callers can pass additional filters such as `application_id` or `task_id` to narrow the search scope. The `threshold` parameter controls minimum similarity scores for returned items. 

These filtering capabilities ensure that agents retrieve only relevant historical context, preventing contamination from unrelated sessions while maintaining personalization accuracy.

## Automatic Summarization for Efficient Retrieval

To prevent context window overflow and improve retrieval efficiency, the system generates summaries when configurable round limits or token thresholds are exceeded. The `_check_need_summary` method (lines 595-607 in [`main.py`](https://github.com/bojieli/ai-agent-book/blob/main/main.py)) triggers this compression automatically.

Summaries are stored as `MemorySummary` items, providing compact representations of lengthy conversation histories that can be quickly injected into prompts during later sessions.

## Implementing a Personalization Workflow

A typical cross-session personalization flow operates through three distinct phases:

1. **Ingestion** – New messages trigger `_add`, which saves embeddings to the vector DB and may create a summary via `_check_need_summary`
2. **Extraction** – `post_process_long_terms` analyzes content to extract user profiles or agent experiences for long-term storage
3. **Retrieval** – In subsequent sessions, agents call `retrival_user_profile` or `retrival_facts` using the same `user_id`, incorporating returned items into the prompt context

This workflow enables agents to reference specific user preferences (like the Alps hiking example) months after the initial conversation, creating truly personalized interactions.

## Summary

- **MemoryFactory** creates singleton `AworldMemory` instances that persist across sessions via configuration-based initialization
- **Dual-tier storage** separates ephemeral session data from permanent user facts and agent experiences
- **Vector embeddings** power semantic search capabilities through `_save_to_vector_db` and the `search` method
- **User-centric APIs** like `retrival_user_profile` filter by `user_id` independently of `session_id` to enable true cross-session recall
- **Automatic summarization** compresses conversation history into reusable `MemorySummary` items for efficient long-term retrieval

## Frequently Asked Questions

### How does the system differentiate between short-term and long-term memory?

Short-term memory stores raw messages and immediate context for the active session only, while long-term memory activates when `enable_long_term` is true, triggering `post_process_long_terms` to extract persistent user facts and profiles. The long-term data survives session termination and remains accessible via retrieval APIs.

### What vector database backends are supported for cross-session retrieval?

The repository uses a vector database abstraction via `VectorFactory` in [`aworld/memory/vector/factory.py`](https://github.com/bojieli/ai-agent-book/blob/main/aworld/memory/vector/factory.py), which can instantiate backends such as Milvus or Pinecone. The core `AworldMemory` class interacts with these backends through the `_save_to_vector_db` and `search` methods, making the specific backend transparent to the retrieval logic.

### How does the personalization workflow handle new user sessions?

When a new session begins for an existing user, the agent calls retrieval methods like `retrival_user_profile` with the consistent `user_id`. The vector database returns the most relevant historical items regardless of their original `session_id`, allowing the agent to reconstruct personalized context from previous interactions.

### Can agents retrieve experience from other agents using this system?

Yes, through the `retrival_agent_experience(agent_id, ...)` method. This retrieves agent-centric experiences stored during previous sessions, enabling knowledge sharing across different agent instances or allowing the same agent to recall its own past problem-solving approaches when encountering similar user requests.