# Useful but Little-Known Features of OpenAI Agent SDK
๐ Are massive tool outputs eating up your context window?
ToolOutputTrimmer automatically trims older tool outputs, keeping your context lean and efficient.
๐ Title: Tool Output Trimmer Extension
๐ URL:
๐งฉ Overview
`ToolOutputTrimmer` works as a `CallModelInputFilter`, trimming tool outputs in conversation history using a sliding window approach. Recent turns (default 2) are preserved as-is, while older turns with outputs exceeding `max_output_chars` (default 500) are truncated to just the first `preview_chars` (default 200). It operates non-destructively using shallow copies, leaving the original messages untouched.
๐ How to use it
```python
from agents import RunConfig
from agents.extensions.tool_output_trimmer import ToolOutputTrimmer
# Use with defaults
trimmer = ToolOutputTrimmer()
# Custom configuration
trimmer = ToolOutputTrimmer(
recent_turns=3, # Keep last 3 turns intact
max_output_chars=1000, # Trim outputs over 1000 chars
preview_chars=300, # Keep first 300 chars as preview
trimmable_tools=["search", "fetch_page"], # Only trim specific tools
)
# Apply via RunConfig
config = RunConfig(call_model_input_filter=trimmer)
result = await "question", run_config=config)
```
๐ Building it into production
ใปApply to tools that return large text payloads (search, web fetch, etc.)
ใปUse `trimmable_tools` to selectively target tools while preserving critical outputs
ใปTune `recent_turns` based on how much recent context your agent needs for reasoning
ใปNon-destructive design allows inspecting original messages during debugging
๐ก Use cases
๐ Optimizing context in RAG pipelines with large search results
๐ Managing long web page fetch outputs efficiently
๐ Reducing token consumption in multi-turn conversations
๐ฐ Cutting API costs through effective context window utilization
โ ๏ธ Watch out
Trimming removes detail from older turns, so if your agent needs to reference past tool outputs, set `recent_turns` high enough. When `trimmable_tools` is not specified, all tools are subject to trimming.
โจ Keep your context window lean and your agents sharp with ToolOutputTrimmer.
#
OpenAIAgentSDK# #
AIAgent#