
Context Compaction is the act of compacting (summarizing) older conversation to reduce the size of the context that’s sent to the model. ADK does it by summarizing every N invocations (we call it compaction_interval
). Each summary also has a overlap in coverage to make sure information at the edge of the compaction_interval
doesn’t get lost (we call it overlap_size
). You will find these concepts similar to those in RAG.
The following diagram shows context compaction with compaction_interval
set to 3 invocations, and overlap_size
set to 1:
With this, let’s upgrade our agent by just defining an app and adding the compaction config:
app = App(
name='story_agent',
root_agent=root_agent,
events_compaction_config=EventsCompactionConfig(
compaction_interval=3,
overlap_size=1
),
)
The agent works the same way, but let’s inspect the request in ADK web. You can see that the first 6 chapters have been compacted into 2 summaries, just like our diagram above:
Yes, it’s that easy! And we are sending 868 tokens instead of 1427. 🚀
Source Credit: https://medium.com/google-cloud/2-minute-adk-context-compaction-in-a-snap-470da15c30f4?source=rss—-e52cf94d98af—4