feat(context-offloader): let a tool call opt out of offloading via invocation_state - #4327
Closed
strandly-the-agent wants to merge 4 commits into
Closed
strandly-the-agent wants to merge 4 commits into
strandly-the-agent wants to merge 4 commits into
Conversation
Tools that are invoked directly by other code (not by the model) need the full result, not a preview. A truthy SKIP_OFFLOAD_KEY in the call's invocation_state exempts that result from offloading.
strandly-the-agent
had a problem deploying
to
manual-approval
September 14, 2026 11:26 — with
GitHub Actions
Error
9 tasks
strandly-the-agent
requested a deployment
to
manual-approval
September 14, 2026 11:47 — with
GitHub Actions
Waiting
mkmeral
approved these changes
Sep 14, 2026
Contributor
Author
|
Closing per |
This branch is waiting to be deployed
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.
Description
ContextOffloaderhooksAfterToolCallEvent, so it offloads every oversized tool result — including results that are never meant for the model. When a tool invokes other tools through the executor and consumes their output in code (a programmatic/direct tool-calling pattern), that code receives a[Offloaded: …]preview instead of the data:json.loadson a preview fails, file contents arrive truncated, and the failure is silent.This adds
SKIP_CONTEXT_OFFLOAD_KEY("strands:skip_context_offload"), exported fromstrands.vended_plugins.context_offloader. When it is truthy in a tool call'sinvocation_state,_handle_tool_resultreturns early and the result passes through untouched. The key rides oninvocation_statebecause that is whatToolExecutor._streamalready threads to the hook, so a caller can opt out without a new hook or a plugin option; the delegate-tool skip a few lines above is the same shape.The key applies to every tool call sharing that
invocation_state(the agent loop passes one dict to all tool calls of an invocation), so the docstring tells callers to pass a copy to scope it to one call. A direct call takes it as a kwarg:Related Issues
None. TypeScript parity is in a separate PR (same key, camelCased to
strands:skipContextOffloadper thestrands:model_routing/strands:modelRoutingprecedent).Documentation PR
Included: a short Opting a call out section in
site/…/plugins/context-offloader.mdx.Type of Change
New feature
Testing
ruff check/ruff format --checkclean on the touched files;pytest tests/strands/vended_plugins/context_offloader/test_plugin.py→ 91 passed (2 new: key set → result and storage untouched; key falsy → offloaded as before).mypyon the package reports the same two pre-existing@hookoverload errors with and without the change;complexipyfor_handle_tool_resultgoes 57 → 58 (already over threshold before this PR — the guard is one extra early return).Exercised end to end: a real
AgentwithContextOffloader(max_result_tokens=50)and a tool that calls another tool viaToolExecutor._streamwith{**invocation_state, SKIP_CONTEXT_OFFLOAD_KEY: True}— the inner code received the full 4,000-char payload, and the outer tool's own result was still offloaded.One independent review pass; its findings (state the invocation-wide scope, show a public route, whole-object test assertions) are folded in.
I ran the relevant checks (
hatch run prepareequivalents run individually: ruff, mypy, pytest)Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Opened by
strandly-the-agentat the request ofmkmeral.