feat: large tool result offload#2162
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
|
Assessment: Comment Well-designed plugin that solves a real problem — proactive tool result externalization is a clear improvement over reactive truncation. The Plugin/Protocol/storage architecture is clean and follows existing SDK patterns ( Review Categories
Solid implementation overall — the architecture is well thought out and the test suite is comprehensive. 🙏 |
…use preview when possible
|
Assessment: Comment The move to token-based thresholds is excellent — it directly addresses mkmeral's earlier feedback and is a significant improvement over character heuristics. The integration with Review Items
The token-based approach is the right design. The private API import is the main item to resolve before merge. |
|
Assessment: Approve No new changes since the previous review. The one open thread ( |
|
nit: Should the PR title reference |
mkmeral
left a comment
There was a problem hiding this comment.
get_encoding comment is the main one. otherwise it looks good to me (if review agent also agrees :p )
|
Assessment: Approve The opt-in retrieval tool is a good design evolution — making it disabled by default with No new issues found. This PR is ready to merge. |
|
Assessment: Approve The self-skip guard refinement is a nice improvement — gating on No new issues. This PR is ready to merge. |
Description
Adds a
ContextOffloaderplugin that proactively intercepts oversized tool results viaAfterToolCallEvent, persists each content block individually to a pluggable storage backend, and replaces the in-context result with a truncated preview plus per-block references. Includes an optional built-in retrieval tool (disabled by default) so the agent can fetch offloaded content on demand, returning each content type in its native format.Problem: When tool outputs exceed the model's context capacity,
SlidingWindowConversationManagerreactively truncates to first/last 200 chars — losing data permanently and wasting a failed API call.Solution: A Plugin (following the
AgentSkillspattern) that operates at tool execution time, before the result enters the conversation. Each content block is stored individually with its content type preserved, enabling type-aware retrieval. Inline guidance in each offloaded result tells the agent to use the preview when possible and to use its available tools to selectively access the data it needs.Token-based thresholds: Uses the agent's
model.count_tokens()for accurate token estimation (tiktoken when available, chars/4 heuristic fallback). The async hook wraps the tool result as a message for counting. Preview slicing uses tiktoken for exact token-level cuts when available, falls back totokens * 4chars.Content type handling:
text/plain, replaced with a previewapplication/json(serialized viajson.dumps), replaced with a previewimage/png), replaced with placeholder + referenceapplication/pdf), replaced with placeholder + referenceStorage backends (required — user must choose one):
InMemoryStorage— no filesystem side effects, content cleared on process exit.clear()method for manual cleanup.FileStorage— persists to disk with.metadata.jsonsidecar for content type tracking across process restartsS3Storage— persists to Amazon S3 (followsS3SessionManagerpatterns), content type preserved via S3 object metadataBuilt-in retrieval tool (opt-in, disabled by default):
retrieve_offloaded_content— agent can fetch offloaded content by referenceinclude_retrieval_tool=True{"json": ...}block, images as{"image": ...}block, documents as{"document": ...}blockretrieve_offloaded_content; always tells the agent to use available toolsUsage:
Related Issues
Closes #1296
Documentation PR
strands-agents/docs#772
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.