You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c91e368
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: sdk/guides/context-condenser.mdx
+59-6Lines changed: 59 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,13 +3,59 @@ title: Context Condenser
3
3
description: Manage agent memory by condensing conversation history to save tokens.
4
4
---
5
5
6
+
## What is a Context Condenser?
7
+
8
+
A **context condenser** is a crucial component that addresses one of the most persistent challenges in AI agent development: managing growing conversation context efficiently. As conversations with AI agents grow longer, the cumulative history leads to:
9
+
10
+
-**💰 Increased API Costs**: More tokens in the context means higher costs per API call
11
+
-**⏱️ Slower Response Times**: Larger contexts take longer to process
12
+
-**📉 Reduced Effectiveness**: LLMs become less effective when dealing with excessive irrelevant information
13
+
14
+
The context condenser solves this by intelligently summarizing older parts of the conversation while preserving essential information needed for the agent to continue working effectively.
OpenHands SDK provides `LLMSummarizingCondenser` as the default condenser implementation. This condenser uses an LLM to generate summaries of conversation history when it exceeds the configured size limit.
19
+
20
+
### How It Works
21
+
22
+
When conversation history exceeds a defined threshold, the LLM-based condenser:
23
+
24
+
1.**Keeps recent messages intact** - The most recent exchanges remain unchanged for immediate context
25
+
2.**Preserves key information** - Important details like user goals, technical specifications, and critical files are retained
26
+
3.**Summarizes older content** - Earlier parts of the conversation are condensed into concise summaries using LLM-generated summaries
27
+
4.**Maintains continuity** - The agent retains awareness of past progress without processing every historical interaction
This approach achieves remarkable efficiency gains:
33
+
- Up to **2x reduction** in per-turn API costs
34
+
-**Consistent response times** even in long sessions
35
+
-**Equivalent or better performance** on software engineering tasks
36
+
37
+
Learn more about the implementation and benchmarks in our [blog post on context condensation](https://openhands.dev/blog/openhands-context-condensensation-for-more-efficient-ai-agents).
38
+
39
+
### Extensibility
40
+
41
+
The `LLMSummarizingCondenser` extends the `RollingCondenser` base class, which provides a framework for condensers that work with rolling conversation history. You can create custom condensers by extending base classes ([source code](https://github.com/All-Hands-AI/agent-sdk/blob/main/openhands-sdk/openhands/sdk/context/condenser/base.py)):
42
+
43
+
-**`RollingCondenser`** - For condensers that apply condensation to rolling history
44
+
-**`CondenserBase`** - For more specialized condensation strategies
45
+
46
+
This architecture allows you to implement custom condensation logic tailored to your specific needs while leveraging the SDK's conversation management infrastructure.
47
+
48
+
49
+
### Example Usage
50
+
6
51
<Note>
7
52
This example is available on GitHub: [examples/01_standalone_sdk/14_context_condenser.py](https://github.com/All-Hands-AI/agent-sdk/blob/main/examples/01_standalone_sdk/14_context_condenser.py)
8
53
</Note>
9
54
55
+
10
56
Automatically condense conversation history when context length exceeds limits, reducing token usage while preserving important information:
To manage context in long-running conversations, the agent can use a context condenser
15
61
that keeps the conversation history within a specified size limit. This example
@@ -158,17 +204,24 @@ uv run python examples/01_standalone_sdk/14_context_condenser.py
158
204
159
205
### Setting Up Condensing
160
206
161
-
Configure a condenser when creating the agent:
207
+
Create a `LLMSummarizingCondenser` to manage the context.
208
+
The condenser will automatically truncate conversation history when it exceeds max_size, and replaces the dropped events with an LLM-generated summary.
209
+
210
+
This condenser triggers when there are more than `max_context_length` events in
211
+
the conversation history, and always keeps the first `keep_first` events (system prompts,
212
+
initial user messages) to preserve important context.
162
213
163
214
```python highlight={3-4}
164
-
from openhands.sdk.context importLLMCondenser
215
+
from openhands.sdk.context importLLMSummarizingCondenser
Metrics include: `input_tokens`, `output_tokens`, `cost`, `api_calls`, and `cache_reads` (if supported).
111
+
The `llm.metrics` object is an instance of the [Metrics class](https://github.com/All-Hands-AI/agent-sdk/blob/main/openhands-sdk/openhands/sdk/llm/utils/metrics.py), which provides detailed information including:
112
+
113
+
-`accumulated_cost` - Total accumulated cost across all API calls
114
+
-`accumulated_token_usage` - Aggregated token usage with fields like:
115
+
-`prompt_tokens` - Number of input tokens processed
116
+
-`completion_tokens` - Number of output tokens generated
117
+
-`cache_read_tokens` - Cache hits (if supported by the model)
118
+
-`cache_write_tokens` - Cache writes (if supported by the model)
119
+
-`reasoning_tokens` - Reasoning tokens (for models that support extended thinking)
120
+
-`context_window` - Context window size used
121
+
-`costs` - List of individual cost records per API call
122
+
-`token_usages` - List of detailed token usage records per API call
123
+
-`response_latencies` - List of response latency metrics per API call
124
+
125
+
For more details on the available metrics and methods, refer to the [source code](https://github.com/All-Hands-AI/agent-sdk/blob/main/openhands-sdk/openhands/sdk/llm/utils/metrics.py).
0 commit comments