Agent Orchestration: Routines and Handoffs
Exploring orchestration patterns from the OpenAI cookbook: routines as instructions plus tools, handoffs for transferring control between agents, a hand-written loop on the Responses API and the production version in the OpenAI Agents SDK.
Routines and Agents
A routine is a set of steps: natural-language instructions plus the tools needed to execute them. LLMs handle conditional branches inside routines robustly without a rigid state machine.
OpenAI described routines and handoffs in the cookbook article "Orchestrating Agents: Routines and Handoffs". That article is now marked as archived. The ideas still hold, and the production implementation lives in the OpenAI Agents SDK (see the last section). The code below uses the Responses API, which OpenAI recommends for all new projects.
from openai import OpenAI
import json
client = OpenAI()
instructions = (
"You are a customer support agent for ACME Inc. "
"Follow the following routine with the user:\n"
"1. Ask probing questions to understand the user's problem.\n"
"2. Propose a fix (make one up).\n"
"3. ONLY if not satisfied, offer a refund.\n"
"4. If accepted, search for the ID and then execute refund."
)
def look_up_item(search_query):
"""Use to find item ID."""
return "item_132612938"
def execute_refund(item_id, reason="not provided"):
"""Refund an item by its ID."""
print(f"Refunding item {item_id}, reason: {reason}")
return "success"
The rest of this lesson opens after a free sign-up
Sign up with email or Google: the first two lessons of every course stay open for good, and the whole catalog is open for 7 days. No card needed.


