# Practical and Useful Patterns with ADK
## ๐ง Keep Long Sessions Cost-Efficient with Context Compaction
As agent conversations grow longer, context balloons and so do costs and latency. ADK's **Context Compaction** automatically summarizes old events, keeping your context lean and your wallet happy! ๐ฏ
## ๐ Title
Context Compaction
## ๐ URL
## ๐งฉ Overview
Context Compaction reduces processing overhead by automatically summarizing older workflow event data during agent execution. Using a sliding window approach, it keeps recent events intact while compressing older ones, optimizing both cost and latency.
Configure it with `EventsCompactionConfig` by setting the `compaction_interval` (how often compression triggers) and `overlap_size` (how many previous events carry over into the next compression batch).
## ๐ How to Use
Set up `EventsCompactionConfig` at the App level:
Import `App` and `EventsCompactionConfig` from ` Pass `EventsCompactionConfig(compaction_interval=3, overlap_size=1)` to the `App`'s `events_compaction_config` parameter, which triggers compression every 3 events while keeping 1 event of overlap from the previous batch.
In TypeScript, you can use token-threshold-based compaction with `TokenBasedContextCompactor`:
```typescript
const agent = new LlmAgent({
name: 'my-agent',
model: 'gemini-flash-latest',
contextCompactors: [
new TokenBasedContextCompactor({
tokenThreshold: 1000,
eventRetentionSize: 1,
summarizer: new LlmSummarizer({
llm: new Gemini({model: 'gemini-flash-latest'})
})
})
]
});
```
## ๐ Practical Usage
**Customer support bot example:**
In long support conversations that span dozens of turns, Context Compaction delivers:
1. **Cost reduction**: Auto-summarize old dialogue to dramatically cut tokens sent per LLM call
2. **Faster responses**: Smaller context means faster LLM processing
3. **Maintained accuracy**: Recent exchanges stay intact, preserving conversational flow
With `compaction_interval=5, overlap_size=2`, compression fires every 5 turns while carrying 2 turns of context into the next window.
**Custom summarizers:**
Use domain-specific summarization prompts to ensure critical business information (order numbers, customer IDs, etc.) is always preserved in summaries.
## ๐ก Use Cases
- ๐ **Customer support**: Prevent context explosion in lengthy support tickets
- ๐ **Document authoring**: Summarize past discussions while keeping the latest direction in long writing sessions
- ๐ **Data analysis agents**: Compress intermediate results across multi-step analysis pipelines
- ๐ฎ **Game NPCs**: Summarize past events to maintain memory over long play sessions
## โ ๏ธ Caveats
- Compaction is irreversible; fine-grained details may be lost in summarization
- Too small an `overlap_size` can cause context discontinuity
- Custom summarizer models add their own cost overhead
- Too-frequent compression intervals increase processing overhead
## โจ Closing
Context Compaction breaks the assumption that "long sessions = high costs." With a single configuration, old events are auto-summarized while fresh context stays intact, optimizing both cost and latency. If your agents handle long-running conversations, this feature is a must-have!
#
ADK# #
AIAgent#