> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentscope.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Context and Memory

> Background about Context and Memory

This document introduces how AgentScope handles context and memory in agent workflows.

<Info>
  For implementation details and APIs, see [Context and Memory](/versions/1.0.21/en/building-blocks/context-and-memory).
</Info>

***

## Why It Matters

Without memory, an agent treats every turn as a new conversation.
With memory, it can:

* keep conversation continuity,
* remember user preferences and task state,
* retrieve useful history when needed.

In AgentScope, this is built around `Msg`, memory backends, and prompt construction.

***

## Three Layers in AgentScope

### Context (inference-time input)

**Context** is the final input sent to the model for one inference call.

In AgentScope terms, it is typically assembled from:

* system instructions,
* current user `Msg`,
* selected short-term history,
* retrieved long-term memory,
* tool results.

Context is fast and direct, but limited by token budget.

### Short-Term Memory (session state)

Short-term memory tracks the current session and usually stores `Msg` objects.

In AgentScope, this is provided by `MemoryBase` implementations (for example:
`InMemoryMemory`, `RedisMemory`, `AsyncSQLAlchemyMemory`).

Common usage:

* recent conversation turns,
* temporary task progress,
* marked messages (such as `hint`, `summary`, `tool_result`).

### Long-Term Memory (cross-session knowledge)

Long-term memory stores information that should survive session boundaries.

In AgentScope, this is abstracted by `LongTermMemoryBase` implementations.
Typical content includes:

* stable user preferences,
* important facts from previous interactions,
* retrievable semantic memories.

***

## How They Work Together

A typical agent turn looks like this:

1. Load recent session memory (short-term).
2. Retrieve relevant long-term memory (if needed).
3. Build model context with current user input and retrieved signals.
4. Run model inference.
5. Write new messages back to short-term memory, and optionally persist key facts to long-term memory.

This loop is often managed inside the agent's reply lifecycle (for example, in `ReActAgent` workflows).

***

## Context Management vs Memory Management

In practice, the boundary is soft:

* **Memory management** focuses on storing, retrieving, marking, and updating information.
* **Context management** focuses on selecting and assembling the right subset of that information into the model input.

So memory provides the source material, and context decides what enters the current inference window.

***

## Summary

| Layer                 | What it is                         | AgentScope mapping                                            |
| --------------------- | ---------------------------------- | ------------------------------------------------------------- |
| **Context**           | Input for one inference            | Prompt assembled from `Msg` + retrieved memory + tool outputs |
| **Short-Term Memory** | Session-level working state        | `MemoryBase` backends storing and filtering `Msg`             |
| **Long-Term Memory**  | Persistent cross-session knowledge | `LongTermMemoryBase` retrieval and storage                    |

Good agent behavior depends less on storing everything, and more on selecting the **right memory at the right time**.
