Skip to main content
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: 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 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.
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 for the distinction.

Next Steps

Environment Awareness

Inject time, tasks, and context usage so the agent stays oriented.

Compress Context

Keep the context length within the preset limit.

Offload Context

Persist dropped content so the agent can read it back on demand.

Workspace

Built-in offloader implementations and the agent’s working environment.