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

Google Advent of Agents:第 10 天

长周期的 Agent 会话面临两大敌人:延迟和“迷失在中间 (Lost in the middle)”综合征。 随着对话历史的增长,重复发送庞大的系统指令变得昂贵,且模型难以在近期的噪音中优先处理早期的规则。

ADK 通过双管齐下的方式解决了这个问题:

  1. 上下文缓存 (Context Caching):允许你缓存 Prompt 中不可变的部分(如系统指令、Few-shot 示例),这样你就无需为每一轮对话重复支付计算成本。
  2. 上下文压缩 (Context Compaction):防止历史记录膨胀。

ADK 不会无休止地追加原始消息。 它采用滑动窗口机制,将较早的事件总结为简练的“记忆”块,同时保留最近的交互原件以确保精准度。

参考资料连结

Code

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
    )
)