07. Frameworks & Libraries
OpenAI Agents SDK (Tools, Handoffs, Guardrails, Tracing)
The OpenAI Agents SDK is a lightweight, code-first framework for single agents or multi-agent networks: an Agent is model + instructions + tools; Handoffs transfer ownership to specialists; Guardrails validate inputs/outputs; Tracing records turns, tools, handoffs, and guardrail spans for debug and eval.
What you will learn
- Define Agent, tools, handoffs, guardrails, and tracing
- Design a triage → specialist handoff topology
- Place input vs output guardrails correctly
- Explain what a default trace captures
- Answer interview questions with short, solid answers
Definition
The OpenAI Agents SDK (Python/TypeScript) orchestrates agent loops for you: tool calls across turns, handoffs between specialists, input/output guardrails, optional sessions for history, and built-in tracing. You own tools, storage, and approvals in your server; the SDK owns the run loop.
An Agent is primarily a model plus instructions plus tools — with optional handoffs, guardrails, structured outputs, and lifecycle hooks.
Introduction
This lesson (#8 in Frameworks & Libraries) follows Microsoft AutoGen Fundamentals. You saw message-driven GroupChat; OpenAI Agents SDK emphasizes typed agents, handoff ownership, and production guardrails/tracing.
Next: Google ADK (Sequential, Parallel and Workflow Agents) — another workflow-oriented agent toolkit.
Why OpenAI Agents SDK
- Thin primitives that map cleanly to production agent apps
- Built-in agent loop (multi-turn tools) without hand-rolling
- Handoffs for specialist ownership, not only tool calls
- Guardrails for input/output policy as first-class citizens
- Default tracing for debug, monitoring, and eval loops
Agents and the Runner
- name + instructions - identity and system behavior
- model / model_settings - which LLM and sampling knobs
- tools / mcp_servers - capabilities the agent may call
- handoffs - other agents that can take over the conversation
- input_guardrails / output_guardrails - policy gates
- Runner.run / run_sync / run_streamed - execute the agent loop
Tools
Tools are how agents act: function tools in your runtime, hosted platform tools, or MCP-backed tools. Keep allowlists per agent — billing tools stay off the marketing agent.
- Function tools - typed app code the model can invoke
- Hosted / platform tools - when the platform executes them
- MCP tools - remote or local MCP servers as tool sources
- reset_tool_choice (default true) - helps avoid tool-use loops
- Same Module 05 rule: least privilege + validate arguments
Handoffs
A handoff transfers conversation ownership to a specialist agent (new instructions, model, and tools). The specialist receives history and continues — modular multi-agent without one god agent holding every tool.
- Triage/router agent - classifies intent, then hands off
- Specialists - narrow tools and instructions per domain
- Ownership change - not a silent side call that returns to triage by default
- Maps to Module 06 specialized agents / supervisor routing
- Trace handoff_span shows who took over and when
Guardrails
Guardrails run policy checks on user input (often in parallel with the start of the run) and on final agent output. Fail closed: trip a guardrail to stop unsafe or off-policy work before side effects.
- input_guardrails - relevance, jailbreak, PII, abuse screens
- output_guardrails - schema, toxicity, policy, brand checks
- Can pair with human review for high-risk actions
- Guardrail trips appear as guardrail_span in traces
- Complement, do not replace, server-side authZ on tools
Tracing
- Enabled by default for server-side SDK runs
- Trace = one workflow; spans = timed operations inside it
- Default spans: agent, generation, function/tool, handoff, guardrail
- Use Traces dashboard to debug prompts, tools, and routing
- group_id can link multi-turn conversation traces
- Feed traces into evals — not just dashboards
Map to earlier modules
- Tools ≈ Module 05 tool-use with allowlists
- Handoffs ≈ specialized agents + routing / supervisor handoff
- Guardrails ≈ safety + validation before/after the loop
- Tracing ≈ observability required for production agents
- Sessions ≈ conversation memory across runs
- CrewAI/AutoGen/LangGraph remain options when their metaphors fit better
Failure modes
- God agent with every tool and no handoffs
- Handoffs without clear specialist boundaries
- Guardrails only on output — toxic input already burns tokens
- Tracing disabled in prod — cannot debug failures
- Trusting the model for authZ instead of server checks on tools
Program / implementation notes
Define agents as code/config, keep secrets server-side, wrap Runner behind your API with budgets, and review traces before widening tool permissions.
- Prefer structured output_type when downstream systems parse results
- Next: Google ADK (Sequential, Parallel and Workflow Agents)
Advantages and limitations
- Advantage: first-class handoffs, guardrails, and tracing
- Advantage: thin API that fits typed Python/TS services
- Advantage: strong fit for OpenAI model + platform tooling
- Limitation: less graph-native than LangGraph for complex durable flows
- Limitation: multi-agent beyond handoffs may need your own orchestration
- Limitation: you still own auth, storage, evals, and cost caps
Key points
- Agent = model + instructions + tools (+ optional handoffs/guardrails).
- Runner runs the multi-turn tool loop for you.
- Handoffs transfer ownership to a specialist with history.
- Input and output guardrails enforce policy; fail closed.
- Tracing records agent, generation, tool, handoff, and guardrail spans.
- Least-privilege tools and server-side authZ still apply.
FAQs
Handoff vs tool call?
A tool call returns a result to the current agent. A handoff transfers conversation ownership to another agent with its own instructions and tools.
Do I need custom tracing code?
Usually no for basics — tracing is on by default. Add custom spans for your business steps; disable or tune only when you must.
Where do MCP tools fit?
Agents can attach mcp_servers so MCP tools appear alongside function tools — still allowlist and validate.
Agents SDK vs Responses API alone?
Responses API is the model/transport surface. Agents SDK adds the orchestration loop: tools, handoffs, guardrails, sessions, and traces.
Interview questions
- What are the core OpenAI Agents SDK primitives?
Answer Agent (model + instructions + tools), handoffs to specialists, guardrails on input/output, sessions for history, and built-in tracing — executed via Runner.
- What is a handoff?
Answer Delegating the conversation to another agent that receives history and takes over with its own instructions, model, and tools.
- Input vs output guardrails?
Answer Input guardrails screen the user request early; output guardrails validate the final agent output before you accept or act on it.
- What does default tracing capture?
Answer The workflow trace plus spans for agents, model generations, tool/function calls, handoffs, and guardrails (and audio spans when used).
- How do tools stay safe?
Answer Per-agent allowlists, argument validation, server-side authZ, and budgets — models propose calls; your runtime enforces policy.
- When prefer LangGraph instead?
Answer When you need explicit durable graphs, complex branching, and checkpointed HITL mid-flow beyond handoff-style specialist routing.
Related topics
Practice task
Complete each numbered practice step in a notebook or doc before opening the next lesson.
- Define a triage agent and 2 specialists (name, instructions one-liner, tool allowlist each).
- List 2 input guardrails and 2 output guardrails for a support workflow.
- Draw the handoff path for “I need a refund” from user → triage → specialist.
- Name 5 span types you would expect in a trace for that refund run.
- Answer interview questions 1, 2, and 4 in two sentences each.