ContextConfig: context compression (summarize older messages) and tool result truncation (cap oversized tool outputs). Both run transparently; the agent continues working without interruption. Beyond that, developers can compress at any time by hand, or leave the timing to the agent itself.
Configure Compression
ContextConfig is passed to the agent at construction time:
context_buffer_ratio must be smaller than trigger_ratio, so that the context usage is injected and the agent still has room to compress on its own before a hard compression happens. Otherwise the agent constructor raises a ValueError.Compress Automatically
Compression runs automatically before each reasoning step. The flow:1
Count tokens
The agent totals the tokens of the system prompt, summary, context, and tool schemas.
2
Check threshold
If total tokens exceed
trigger_ratio × context_size, compression activates. Otherwise the agent proceeds with the model call as usual.3
Split messages
Older messages are marked for compression; recent messages within
reserve_ratio × context_size are kept. Tool call / result pairs are kept intact across the split.4
Generate summary
The model produces a structured summary from the older messages, with five fields:
task_overview, current_state, important_discoveries, next_steps, context_to_preserve.5
Update state
The summary replaces the compressed messages; the reserved messages become the new context. The agent then continues its reasoning step.
The remaining 10% between
trigger_ratio (max 0.9) and the full context size is reserved for the compression model call itself: the model needs room to generate the summary.compression_fallback_to_truncation decides what happens: by default the oldest messages are dropped and a truncation note is left where the summary would go, so the agent keeps running with a shortened context; with False an error is raised and the context is left untouched, at the risk of exceeding the model’s context size.
Compress Manually
Compression can also be triggered manually by calling the agent’scompress_context() method. Without arguments, it uses the agent’s stored context_config; pass a one-off ContextConfig to override, or an instructions HintBlock to guide the summarization:
trigger_ratio × context_size, so it is safe to call between turns or at any custom checkpoint.
Compress Agentically
Automatic compression fires the moment the threshold is crossed, which often lands in the middle of an unfinished piece of work, so the summary tends to lose the details still in flight. Setcompression_tool_enabled to True and the agent gets a CompressContext tool, letting it compress ahead of the hard threshold at the boundary between two pieces of work:
Enable agentic compression
Limit Images
max_image_num prevents images from accumulating in the model context over a long conversation. Once the count exceeds the limit, AgentScope removes images starting from the oldest and leaves a hint in their place:
- If the agent is configured with an
offloader, the image is persisted first and the hint carries the path for re-reading it; - Without an
offloader, the image is dropped and the hint only records that it was removed by the image limit.
max_image_num=0 to keep no images in the model context at all.
Truncate Tool Results
After each tool call, the agent compares the result’s token count againsttool_result_limit. If the limit is exceeded, the result is split into a reserved portion (kept in context) and an offloaded portion (handed to the offloader if one is attached, see Offload Context).
A truncation marker is appended to the reserved portion so the agent knows the output was clipped: