Skip to content

Commit c37bd27

Browse files
hangfeicopybara-github
authored andcommitted
feat: Introduce LLM context compaction interface
Provide a more efficient way to compact LLM context for better agentic performance. * `app`: the top level abstraction for an ADK application. It contains an root agent, and plugins. * `content_strategy`: the abstraction for selecting the contents for LLM request. * `compaction_strategy`: the abstraction for compacting the events. * Added `sequence_id` and `summary_range` in event class. PiperOrigin-RevId: 808634224
1 parent e86647d commit c37bd27

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

src/google/adk/apps/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
# limitations under the License.
1414
from __future__ import annotations
1515

16-
from abc import ABC
1716
from typing import Optional
1817

1918
from pydantic import BaseModel
2019
from pydantic import ConfigDict
2120
from pydantic import Field
2221

2322
from ..agents.base_agent import BaseAgent
23+
from ..apps.base_events_compactor import BaseEventsCompactor
2424
from ..plugins.base_plugin import BasePlugin
2525
from ..utils.feature_decorator import experimental
2626

@@ -50,3 +50,6 @@ class App(BaseModel):
5050

5151
plugins: list[BasePlugin] = Field(default_factory=list)
5252
"""The plugins in the application."""
53+
54+
event_compactor: Optional[BaseEventsCompactor] = None
55+
"""The event compactor strategy for the application."""
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
import abc
17+
from typing import Optional
18+
19+
from google.genai.types import Content
20+
21+
from ..events.event import Event
22+
from ..utils.feature_decorator import experimental
23+
24+
25+
@experimental
26+
class BaseEventsCompactor(abc.ABC):
27+
"""Base interface for compacting events."""
28+
29+
async def maybe_compact_events(
30+
self, *, events: list[Event]
31+
) -> Optional[Content]:
32+
"""A list of uncompacted events, decide whether to compact.
33+
34+
If no need to compact, return None. Otherwise, compact into a content and
35+
return it.
36+
37+
This method will summarize the events and return a new summray event
38+
indicating the range of events it summarized.
39+
40+
When sending events to the LLM, if a summary event is present, the events it
41+
replaces (those identified in itssummary_range) should not be included.
42+
43+
Args:
44+
events: Events to compact.
45+
agent_name: The name of the agent.
46+
47+
Returns:
48+
The new compacted content, or None if no compaction is needed.
49+
"""
50+
raise NotImplementedError()

src/google/adk/events/event_actions.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
from __future__ import annotations
1616

17-
from typing import Any
1817
from typing import Optional
1918

19+
from google.genai.types import Content
2020
from pydantic import alias_generators
2121
from pydantic import BaseModel
2222
from pydantic import ConfigDict
@@ -26,6 +26,23 @@
2626
from ..tools.tool_confirmation import ToolConfirmation
2727

2828

29+
class EventCompaction(BaseModel):
30+
"""The compaction of the events."""
31+
32+
model_config = ConfigDict(
33+
extra='forbid',
34+
alias_generator=alias_generators.to_camel,
35+
populate_by_name=True,
36+
)
37+
"""The pydantic model config."""
38+
39+
compaction_range: Optional[tuple[float, float]] = None
40+
"""The sequence ID range of the events that are summarized, in the form(start_sequence_id, end_sequence_id)`"""
41+
42+
compacted_content: Content
43+
"""The summarized content of the events."""
44+
45+
2946
class EventActions(BaseModel):
3047
"""Represents the actions attached to an event."""
3148

@@ -72,3 +89,6 @@ class EventActions(BaseModel):
7289
)
7390
"""A dict of tool confirmation requested by this event, keyed by
7491
function call id."""
92+
93+
compaction: Optional[EventCompaction] = None
94+
"""The compaction of the events."""

0 commit comments

Comments
 (0)