Conversation
AgentTool runs the wrapped agent through a nested Runner built without an App, so the caller's events_compaction_config never reaches the nested invocation. A wrapped agent that makes many tool calls keeps growing its own history until the model rejects the request (prompt + max_output_tokens over the context window), and the error aborts the caller's invocation even though the caller's own history is being compacted. The nested run now inherits the caller's token-threshold trigger (token_threshold and event_retention_size). The sliding-window trigger is not inherited: it runs after an invocation finishes, and the nested session is discarded when the tool returns. Fixes google#7397
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
AgentToolruns the wrapped agent through a nestedRunnercreated from a bare agent, so the nested run gets noevents_compaction_config. A wrapped agent that makes many tool calls keeps growing its own history until the model rejects the request (prompt +max_output_tokensover the context window). The resultingContextWindowExceededErroraborts the caller's invocation, even though the caller's own history is being compacted.Solution:
AgentTool.run_asyncnow builds the nestedAppitself (the constructionRunnerrecommends) and gives it the caller's token-threshold compaction settings (token_threshold,event_retention_size). The sliding-window trigger is not inherited: it runs after an invocation finishes, and the nested session is discarded when the tool returns, so it would only add a summarizer call. Building theAppalso replaces the deprecatedRunner(plugins=...)argument, which this code path used to pass.Testing Plan
Unit Tests:
I have added or updated unit tests for my change.
All unit tests pass locally.
New
test_agent_tool_compacts_nested_history_at_caller_token_threshold: the caller's app compacts at 100 tokens and the wrapped agent reads a large file three times. The wrapped agent's last request carries the compaction summary. Onmainthe same test fails (assert 'NESTED SUMMARY' in 'test1'), because the nested history is never compacted.New
test_agent_tool_compacts_each_call_and_keeps_caller_config: the root agent calls the agent tool twice. Each nested run compacts its own history, the second run starts from a fresh history, and the caller'sEventsCompactionConfigis left unchanged. Fails onmainfor the same reason.New
test_agent_tool_keeps_nested_history_without_token_threshold, parametrized over no compaction config and a sliding-window-only config: the wrapped agent keeps its full history and no summary is added. Passes onmainand with the fix; it guards against inheriting more than the token-threshold trigger.The existing
Runnerstubs intest_agent_tool.pynow takeapp=.pytest tests/unittests/tools/test_agent_tool.py: 58 passed on Python 3.10, 3.11, 3.12, 3.13 and 3.14.pytest tests/unittests -n auto(Python 3.12, rebased on 86a47f6): 17412 passed, 89 skipped, 25 xfailed, 2 xpassed.toxon Python 3.10, 3.11, 3.12, 3.13 and 3.14 passed in every environment on the previous revision, which had the same source change before the added tests and the rebase.pre-commit run --files src/google/adk/tools/agent_tool.py tests/unittests/tools/test_agent_tool.py: all hooks pass.mypy src/google/adk/tools/agent_tool.pyreports no new errors compared withmain.Manual End-to-End (E2E) Tests:
A root agent calls an
AgentTool-wrappedreaderagent, which reads four long chapters with a function tool before answering. Both agents usegemini-3.1-flash-lite; the app setsEventsCompactionConfig(token_threshold=2000, event_retention_size=1). Abefore_model_callbackonreaderlogs each request it sends:The same failure was observed in practice with google-adk 1.36.1 and
LiteLlm-> vLLM 0.19.1 serving Gemma 4 31B (max_model_len=32768): across about 130 sessions of a coding agent with anAgentTool-wrapped navigation agent, all 8 context-window failures came from the wrapped agent and none from the root agent, which compacts at 14,336 tokens.Checklist
Additional context
Related: #3230 (context caching for agent tools and sub agents). Thanks to @loyce-cheng for reviewing the fix on #7397 and suggesting the additional regression cases.
This targets
main; I can retarget it tov1if that branch is preferred.