Lesson 07 OpenAI Agents SDK Intro
Theme:
Lesson 07 · Slide 1

Lesson 07 — OpenAI Agents SDK Intro

Three primitives, one big idea: agents that hand off work to other agents.

Agents Module 4 OpenAI Agents SDK Handoffs

Lesson 07 · Slide 2

Recap of where we've been

Same underlying loop — new composition primitive on top of it.

Lesson 07 · Slide 3

What the OpenAI Agents SDK actually is

Key clarification

It's the openai-agents PyPI package, separate from the base openai client.

pip install openai-agents gives you the SDK; you still install openai underneath for the model HTTP layer.

Imports come from the top-level agents namespace:

from agents import Agent, Runner, handoff, function_tool

Lesson 07 · Slide 4

The three headline primitives

Agent

A model + instructions + tools + a list of agents it can hand off to.

handoff(target_agent, ...)

A typed reference that lets one agent transfer control to another mid-run. The handoff appears to the LLM as if it were a tool call.

Guardrails

@input_guardrail and @output_guardrail decorators that wrap an agent with cheap pre/post checks. They short-circuit a run if their GuardrailFunctionOutput.tripwire_triggered returns true.

Lesson 07 · Slide 5

A second supporting set

Runner.run(agent, input)

Async entry point that drives the agent loop. There's also Runner.run_sync(...) for non-async callers.

@function_tool

The SDK's tool decorator. Same idea as Claude SDK's @tool: reads type hints + docstring, builds the JSON Schema for you.

RunResult.final_output

Final answer object. With output_type= set to a Pydantic model the SDK returns a typed instance, not a string.

Lesson 07 · Slide 6

Hello-world agent in 10 lines

from agents import Agent, Runner, function_tool

@function_tool
def get_weather(city: str) -> str:
    """Get current weather for a city."""
    return f"{city}: 4°C, light rain"

agent = Agent(
    name="WeatherBot",
    instructions="You answer weather questions using the get_weather tool.",
    model="gpt-4o-mini",
    tools=[get_weather],
)
result = Runner.run_sync(agent, "What's the weather in Kyiv?")
print(result.final_output)

Shape is recognisably the same as Claude SDK's hello-world — just with Runner as the driver and function_tool instead of tool.

Lesson 07 · Slide 7

What's new compared to Claude SDK

ConceptClaude SDKOpenAI Agents SDK
Single-agent loopAgent.run()Runner.run(agent, ...)
Multi-agent compose(you wire it)handoffs=[other_agent]
Structured final answerfinal-answer tool patternoutput_type=Pydantic
Pre/post checks(you wire it)@input_guardrail / @output_guardrail
ModelClaude onlyany provider via LiteLLM adapter

The bold rows justify reaching for this SDK over Claude's: handoffs and guardrails are first-class.

Lesson 07 · Slide 8

What a handoff looks like to the LLM

Notes: The handoff is invisible to the user but visible in the trace — result.last_agent.name tells you who ended up handling the request.
Lesson 07 · Slide 9

A two-line handoff

billing = Agent(name="Billing", instructions="Handle billing only.", model="gpt-4o-mini")
triage  = Agent(name="Triage",  instructions="Route to the right specialist.",
                model="gpt-4o-mini", handoffs=[billing])
result = Runner.run_sync(triage, "My invoice is wrong.")
print(result.final_output)
print(result.last_agent.name)  # 'Billing'

last_agent tells you who ended up handling the request — the SDK swapped to Billing mid-run without you wiring a second call.

Lesson 07 · Slide 10

Guardrails in 8 lines

from agents import input_guardrail, GuardrailFunctionOutput

@input_guardrail
async def block_pii(ctx, agent, user_input):
    bad = any(tok in user_input for tok in ("SSN", "credit card"))
    return GuardrailFunctionOutput(
        output_info={"pii_detected": bad},
        tripwire_triggered=bad,
    )

agent = Agent(name="...", instructions="...", input_guardrails=[block_pii])

The SDK runs block_pii before the model sees the input; tripwire short-circuits the run with an InputGuardrailTripwireTriggered exception.

Lesson 07 · Slide 11

When to pick OpenAI Agents SDK

Pick it when

  • You have multiple specialist agents that share a conversation (triage / handoff).
  • You want typed structured output by default (output_type=).
  • You want pre/post guardrails without writing them yourself.
  • You want one client that can drive non-OpenAI models.

Sweet spot.

Pick something else when

  • One agent + many tools — Claude SDK.
  • Explicit DAG control flow — LangGraph.
  • A team of agents collaborating in parallel rather than passing control — CrewAI.
Lesson 07 · Slide 12

What it does not give you for free

Notes: Treat it as an OpenAI-first SDK with experimental multi-provider support — not a true provider-agnostic abstraction layer.
Lesson 07 · Slide 13

Recap & what's next

Coming up: Lesson 08 — build a customer-support triage that hands off to billing, technical, and account specialists in ~120 lines.
1 / ?