feat: add ToolOutputTrimmer for smart context management#2468
Open
jkrish wants to merge 3 commits intoopenai:mainfrom
Open
feat: add ToolOutputTrimmer for smart context management#2468jkrish wants to merge 3 commits intoopenai:mainfrom
jkrish wants to merge 3 commits intoopenai:mainfrom
Conversation
Built-in call_model_input_filter that surgically trims large tool outputs from older conversation turns using a sliding window approach, reducing token usage while preserving recent context at full fidelity.
Member
|
Thanks for sharing this idea. I haven't checked in depth yet, but we'll consider this in the next minor version. |
seratch
reviewed
Feb 11, 2026
| @@ -0,0 +1,170 @@ | |||
| """Built-in call_model_input_filter that trims large tool outputs from older turns. | |||
Member
There was a problem hiding this comment.
can you move this to extensions?
Author
There was a problem hiding this comment.
Done — moved to src/agents/extensions/memory/tool_output_trimmer.py with updated imports and tests. Import path is now from agents.extensions.memory import ToolOutputTrimmer.
Member
There was a problem hiding this comment.
I don't think call_model_input_filter is only for memory feature users. So, you can place it just under extensions module.
Author
There was a problem hiding this comment.
Done — moved up to src/agents/extensions/tool_output_trimmer.py. Import path is now:
from agents.extensions import ToolOutputTrimmerMove ToolOutputTrimmer from agents.memory to agents.extensions.memory. Update import paths and exports.
Move ToolOutputTrimmer up one level from extensions/memory/ to extensions/ since call_model_input_filter is not tied to the memory subsystem. - src/agents/extensions/memory/tool_output_trimmer.py → src/agents/extensions/tool_output_trimmer.py - tests/extensions/memory/test_tool_output_trimmer.py → tests/extensions/test_tool_output_trimmer.py - Import path: from agents.extensions import ToolOutputTrimmer
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.
Summary
Adds a built-in
ToolOutputTrimmerclass that implementsCallModelInputFilter— a sliding-window filter that surgically trims large tool outputs from older conversation turns while keeping recent turns at full fidelity.Closes #2467. Related: #177, #1494.
What it does
function_call_outputitems in old turns — preserves user/assistant messages andfunction_callitemscall_idmapping for informative summariesrecent_turns >= 1,max_output_chars >= 1,preview_chars >= 0)Usage
Files changed
src/agents/memory/tool_output_trimmer.pyToolOutputTrimmerdataclass (170 lines)src/agents/memory/__init__.pyToolOutputTrimmersrc/agents/__init__.pyToolOutputTrimmertests/memory/test_tool_output_trimmer.pyTest plan
make format— cleanmake lint— cleanmake mypy— 0 new errorsmake tests— 1888 passed, 4 skipped, 0 failures (no regressions)