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:
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’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.
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: