Long-running agent sessions face two enemies: latency and "lost in the middle" syndrome. The ADK solves this with Context Caching and Context Compaction.
https://youtu.be/L3eKHw9df-g?si=y6T3LoCRDBJa495S
长周期的 Agent 会话面临两大敌人:延迟和“迷失在中间 (Lost in the middle)”综合征。 随着对话历史的增长,重复发送庞大的系统指令变得昂贵,且模型难以在近期的噪音中优先处理早期的规则。
ADK 通过双管齐下的方式解决了这个问题:
ADK 不会无休止地追加原始消息。 它采用滑动窗口机制,将较早的事件总结为简练的“记忆”块,同时保留最近的交互原件以确保精准度。
from google.adk.apps import App, EventsCompactionConfig
from google.adk.agents.context_cache_config import ContextCacheConfig
# Configure your App with both Caching and Compaction
app = App(
name='long-memory-agent',
root_agent=my_agent,
# 1. Cache heavy instructions
context_cache_config=ContextCacheConfig(
min_tokens=2048, # Only cache if prompt is heavy
ttl_seconds=1800, # Keep cache alive for 30 mins
cache_intervals=10 # Refresh after 10 uses
),
# 2. Compress history to prevent "Context Rot"
events_compaction_config=EventsCompactionConfig(
compaction_interval=3, # Summarize every 3 turns
overlap_size=1 # Keep 1 turn of context overlap
)
)