Agentic AI Notebook
Phase 11

OpenAI Agents SDK

~3 min read

Concept & How It Works

  • Key points are in the visual diagram above.

Why Does It Exist?

Building agents on raw chat completions requires manual loop management, tool routing, and guardrails. The Agents SDK provides primitives — Agent, Runner, Handoff, Guardrail — that handle orchestration, letting developers focus on agent logic and tool design.

Real-World Analogy

The Agents SDK is like a call center management system — it routes calls between departments (handoffs), enforces scripts (guardrails), and logs every interaction (tracing), while agents focus on solving problems.
Loading diagram...

Visual Workflows

What is OpenAI Agents SDK?

Loading diagram...

Example

Scenario

Customer service system: triage agent classifies query → handoffs to billing agent (has payment tools) or tech agent (has diagnostic tools). Input guardrail blocks PII. Output guardrail ensures professional tone. All steps traced in OpenAI dashboard.

Solution

In OpenAI Agents SDK, apply OpenAI Agents SDK to this scenario: Customer service system: triage agent classifies query → handoffs to billing agent (has payment tools) or tech agent (has diagnostic tools). Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

Practice Task

Do this before moving to the next module — reading alone is not enough.

Open the Code Walkthrough below and run it locally. Change one parameter related to OpenAI Agents SDK (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

Code Walkthrough

Highlighted lines show where OpenAI Agents SDK happens in the code.

OpenAI Agents SDK
1from agents import Agent, Runner, handoff, GuardrailFunctionOutput  # import dependencies2from agents import input_guardrail, output_guardrail  # import dependencies3
4@input_guardrail5async def pii_guardrail(ctx, agent, input):6    if contains_pii(input):7        return GuardrailFunctionOutput(tripwire_triggered=True, output_info="PII detected")  # return the result8    return GuardrailFunctionOutput(tripwire_triggered=False)  # return the result9
10billing_agent = Agent(11    name="Billing Agent",12    instructions="Handle billing inquiries. Use payment tools.",13    tools=[check_balance, process_refund],14)15
16tech_agent = Agent(17    name="Tech Agent",18    instructions="Diagnose technical issues. Use diagnostic tools.",19    tools=[run_diagnostic, check_status],20)21
22triage_agent = Agent(23    name="Triage",24    instructions="Classify the query and hand off to the right specialist.",25    handoffs=[billing_agent, tech_agent],26    input_guardrails=[pii_guardrail],27)28
29result = await Runner.run(triage_agent, "I was charged twice for my subscription")

Commands to Remember

Commands to Remember

  • pip install openai-agents # OpenAI Agents SDK
  • python -c "from agents import Agent, Runner" # verify install

Common Mistakes

  • Too many handoffs — latency and context loss
  • No guardrails on production agents
  • Not using tracing — can't debug multi-agent flows
  • Monolithic agent instead of specialist handoffs
  • Ignoring max_turns — runaway agent loops

Cheat Sheet

Quick recap — the most important points from this module.

Cheat Sheet

quick ref
  • Agent + Runner + Handoff
  • Guardrails = input/output validation
  • Triage → specialist pattern
  • max_turns on Runner
  • Built-in tracing
  • Pydantic structured outputs