> ## 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.

> ## Agent Instructions
> For AgentScope Python, use https://docs.agentscope.io/stable/en/index for new projects. For existing projects, check the installed agentscope version and use matching versioned documentation.
> The /latest/ alias points to the newest documentation and may move ahead of /stable/ when a development version exists. Do not mix AgentScope 1.x and 2.x APIs.
> State the AgentScope version when providing installation commands or code examples. ReMe uses its own continuously updated /reme/latest/ documentation.

# Overview

> Manage the agent's working memory to keep long tasks on track

The context is an agent's working memory: the messages (user inputs, assistant responses, tool calls, tool results) that the LLM sees on every reasoning step. Context management is about more than fitting the model's window; it shapes what the model sees at each step so the agent completes tasks better, through three mechanisms:

| Mechanism | What It Does | Page |
| - | - | - |
| **Context injection** | Keeps the agent aware of runtime state that changes across turns (time, tasks, context usage) by injecting hints into the context | [Environment Awareness](https://docs.agentscope.io/en/versions/2.0.12dev/building-blocks/context/environment-awareness.md) |
| **Context compression** | Summarizes older messages and truncates oversized tool results, keeping long conversations within the model's window | [Compress Context](https://docs.agentscope.io/en/versions/2.0.12dev/building-blocks/context/compress-context.md) |
| **Context offloading** | Persists dropped content (compressed messages, truncated tool results) to external storage so details remain retrievable | [Offload Context](https://docs.agentscope.io/en/versions/2.0.12dev/building-blocks/context/offload-context.md) |

The three mechanisms work in concert: injection adds what the model needs to know now, compression removes what it no longer needs verbatim, and offloading keeps the removed content one file-read away.

## Assemble Context

Before each model call, the agent assembles a single API input from three layers. The structure below shows what flows into that call:

**Model API Input**

**System Prompt**

**Base system prompt**

**Skill instructions (from Toolkit)**

**on\_system\_prompt middleware transforms**

**Summary (compressed history, if any)**

**Context (recent uncompressed messages)**

How each layer is built:

1. **System prompt**: starts from the `system_prompt` passed at agent creation, then appends skill instructions (each skill's name and description, sourced from the toolkit), then runs every `on_system_prompt` [middleware](https://docs.agentscope.io/en/versions/2.0.12dev/building-blocks/middleware.md) hook in order.
2. **Summary**: the compressed digest of older messages, present only after a compression has occurred.
3. **Context**: the recent uncompressed messages (user inputs, assistant responses, tool calls, tool results). This is also where injected runtime-state hints live.

> [!TIP]
>
> Fixed information belongs in the system prompt (via the `on_system_prompt` middleware hook for dynamic composition); information that changes within a conversation belongs in the context, injected as hints. See [Environment Awareness](https://docs.agentscope.io/en/versions/2.0.12dev/building-blocks/context/environment-awareness.md) for the distinction.

## Next Steps

> **[Environment Awareness](https://docs.agentscope.io/en/versions/2.0.12dev/building-blocks/context/environment-awareness.md)**
>
> Inject time, tasks, and context usage so the agent stays oriented.

> **[Compress Context](https://docs.agentscope.io/en/versions/2.0.12dev/building-blocks/context/compress-context.md)**
>
> Keep the context length within the preset limit.

> **[Offload Context](https://docs.agentscope.io/en/versions/2.0.12dev/building-blocks/context/offload-context.md)**
>
> Persist dropped content so the agent can read it back on demand.

> **[Workspace](https://docs.agentscope.io/en/versions/2.0.12dev/building-blocks/workspace/overview.md)**
>
> Built-in offloader implementations and the agent's working environment.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.