# 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#