Long-Term Memory Architecture for LLM Agents: Offline "Dreaming" and Sleep-Time Compute

    This article examines a cross-session long-term memory architecture for LLM agents that borrows from how the human brain consolidates memories during sleep. It describes a four-layer memory design, an offline "dreaming" process that reviews and corrects past interactions, the idea of memory as prediction of future needs, and the engineering role of cloud-native infrastructure for log storage, offline scheduling, and memory retrieval. Readers will learn how agents can move from stateless tools to stateful learning systems that accumulate and refine experience across sessions.

    As large language model agents are used more widely in complex workflows, conventional context windows and single-session capabilities are no longer sufficient for long-horizon tasks. Agents generate dense interaction logs while executing tasks. Without an effective way to retain experience, every session effectively starts from zero. To address this problem, the industry has proposed a cross-session long-term memory architecture inspired by mechanisms in the human brain. By using idle compute between interactions for background “dreaming,” the system can prune, correct, and consolidate memories.

    1. Hierarchical Design of Cross-Session Memory Architecture

    Building practical long-term memory for agents is not simply a matter of concatenating historical dialogues or aggressively compressing them. It requires a rigorous hierarchical architecture. Such an architecture typically contains the following four core layers, with a closed loop across them:

    1. Raw Record Layer: This layer contains the original interaction logs generated during the agent’s daily work, including user instructions, tool call results, and intermediate reasoning steps. Raw records are large in volume and contain substantial noise. Injecting them directly into context would sharply increase token consumption and diffuse attention.
    2. Memory Note Layer: During a session, the agent tags observations, events, or rules it considers important and writes them directly to an external memory folder. This layer provides the raw material for processing by the “dreaming” mechanism.
    3. Memory Representation Layer: After offline processing, scattered memory notes are reorganized and distilled into structured long-term knowledge, such as reference documents, rule bases, or verified sets of facts.
    4. Context Injection Layer: At the start of a new session or during an ongoing session, the system retrieves and injects relevant memory representations on demand according to the current task, guiding the agent’s subsequent actions.

    It is important to emphasize that this agent memory strictly excludes fine-tuning or modification of model weights. It does not rely on in-context learning within a single conversation, nor does it replace context by directly compressing old dialogues into summaries. Instead, it is an independent experience repository read as plain-text context in later sessions.

    2. Sleep-Time Compute: Offline “Dreaming”

    The human brain produces a vast amount of experience every day. The hippocampus captures information in the moment and forms temporary memories. During offline states such as sleep, the hippocampus replays recent experiences, distills important content, and integrates it into the cortex as long-term knowledge. Academia and industry have introduced this division of labor into AI through the concept of sleep-time compute.

    Agents often have idle time between two interactions. The system can use this idle compute to let the model enter a background “dreaming” state. During this offline processing, the agent repeatedly replays and reorganizes historical logs to perform the following operations:

    • Reflect on the past: Compare expected and actual results in historical interactions to identify deviations.
    • Correct errors: Identify erroneous tool calls or reasoning paths that caused task failures, and correct them in memory notes or mark them with warning labels.
    • Consolidate experience: Distill rules or facts with general value and write them into the memory representation layer.

    In the human brain, emotional or chemical signals mark memories at critical moments so that they are replayed more often during sleep. Similarly, an agent can tag high-value events during real-time interaction to guide attention allocation during “dreaming.” However, this raises a technical challenge: how can an agent predict which current event will be important in the future? This relates to the core nature of memory mechanisms.

    3. Memory as Prediction of the Future: Building a Learning Loop

    From an information-theoretic perspective, taking notes is itself a prediction about the future. When an agent writes down a rule, it is essentially predicting that the rule will be useful in future tasks. When it records a verified fact, it is predicting that its future self will not need to spend compute again to verify that information when using it.

    In benchmarks that evaluate long-horizon memory capability, such as database query tasks, an agent needs to look up tables to answer a series of questions. After each answer, the system publishes the correct answer, and the agent can write the experience into a memory directory for later use. During this process, models at different capability stages exhibit very different learning patterns:

    • Initial form: The model often produces only a set of unchecked guesses, and the memory folder contains scattered and unverified theories. Such memory lacks confidence management and may even mislead subsequent tasks.
    • Advanced form: The model begins to show metacognitive ability. It can integrate scattered notes into a single reference document and strictly distinguish between “verified” and “unverified” details. This structured memory with confidence labels marks the formation of a genuine learning loop: the agent can not only accumulate knowledge but also recognize the boundaries of its own knowledge.

    4. Engineering Practice and Cloud-Native Infrastructure Support

    In engineering deployment, implementing this long-term memory architecture based on the “dreaming” mechanism depends on cloud-native infrastructure. Developers can build the system with general cloud-native capabilities such as Baidu AI Cloud:

    1. Log and raw record storage: Use object storage and logging services to collect massive raw logs from agent interactions with high concurrency and low cost.
    2. Sleep-time compute scheduling: Use message queues to capture signals indicating that the agent is idle, trigger a container platform to launch offline processing tasks, and use elastic compute resources to execute memory replay and reorganization logic without consuming compute for real-time interaction.
    3. Memory retrieval and injection: Store the organized memory representation layer in a vector database or relational database. Combined with retrieval-augmented generation (RAG), relevant historical experience can be precisely injected into new session contexts.

    In summary, a long-term memory architecture inspired by the human brain breaks through the bottleneck of conventional large models limited by context windows. By introducing sleep-time compute and an offline “dreaming” mechanism, agents can accumulate and correct experience over time, achieving a key transition from “stateless tools” to “stateful learning systems.” For enterprise applications, agents that can retain knowledge across sessions and self-correct will demonstrate higher reliability and execution efficiency in complex business processes.