
Customer support is one of the most practical uses of AI agents, and it naturally fits a multi-agent design: a triage agent understands the request and hands off to specialists for billing, technical issues or account questions. The OpenAI Agents SDK makes this pattern simple with a small set of primitives: agents, tools, handoffs, guardrails, sessions and built-in tracing. In this openai agents sdk tutorial, you will build a customer support ai agent system in Python step by step, including openai agents sdk handoffs, input guardrails and agent tracing.
Start with the building blocks.
Core Primitives
Agent: a model configured with a name, instructions and optionally tools, handoffs and output types.
Tool: a Python function the agent can call, defined with a decorator that converts the function signature and docstring into a schema.
Handoff: a way for one agent to transfer the conversation to another, specialized agent.
Guardrail: a check that runs on input or output and can stop execution when something is wrong.
Runner: the component that executes the agent loop until a final output is produced.
Session: memory that keeps conversation history across turns.
Tracing: automatic recording of every model call, tool call, handoff and guardrail, viewable in the OpenAI dashboard.
What You Need
- Python 3.10 or later.
- An OpenAI API key set as an environment variable.
- Install the SDK by running pip install openai-agents.
Step 1: Create the Tools
Create a file named support.py. Define tools as Python functions decorated with the function tool decorator. For this tutorial, use simple mock implementations:
- get_order_status takes an order ID and returns a status, such as shipped with a tracking number.
- get_invoice takes a customer email and returns recent invoice details.
- reset_password_link takes an email and returns a confirmation that a reset link was sent.
- check_service_status returns whether systems are operational.
Write clear docstrings for each function, because they become the tool descriptions the model reads. In production, these would call your real systems.
Step 2: Create Specialist Agents
Create three specialist agents:
Billing agent: instructions explain that it handles invoices, charges and refunds, uses the get_invoice tool, and may offer refunds only according to a stated policy. Give it the get_invoice tool.
Technical agent: handles bugs, outages and how-to questions. Instructions tell it to check service status first and give step-by-step troubleshooting. Give it check_service_status.
Account agent: handles passwords, profile changes and order status. Give it reset_password_link and get_order_status.
Each specialist should have focused instructions and only the tools it needs, following the principle of least privilege.
Step 3: Create the Triage Agent
Create a triage agent whose instructions say: You are the first point of contact for customer support. Determine what the customer needs and hand off to the appropriate specialist: billing for payments and invoices, technical for problems using the product, account for passwords, profiles and orders. Ask a clarifying question if the request is unclear. Do not try to solve specialist problems yourself.
Set its handoffs parameter to the list of the three specialists. The SDK automatically creates handoff tools that the triage agent can call to transfer control.
Optionally, give each specialist a handoff back to triage, so conversations that change topic can be rerouted.
Step 4: Add an Input Guardrail
Agents sdk guardrails protect against misuse. Create an input guardrail that checks whether the user's message is related to customer support. Implement it with a small, fast agent that returns structured output, such as a boolean is_off_topic and a reason.
The guardrail function runs this checker on the input and returns a guardrail output with tripwire triggered set when the message is off topic. Attach the guardrail to the triage agent.
When the tripwire triggers, the runner raises an exception that your application catches to return a polite message, such as I can only help with questions about your account and our product.
Guardrails run in parallel with the agent, so they add minimal latency.
Step 5: Run the Agent
Use the Runner's run method with the triage agent and the user's message. The result contains the final output and information about which agent produced it.
Test with several messages:
I was charged twice this month. The triage agent hands off to billing, which calls get_invoice and responds.
The dashboard will not load. Triage hands off to technical, which checks service status and offers troubleshooting.
Write me a poem about cats. The guardrail triggers.
Visual description: printing the result shows the final response and the name of the last agent, such as Billing agent, confirming the handoff worked.
Step 6: Add Conversation Memory
For multi-turn conversations, use a session. The SDK provides session implementations, including SQLite-based storage, that automatically load and save conversation history. Pass the same session to each run call for a given customer conversation, and the agents remember earlier messages.
Step 7: Review Agent Tracing
Agent tracing is enabled by default. Open the traces section of the OpenAI platform dashboard to see each run.
Visual description: a timeline shows the triage agent's model call, the handoff to the billing agent, the get_invoice tool call with its input and output, and the final response, each with timing information.
Tracing makes it easy to diagnose wrong handoffs, tool errors and slow steps. You can also export traces to other observability platforms through trace processors.
Step 8: Prepare for Production
- Replace mock tools with real API calls, with authentication and error handling.
- Add output guardrails to check responses for policy violations or sensitive data.
- Require human approval for consequential actions such as refunds.
- Use structured outputs for responses that feed other systems.
- Add evaluation sets of real support conversations to test routing accuracy.
- Monitor handoff rates and escalation to human agents.
Troubleshooting Common Issues
Wrong handoffs: make specialist names and handoff descriptions clearer, and add examples of routing decisions to triage instructions.
Specialists answering outside their scope: tighten instructions and add handoffs back to triage.
Guardrail false positives: refine the checker's instructions with examples of valid support messages.
Missing context after handoff: confirm the conversation history passes to specialists, and use input filters only when you intentionally want to trim history.
Authentication errors: verify the API key environment variable is set in the running environment.
Python Multi Agent OpenAI Patterns Beyond Handoffs
Besides handoffs, the SDK supports agents as tools, where an orchestrator calls specialist agents and keeps control. Use handoffs when the specialist should take over the conversation, and agents as tools when the orchestrator needs to combine results from several specialists.
Frequently Asked Questions
Can I use non-OpenAI models? The SDK supports other providers through integrations, though some features work best with OpenAI models.
How does this compare with LangGraph? The Agents SDK is simpler with fewer concepts; LangGraph offers more explicit control and persistence for complex workflows.
Conclusion
In this openai agents sdk tutorial, you built a support system with a triage agent, specialist agents with focused tools, handoffs, an input guardrail, session memory and built-in tracing.
Key takeaways: keep specialists focused, give each only necessary tools, use guardrails for scope and safety, inspect traces to debug routing, and add human approval for consequential actions. Next, connect one real system, such as your order database, and evaluate routing accuracy on real historical tickets.