Skip to main content
When the context window fills up, AgentScope keeps it in shape with two automatic mechanisms governed by ContextConfig: context compression (summarize older messages) and tool result truncation (cap oversized tool outputs). Both run transparently; the agent continues working without interruption.

Configure Compression

ContextConfig is passed to the agent at construction time:
Available fields:

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.

Compress Manually

Compression can also be triggered manually by calling the agent’s compress_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:
The method is a no-op when token usage is below trigger_ratio × context_size, so it is safe to call between turns or at any custom checkpoint.

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.
Set 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 against tool_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:
When an offloader is attached, the marker also points the agent to the persisted full output:
Setting tool_result_limit too low may starve the agent of critical tool output. Setting it too high risks one result filling the entire context.