OpenAI Agents API & SDK

How autonomous AI agents orchestrate tools, handoffs, and guardrails — explained with animated diagrams

1. The Agent Loop

The core runtime cycle. The model generates a response, and if it includes tool calls or a handoff, the system executes them and loops back. The loop continues until the model produces a final text output.

User Input LLM Call (model generates) Tool call? Handoff? YES Execute Tool / Process Handoff append result to context NO Final Output Input & Output Guardrails
Model inference
Tool execution
Decision point
Guardrails
User I/O

2. SDK Architecture

The Python SDK provides primitives — Agent, Runner, Tools, Handoffs, and Guardrails — that compose into multi-agent systems. The Runner orchestrates everything.

Runner orchestrator Agent • instructions • model config • tools & handoffs Tools • function tools • hosted tools • computer use Handoffs • agent → agent • context preserved • specialization Guardrails • input validation • output filtering • tripwires RunContext shared state & deps

Agent Definition

An agent is a configuration: a model, instructions (system prompt), tools it can invoke, handoffs it can make, and guardrails that constrain it. It's declarative — the Runner brings it to life.

Runner Execution

The Runner manages the agent loop: calls the model, executes tools, processes handoffs, checks guardrails, and manages state. Use Runner.run() for one-shot or Runner.run_streamed() for streaming.

Tools Capabilities

Function tools (your Python functions), hosted tools (web search, file search, code interpreter), and computer-use tools. Each gets auto-converted to the model's tool schema.

Handoffs Delegation

An agent can hand control to another agent. The conversation history transfers, and the new agent takes over the loop. Enables specialization — a triage agent routes to domain experts.

3. Multi-Agent Handoff Sequence

A real-world pattern: a triage agent receives user input, determines intent, and hands off to a specialized agent that uses tools to fulfill the request.

User Triage Agent Refund Agent Tools "I want a refund" classify... HANDOFF lookup_order(id) {order_data} process_refund(id) "Refund processed! $49.99 returned" ROUTE HAND EXECUTE REPLY
User message
Agent response
Handoff transfer
Tool call/response

4. Key Concepts

Primitives

Agents — LLM + instructions + tools. Handoffs — delegate to another agent. Guardrails — validate input/output. Tracing — built-in observability for debugging and eval.

Hosted Tools

Web Search — browse the internet. File Search — RAG over uploaded files. Code Interpreter — sandboxed Python execution. Computer Use — GUI interaction via screenshots.

Streaming

Runner.run_streamed() yields events as they happen: RawResponsesStreamEvent for model tokens, RunItemStreamEvent for tool calls and messages, enabling real-time UIs.

Multi-Agent Patterns

Orchestrator — a central agent delegates. Pipeline — agents in sequence. Parallel — fan out to multiple agents. Evaluator — one agent checks another's output.