# Practical and Useful Patterns for OpenAI Agent SDK
🌍 Is your handoff target drowning in irrelevant tool call history from previous agents?
Input filters and recommended prompts ensure each agent receives exactly the context it needs.
📌 Title: Handoffs – Input filters / Recommended prompts
🔗 URL:
🧩 Overview
Handoff input filters transform the conversation history passed to the target agent. `remove_all_tools` strips all tool call history, `nest_handoff_history` (beta) compresses multi-handoff history into a single summary message. Additionally, `RECOMMENDED_PROMPT_PREFIX` helps the LLM correctly understand the handoff mechanism and make better routing decisions.
🛠 Usage
Define `Agent(name="faq", instructions="Answer frequently asked questions. Keep responses simple.")` and `Agent(name="specialist", instructions="Answer advanced technical questions.")`. The triage agent is `Agent(name="triage", instructions=RECOMMENDED_PROMPT_PREFIX + "\nClassify customer inquiries and route to the appropriate specialist.", handoffs=[...])`, where the FAQ handoff uses `Handoff(agent=faq_agent, input_filter=handoff_filters.remove_all_tools, handoff_description="General FAQ questions")` to strip tool history, and the specialist handoff uses `Handoff(agent=specialist_agent, input_filter=handoff_filters.nest_handoff_history, handoff_description="Advanced technical questions")` to compress history. Import `RECOMMENDED_PROMPT_PREFIX` from `agents.extensions.handoff_prompt`.
🏗 Practical Patterns
**Clean Handoffs with remove_all_tools**
When a triage agent uses multiple tools (DB queries, API calls) before handing off to an FAQ agent, that tool history is noise for the FAQ agent. `remove_all_tools` strips all tool-related messages, giving the target agent a clean conversation history. This also reduces token consumption.
**History Compression with nest_handoff_history (beta)**
In multi-hop handoffs (A to B to C), history accumulates rapidly. `nest_handoff_history` compresses prior handoff history into a single summary message, using the context window much more efficiently for the final agent.
**RECOMMENDED_PROMPT_PREFIX for Accurate Routing**
Including `prompt_with_handoff_instructions()` or `RECOMMENDED_PROMPT_PREFIX` in an agent's `instructions` helps the LLM understand available handoff targets and when to use them. Without this, the model may try to answer questions itself instead of delegating to the right specialist.
**Custom Filters**
When built-in filters aren't enough, create custom ones: keep only the last 5 messages, remove messages containing sensitive data, or preserve only specific tool call results. Tailor the handoff input to exactly what the target agent needs.
💡 Use Cases
🧹 Strip tool history before handing off to a simple FAQ agent
📦 Compress multi-hop handoff history to optimize token usage
🤖 Improve handoff routing accuracy with RECOMMENDED_PROMPT_PREFIX
🔒 Filter out messages containing sensitive information before handoff
⚠️ Caveats
- `remove_all_tools` removes ALL tool-related messages. Don't use it if the target agent needs to reference tool results.
- `nest_handoff_history` is a beta feature. Compression behavior may change in future releases.
- When using `RECOMMENDED_PROMPT_PREFIX`, prepend it to existing `instructions`. Appending at the end may reduce its effectiveness.
- Custom filters should only add or remove messages — not reorder them or change roles, which can cause unpredictable model behavior.
✨ Optimize handoff inputs with filters so each agent receives exactly the context it needs to perform at its best!
#
OpenAIAgentSDK# #
AIAgent#