๊ฐ€์ž… ํ›„ ์ดˆ๋Œ€ ๋งํฌ๋ฅผ ๊ณต์œ ํ•˜๋ฉด ๋™์˜์ƒ ์žฌ์ƒ ๋ฐ ์ดˆ๋Œ€ ๋ณด์ƒ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture ๆŠ•็จฟใฏๅ€‹ไบบใฎๆ„่ฆ‹ใงใ™ใ€‚
๊ฐ€์ž… May 2026
280 ํŒ”๋กœ์ž‰ ์ค‘    416 ํŒฌ
# 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#
๋” ๋ณด๊ธฐ