Skip to content

feat(plugins): add ToolResultContextPlugin to offload oversized tool results to artifacts - #8512

Open
karthik-0306 wants to merge 1 commit into
google:mainfrom
karthik-0306:feat/8364-tool-result-offload
Open

karthik-0306 wants to merge 1 commit into
google:mainfrom
karthik-0306:feat/8364-tool-result-offload

Conversation

@karthik-0306

Copy link
Copy Markdown

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 with ToolResultContextConfig / OffloadPolicy, all exported from google.adk.plugins.

  • In 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.
  • The function response is replaced by a stub: status, result_id (the artifact name), original_tokens, a head/tail preview, and a message for the model. The call id and name are kept, so call/response pairing is unchanged; only the payload is replaced.
  • The new artifact is recorded in the event's actions.artifact_delta, like an artifact a tool saves itself.
  • Per-tool limits (per_tool_max_result_tokens), exclude_tools, and offload_errors (error results stay inline by default).
  • Never touched: client function responses (adk_request_credential / confirmation / input), responses with will_continue, results the caller posts back for long-running tools, and partial events.
  • No artifact service: the result is truncated to its preview (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_callback rather than after_tool_callback: a plugin that returns a value from after_tool_callback short-circuits the agent's own after_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 in docs/guides/README.md.

Follow-ups planned in #8364: a load_tool_result tool for paging through offloaded results, cache-aware clearing of stale results in the outgoing request, and telemetry + a sample.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

New tests in tests/unittests/plugins/test_tool_result_context_plugin.py and tests/unittests/plugins/test_tool_result_context_plugin_config.py:

  • End-to-end through the runner: the next model request carries the stub; the artifact holds the full result; the persisted session event holds the stub and its artifact_delta; small results are untouched; agent after_tool_callbacks still receive the complete result; parallel calls go to separate artifacts.
  • Policy rules: size estimate, head/tail preview, preview_tokens=0, per-tool limits, exclusions, client function responses, error results (default and offload_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.
  • Config validation: limits, preview_tokens smaller than every limit, extra='forbid'.
$ pytest tests/unittests/plugins/test_tool_result_context_plugin.py tests/unittests/plugins/test_tool_result_context_plugin_config.py tests/unittests/plugins/test_plugins_package.py
62 passed

The tests/unittests/plugins, tests/unittests/apps, tests/unittests/test_runners.py and tests/unittests/test_import_loading.py suites also pass, apart from BigQuery analytics tests that fail identically on a clean main on 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.py and scripts/check_docs.py all pass.

Manual End-to-End (E2E) Tests:

An LlmAgent with a tool returning 6,000 order records, run through InMemoryRunner with a scripted model, without and with the plugin:

--- plugin=off
second model request: ~80317 tokens
stored function response keys: ['orders']
--- plugin=on
second model request: ~720 tokens
stored function response keys: ['message', 'original_tokens', 'preview', 'result_id', 'status']
status=offloaded original_tokens=89263
artifact tool_result_search_orders_adk-4f859969-bd0e-41f4-a3b6-8ca18f101240.json: 6000 orders recovered

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Open design questions from #8364 (plugin vs. App field; dedicated load_tool_result tool vs. range reads in load_artifacts) affect only the later phases. This PR keeps everything inside a plugin with no changes to the flow, runner or session schema.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant