Repository navigation
feat(plugins): add ToolResultContextPlugin to offload oversized tool results to artifacts - #8512
Open
karthik-0306 wants to merge 1 commit into
Open
karthik-0306 wants to merge 1 commit into
karthik-0306 wants to merge 1 commit into
Conversation
…results to artifacts Tool results estimated above a token limit are saved in full through the artifact service and replaced in the session and model context by a stub with a head/tail preview, the original size and the artifact name. Phase 1 of google#8364.
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:
A single tool call (web fetch, SQL query, MCP or OpenAPI call) can return tens of thousands of tokens. ADK stores that result in the session as-is and re-sends it on every later model call. Token-threshold compaction cannot shrink it: the newest events are always kept raw, and when a tool result is summarized it is cut to 2,000 characters and cannot be recovered.
Solution:
Adds
ToolResultContextPlugin, configured withToolResultContextConfig/OffloadPolicy, all exported fromgoogle.adk.plugins.on_event_callback, any function response estimated above the token limit (default 4,000, four characters per token, the same heuristic as token-threshold compaction) is saved in full through the invocation's artifact service as a session-scoped text artifact.status,result_id(the artifact name),original_tokens, a head/tailpreview, and amessagefor the model. The callidandnameare kept, so call/response pairing is unchanged; only the payload is replaced.actions.artifact_delta, like an artifact a tool saves itself.per_tool_max_result_tokens),exclude_tools, andoffload_errors(error results stay inline by default).adk_request_credential/confirmation/input), responses withwill_continue, results the caller posts back for long-running tools, and partial events.status: truncated) with a one-time warning. Artifact save failure: the full result is kept inline and a warning is logged, so nothing is lost.Why
on_event_callbackrather thanafter_tool_callback: a plugin that returns a value fromafter_tool_callbackshort-circuits the agent's ownafter_tool_callbacks. Rewriting at the event level means every tool callback still sees the complete result, and the size measured is the final function response.Includes a unit guide at
docs/guides/plugins/tool_result_context_plugin/index.md, listed indocs/guides/README.md.Follow-ups planned in #8364: a
load_tool_resulttool for paging through offloaded results, cache-aware clearing of stale results in the outgoing request, and telemetry + a sample.Testing Plan
Unit Tests:
New tests in
tests/unittests/plugins/test_tool_result_context_plugin.pyandtests/unittests/plugins/test_tool_result_context_plugin_config.py:artifact_delta; small results are untouched; agentafter_tool_callbacks still receive the complete result; parallel calls go to separate artifacts.preview_tokens=0, per-tool limits, exclusions, client function responses, error results (default andoffload_errors=True),will_continue, user-posted results, partial events, mixed small/large parts in one event, call id and name preserved, input event not mutated, missing call id, unsafe characters in artifact names, no artifact service, failed save, disabled offload, default limit, non-tool events.preview_tokenssmaller than every limit,extra='forbid'.The
tests/unittests/plugins,tests/unittests/apps,tests/unittests/test_runners.pyandtests/unittests/test_import_loading.pysuites also pass, apart from BigQuery analytics tests that fail identically on a cleanmainon Windows (symlink and line-ending related) and are unrelated to this change.isort,pyink,ruff,mypy(strict),scripts/compliance_checks.py,scripts/check_new_py_files.pyandscripts/check_docs.pyall pass.Manual End-to-End (E2E) Tests:
An
LlmAgentwith a tool returning 6,000 order records, run throughInMemoryRunnerwith a scripted model, without and with the plugin:Checklist
Additional context
Open design questions from #8364 (plugin vs.
Appfield; dedicatedload_tool_resulttool vs. range reads inload_artifacts) affect only the later phases. This PR keeps everything inside a plugin with no changes to the flow, runner or session schema.