From 23a3b062945b70fa27650b568920b91860d1958e Mon Sep 17 00:00:00 2001 From: machache Date: Sun, 27 Jul 2025 23:29:56 +0200 Subject: [PATCH 1/2] feat: add support for ContetxtWindowCompressionConfig in RunConfig --- src/google/adk/agents/run_config.py | 5 ++ src/google/adk/flows/llm_flows/basic.py | 3 + .../streaming/test_live_streaming_configs.py | 56 +++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/src/google/adk/agents/run_config.py b/src/google/adk/agents/run_config.py index 52d8a9f570..e7e0cc404b 100644 --- a/src/google/adk/agents/run_config.py +++ b/src/google/adk/agents/run_config.py @@ -82,6 +82,11 @@ class RunConfig(BaseModel): session_resumption: Optional[types.SessionResumptionConfig] = None """Configures session resumption mechanism. Only support transparent session resumption mode now.""" + context_window_compression: Optional[types.ContextWindowCompressionConfig] = ( + None + ) + """Configuration for context window compression. If set, this will enable context window compression for LLM input.""" + max_llm_calls: int = 500 """ A limit on the total number of llm calls for a given run. diff --git a/src/google/adk/flows/llm_flows/basic.py b/src/google/adk/flows/llm_flows/basic.py index c5dfbd1c24..176c518870 100644 --- a/src/google/adk/flows/llm_flows/basic.py +++ b/src/google/adk/flows/llm_flows/basic.py @@ -77,6 +77,9 @@ async def run_async( llm_request.live_connect_config.session_resumption = ( invocation_context.run_config.session_resumption ) + llm_request.live_connect_config.context_window_compression = ( + invocation_context.run_config.context_window_compression + ) # TODO: handle tool append here, instead of in BaseTool.process_llm_request. diff --git a/tests/unittests/streaming/test_live_streaming_configs.py b/tests/unittests/streaming/test_live_streaming_configs.py index 5926c42f52..a25cb48e07 100644 --- a/tests/unittests/streaming/test_live_streaming_configs.py +++ b/tests/unittests/streaming/test_live_streaming_configs.py @@ -586,3 +586,59 @@ def test_streaming_with_session_resumption_config(): llm_request_sent_to_mock.live_connect_config.session_resumption.transparent is True ) + + +def test_streaming_with_context_window_compression_config(): + """Test streaming with context window compression config.""" + response = LlmResponse(turn_complete=True) + + mock_model = testing_utils.MockModel.create([response]) + + root_agent = Agent( + name='root_agent', + model=mock_model, + tools=[], + ) + + runner = testing_utils.InMemoryRunner( + root_agent=root_agent, response_modalities=['AUDIO'] + ) + + # Create run config with context window compression + run_config = RunConfig( + context_window_compression=types.ContextWindowCompressionConfig( + trigger_tokens=1000, + sliding_window=types.SlidingWindow(target_tokens=500), + ) + ) + + live_request_queue = LiveRequestQueue() + live_request_queue.send_realtime( + blob=types.Blob(data=b'\x00\xFF', mime_type='audio/pcm') + ) + + res_events = runner.run_live(live_request_queue, run_config) + + assert res_events is not None, 'Expected a list of events, got None.' + assert ( + len(res_events) > 0 + ), 'Expected at least one response, but got an empty list.' + assert len(mock_model.requests) == 1 + + # Get the request that was captured + llm_request_sent_to_mock = mock_model.requests[0] + + # Assert that the request contained the correct configuration + assert llm_request_sent_to_mock.live_connect_config is not None + assert ( + llm_request_sent_to_mock.live_connect_config.context_window_compression + is not None + ) + assert ( + llm_request_sent_to_mock.live_connect_config.context_window_compression.trigger_tokens + == 1000 + ) + assert ( + llm_request_sent_to_mock.live_connect_config.context_window_compression.sliding_window.target_tokens + == 500 + ) From c8a5b15cae2d2b72f331797d07ae0bbaf977ed3c Mon Sep 17 00:00:00 2001 From: Hangfei Lin Date: Wed, 15 Oct 2025 10:02:50 -0700 Subject: [PATCH 2/2] Update run_config.py --- src/google/adk/agents/run_config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/google/adk/agents/run_config.py b/src/google/adk/agents/run_config.py index b3cbe92325..754970537e 100644 --- a/src/google/adk/agents/run_config.py +++ b/src/google/adk/agents/run_config.py @@ -104,6 +104,7 @@ class RunConfig(BaseModel): Right now, only audio is supported. """ + max_llm_calls: int = 500 """ A limit on the total number of llm calls for a given run.