OpenAI Agents SDK: configure handoffs and input history scope
Hand a task to specialist agents, add model-supplied metadata with input_type, narrow forwarded history with input_filter, and check boundaries.
Prepare the input and environment · Mechanism and data flow · Workflow map
At a glance
A handoff lets one agent delegate a task to another agent that specializes in a different area. The official example is a customer support app with separate agents for order status, refunds and FAQs. This article relies only on the Handoffs page at a fixed revision. It covers declaring handoffs, letting the model attach metadata, and limiting the history the receiving agent sees. Official facts come from that page. The completion checks, stopping conditions and credential cleanup are editorial recommendations, not official behavior. No product or API was run in this batch.
Runtime and applicability
- Article type
- Engineering practice
- Verified
- Applies to
- OpenAI Agents SDK Documented openai_agents_sdk task and conditions
Execution locations and responsibilities
- Python application definition and caller Define the configuration or request shown here and check its inputs, outputs and failure branches.
When to use
- An existing application or request with an initial agent that should delegate work, and an environment where the SDK imports as
agents. The source code also imports fromagents.extensionsandpydantic, but the source gives no install command, version support or credential setup, so confirm those in the official docs for your environment.
When not to use
input_typeis structured metadata the model attaches to the handoff call. It does not replace the next agent's main input, does not choose the destination, and is not the same asRunContextWrapper.context.
Prepare the input and environment
- An existing application or request with an initial agent that should delegate work, and an environment where the SDK imports as
agents. The source code also imports fromagents.extensionsandpydantic, but the source gives no install command, version support or credential setup, so confirm those in the official docs for your environment. - Editorial recommendation: confirm a working model configuration, then check access, credentials and the asynchronous entry point for the actual model and runtime. The source does not specify this setup. The excerpts define objects without calling
Runner.run; the asynchronouson_handoffneeds the application's existing run entry point. - Know who manages the history before you start. The source says server-managed conversations (
conversation_id,previous_response_idorauto_previous_response_id) do not support handoff input filters, while client-managed history can use an explicit filter to select or redact content. - Decide where the metadata that
on_handoffreceives, such asreason, will be recorded and who may read it. The source only prints it and sets no storage.
Mechanism and data flow
A handoff is a tool the model sees: The source says handoffs are represented to the LLM as tools. For a target agent named Refund Agent, the tool is transfer_to_refund_agent. Every agent has a handoffs parameter that takes either an Agent directly or a Handoff object that customizes the handoff. When you pass a plain Agent and set its handoff_description, that text is appended to the default tool description. It hints when the model should pick the handoff without writing a full handoff() object. The handoff() helper always transfers control to the specific agent you passed. With several possible destinations, the source says to register one handoff per destination and let the model choose. A custom Handoff is for the case where your own handoff code must decide at invocation time which agent to return.
input_type is tool-call arguments only: input_type describes the arguments of the handoff tool call itself. The source says the SDK exposes that schema to the model as the handoff tool's parameters, validates the returned JSON locally, and passes the parsed value to on_handoff. It does not replace the next agent's main input and does not choose a different destination. The receiving agent still sees the conversation history unless you change it with input_filter or nested handoff history settings. It is also separate from RunContextWrapper.context: use input_type for metadata the model decides at handoff time, and the context for application state and dependencies you already have. is_enabled is evaluated while the SDK prepares the available handoffs, before the model returns arguments, so it cannot authorize values inside them. When authorization depends on parsed fields, check at the start of on_handoff before any application side effects, and raise on failure. The SDK continues the transfer only after on_handoff returns successfully. Tool input guardrails apply to function tools, not handoffs.
input_filter changes the forwarded history: When a handoff occurs, the new agent takes over the conversation and by default sees the entire previous history. An input_filter is a function that receives a HandoffInputData and must return a new one. That object holds input_history (the input history before Runner.run(...) started), pre_handoff_items (items generated before the agent turn where the handoff was invoked), new_items (items from the current turn, including the handoff call and handoff output items), optional input_items (items forwarded to the next agent instead of new_items, so model input can be filtered while new_items stays intact for session history), and run_context. Nested handoff history is an opt-in beta, disabled by default, and applies only when neither the handoff's input_filter nor the active RunConfig.handoff_input_filter is set. It changes how the transcript is represented and does not redact: tool-call arguments and outputs can remain in the generated summary.
Run and guardrail boundaries: The source notes that handoffs stay within a single run. Input guardrails still apply only to the first agent in the chain, and output guardrails only to the agent that produces the final output. To check each custom function-tool call inside the workflow, use tool guardrails. If both the handoff and the active RunConfig.handoff_input_filter define a filter, the per-handoff input_filter takes precedence for that handoff. These rules decide which checks can sit at the handoff and which must go elsewhere.
Workflow map
| Stage | What happens |
|---|---|
| A handoff is a tool the model sees | The source says handoffs are represented to the LLM as tools. |
| input_type is tool-call arguments only | input_type describes the arguments of the handoff tool call itself. |
| input_filter changes the forwarded history | When a handoff occurs, the new agent takes over the conversation and by default sees the entire previous history. |
| Run and guardrail boundaries | The source notes that handoffs stay within a single run. |
Complete the task
Declare one handoff per destination: List the specialist agents the initial agent may delegate to, and give each destination its own handoff. The source's basic example defines billing_agent and refund_agent, then creates triage_agent whose handoffs list holds billing_agent itself and handoff(refund_agent). Both forms work. Passing the Agent directly is simplest, and handoff() is for customization. The code only constructs objects. It needs the agents package to be importable, has no Runner.run, no input or output, and makes no model call. It cannot show that the model picks the right destination.
Check tool names and descriptions: The default tool name comes from Handoff.default_tool_name() and has the form transfer_to_<agent_name>; the default description comes from Handoff.default_tool_description(). You can override them with tool_name_override and tool_description_override. If you keep passing plain Agent objects, you can set handoff_description to hint when the model should choose that handoff. It is appended after the default description and exists only when set. The source's override example has an on_handoff callback that prints Handoff called and does not show the tool text the model actually receives. Whether the name and description suit your agents therefore has to be checked in your own environment.
Receive metadata with input_type and on_handoff: When the model should supply a small piece of data at handoff time, define a Pydantic model. The source's EscalationData has one string field, reason. The async callback on_handoff(ctx: RunContextWrapper[None], input_data: EscalationData) prints that reason, and handoff(agent=agent, on_handoff=on_handoff, input_type=EscalationData) produces handoff_obj. The code depends on pydantic and agents. To take effect, handoff_obj would still have to be placed in some agent's handoffs and the run executed, which the excerpt does not do. The source lists fields such as reason, language, priority and summary as suitable, and gives a triage agent handing off to a refund agent with { "reason": "duplicate_charge", "priority": "high" } as an illustration. That is documentation prose, while the code has only reason. Put any authorization check at the start of on_handoff and raise on failure.
Decide what history the receiver sees: If the receiving agent should not inherit the full history, set an input_filter. The source's ready-made example passes handoff_filters.remove_all_tools for an FAQ agent, which the source says removes all tool-related items from the history when FAQ agent is called. The code imports handoff_filters from agents.extensions and likewise only builds objects. remove_all_tools removes structured tool items only. It does not redact tool arguments or results already copied into ordinary messages or nested-history summaries, so use a custom filter when those need removing. If a custom filter calls nest_handoff_history, sanitize input_history, pre_handoff_items and new_items first, because the helper builds nested history from those three fields and ignores any existing input_items. To keep the original new_items for session history, nest first and then sanitize the returned input_history.
Handle server-managed conversations and prompts: Handoff input filters are not supported with conversation_id, previous_response_id or auto_previous_response_id. When the receiving agent must not inherit that server-managed history, the source says to start a separate run with explicitly selected input, and not to reuse the original conversation_id or previous_response_id there. The source also recommends putting handoff information in your agents so the LLM understands handoffs. You can use agents.extensions.handoff_prompt.RECOMMENDED_PROMPT_PREFIX, or call agents.extensions.handoff_prompt.prompt_with_handoff_instructions to add recommended content automatically. In the source's prompt example, the prefix opens the instructions and you fill in the rest of the prompt yourself.
Run the recommended static and semantic checks: This step is an editorial recommendation, not official behavior. For the static check, confirm that each destination has one handoff, that handoff_obj is actually attached to some agent's handoffs, that every input_type field is used by on_handoff, and that a filter returns a new HandoffInputData. For the semantic check, compare your design with the source. Make sure you are not using input_type to replace the main input or pick a destination, not expecting remove_all_tools to redact content, and not expecting is_enabled to authorize argument values. These checks cannot prove the model behaves correctly. Observe real runs yourself in your own environment.
Set stopping conditions and clean up: This step is also an editorial recommendation. The official page offers no API for canceling a handoff, and this article assumes none. If you validate manually, stop on any exception (including one that on_handoff raises when authorization fails), on empty output, or when you decide to quit, and do not record these as success. On every exit path, clean up: remove credentials you set temporarily for the trial, delete saved reason metadata and logs you created, and confirm no local files remain. Clean up only what you created. Do not touch the original session storage, since input_filter changes the input passed on and does not delete stored history.
Worked example
Take the customer support case. The source's basic example gives triage_agent both billing_agent and handoff(refund_agent). To record why a case is escalated, follow the source's EscalationData example: on_handoff receives input_data.reason and prints it. That reason is metadata the model supplies in the tool call, and the receiving agent's main input is not replaced. If an FAQ agent should not see tool items, set handoff_filters.remove_all_tools as its input_filter, as the source does. These are official documentation examples. This batch did not run them or combine them into one application, so there is no observed output.
from agents import Agent, handoff
billing_agent = Agent(name="Billing agent")
refund_agent = Agent(name="Refund agent")
# (1)!
triage_agent = Agent(name="Triage agent", handoffs=[billing_agent, handoff(refund_agent)])
from pydantic import BaseModel
from agents import Agent, handoff, RunContextWrapper
class EscalationData(BaseModel):
reason: str
async def on_handoff(ctx: RunContextWrapper[None], input_data: EscalationData):
print(f"Escalation agent called with reason: {input_data.reason}")
agent = Agent(name="Escalation agent")
handoff_obj = handoff(
agent=agent,
on_handoff=on_handoff,
input_type=EscalationData,
)
from agents import Agent, handoff
from agents.extensions import handoff_filters
agent = Agent(name="FAQ agent")
handoff_obj = handoff(
agent=agent,
input_filter=handoff_filters.remove_all_tools, # (1)!
)
Check the result and failure paths
- Success (editorial recommendation): each destination has its handoff, the tool name and description match what you intended,
on_handoffreceives the value parsed frominput_type, and the history the receiver sees matches your chosen filtering. Base this on real runs in your own environment, because the source excerpts have no run results. - Failure (editorial recommendation): an import fails, the model's arguments fail local validation,
on_handoffraises, a filter does not return a newHandoffInputData, a filter is relied on in a server-managed conversation, or the receiver can still see content that should be excluded. Any of these means the result is not a success. - Empty output (editorial recommendation): if a run yields nothing to verify, such as no
on_handofflog or no final result, treat it as incomplete rather than passed. The source excerpts only define agents and never callRunner.run, so empty output is expected from them, but it also means no run behavior was verified. - User stopping (editorial recommendation): if you stop during manual validation, apply the cleanup items from the last step and note in your record that validation was not finished. The official page has no cancellation API, so do not invent one.
Scope and limits
input_filtercan select or redact forwarded content; nested history itself does not redact it. Neither deletes original storage.remove_all_toolsremoves structured tool items only, leaving arguments and results copied into ordinary messages or summaries. The receiving agent and its model provider receive the forwarded history.- The source says nothing about concrete runtime artifacts, MCP permission guarantees, installation, version support or current pricing, and this article adds none. Nested handoff history is an opt-in beta that is off by default.
This article draws on the Handoffs page at a fixed revision. No SDK was installed and no product or API was run in this batch. The source code was not executed and no run result was observed. The completion checks, stopping conditions and credential cleanup are editorial recommendations, not official behavior. Check your own environment, parameters and permissions against the official docs before use.
Sources
All links are official pages; verification dates are listed per source, and links point to the version checked.
- OpenAI · Handoffs
91fb508