Skip to main content
Context Compression allows you to manage your agent context while it is running, helping the agent stay within its context window and avoid rate limits or decreases in response quality. Think of it like a research assistant who reads lengthy reports and gives you the key bullet points instead of the full documents.

The Problem: Verbose Tool Results

If you are using tools with large response sizes, without compression, tool results quickly consume your context window: This quickly becomes expensive and hits context limits during complex workflows.

The Solution: Automatic Compression

Context compression summarizes tool results after a threshold:
Benefits:
  • Dramatically reduced token costs
  • Stay within context window limits
  • Preserve critical facts and data
  • Automatic compression

How It Works

Context compression follows a simple pattern:
1

Enable Compression

Set compress_tool_results=True on your agent or team, or provide a CompressionManager. The system monitors tool call results as they come in.
2

Threshold Reached

After the threshold is reached, compression is triggered. Each uncompressed tool call result is individually summarized.
3

Intelligent Summarization

The compression model preserves key facts (numbers, dates, entities, URLs) while removing boilerplate, redundancy, and filler text.
4

The LLM loop continues

The compressed tool results are used in the next LLM executions, reducing token usage and extending the life of your context window.
When using arun on Agent or Team, compression is handled asynchronously and the uncompressed tool call results are summarised concurrently.

Enable Compression

Turn on compress_tool_results=True to automatically compress tool results. This comes with a default threshold of 3 tool calls. For example:
You can also enable compress_tool_results=True on individual team members to compress their tool results independently.

Custom Compression

Provide a CompressionManager to customize the compression behavior:
Use a faster, cheaper model like gpt-4o-mini for compression to reduce latency and cost while using a more capable model as your Agent’s main model.

Compression Triggers

The CompressionManager supports two types of thresholds for triggering compression:
If neither threshold is set, compress_tool_results_limit defaults to 3.

Tool-Based Compression

Set compress_tool_results_limit when you have predictable tool call patterns and want compression to trigger after a fixed number of tool call results.

Token-Based Compression

Use compress_token_limit when you need precise control over context size, especially when tool results vary significantly in size:
Token counting includes messages, tool definitions, and output schemas. See Token Counting for details.

When to Use Context Compression

Perfect for:
  • Agents with tools that return verbose results (web search, APIs)
  • Multi-step workflows with many tool calls
  • Long-running sessions where context accumulates
  • Production systems where cost matters

Developer Resources