Agent Orchestration: Routines and Handoffs
Exploring orchestration patterns from the official OpenAI cookbook: routines as system prompt + tools, handoffs for transferring control between agents, and how this is implemented in Swarm.
Implement a two-agent system: Triage Agent with a transfer_to_refund tool, Refund Agent with look_up_item and execute_refund tools. Verify that the conversation correctly transfers between agents.
Task grader
Copy and adapt to your context. Text in angle brackets should be replaced.
tools = [look_up_item, execute_refund]
tool_schemas = [function_to_schema(t) for t in tools]
tools_map = {t.__name__: t for t in tools}
def run_full_turn(system_msg, messages):
while True:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": system_msg}] + messages,
tools=tool_schemas,
)
msg = response.choices[0].message
messages.append(msg)
if not msg.tool_calls:
return msg
for tc in msg.tool_calls:
result = execute_tool_call(tc, tools_map)
messages.append({"role": "tool",
"tool_call_id": tc.id, "content": result})