OpenAI Agents SDK · Chapter 10 of 24

Orchestration in code

By the end of this chapter you can decide an agent workflow in Python instead of leaving it to a model: route a message on a typed output, chain one agent's output into the next, run desks in parallel with asyncio.gather, loop a writer and a reviewer until the reviewer passes, with a limit, and say when code should decide and when a model should. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Several agents
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of OpenAI Agents SDK are free. This one comes with the all labs plan.

Why you need this

In chapters 8 and 9 a model decided the flow: the triage agent chose a desk, the manager chose which desks to ask. That is flexible, and it is a guess every time. Bhanu went through a day of handoffs and found the ones that worried him: a message asking for the Sunday paper in advance went to the academic desk like any timetable question.

Kavita is clear: some decisions are Shikhar's rules, not a model's judgement. A leaked paper is always refused. A student who writes that he wants to give up always reaches a counsellor. Karthik: "Then those decisions go in code. The model does what it is good at, reading a message in Hinglish and saying what it is about; Python does the rest, the same way every time."

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    route.py: route by intent

    Kavita wants the routing to be Shikhar's, not a model's guess. Make a classifier agent for lab-chat-mini with the instructions Classify the message into one of Shikhar's intents. and an output type Intent with one field, intent, a Literal of the eleven intents: admission, fees, refund, schedule, doubt, results, hostel, complaint, praise, off_limits, counsellor.

    Make three desks for lab-chat, with no handoffs: the Fees desk (fee_status and refund_quote), the Hostel desk (find_hostel_beds) and the Academic desk (class_schedule), with chapter 8's names and instructions. In a dictionary, route fees and refund to the Fees desk, hostel to the Hostel desk and schedule to the Academic desk.

    For each message, in this order, classify it, then run its desk on it and print the intent, ->, the desk's name, a colon and a space, then the desk's final output. An intent with no desk runs no agent: print the intent, then -> Bhanu.

    S2053 ki fees kitni baki hai?

    boys hostel me room khali hai?

    mess ka khana bahut kharab hai

  2. Task 2

    parallel_desks.py: two desks at once

    The desks are slow today (lab-chat-slow, about 4 seconds a request, and each desk makes two). Make the Fees desk (fee_status and refund_quote) and the Hostel desk (find_hostel_beds) of task 1 for lab-chat-slow, and run both, at the same time, on the parent's whole message:

    S2053 ka refund kitna milega? aur boys hostel me AC room khali hai?

    Print two lines: Fees desk: and its final output, then Hostel desk: and its final output. One after the other the desks take about 16 seconds; together, about 8.

  3. Task 3

    reviewer.py: a writer and a reviewer, with a limit

    Kavita wants no reply about tests to go out wrong. Make a writer agent for lab-chat with the instructions You write Shikhar Classes' replies to parents. and a reviewer agent for lab-chat-mini with the instructions Grade the reply against the key facts. Pass only if it covers every key fact. and an output type Review with passed (a bool) and missing (a list of strings).

    For each question, with its rule read from /workspace/data/policy.md (the rule's text without its number): Can my son retake the Sunday test he missed? (rule 3.3), then Sunday test ka result kab aata hai? (rule 3.2). Print Q: and the question. Then up to three rounds: the writer drafts a reply to the question; the reviewer gets Answer: and the draft, a blank line, Key facts: and on the next line - and the rule; print Round N: passed or Round N: failed. On a pass print Send: and the draft and stop. On a fail, the next round's writer is a clone of the writer whose instructions add a blank line, Facts: and the missing facts, one per line. After three failed rounds print Send to Bhanu: no reply passed in 3 rounds

  4. Task 4 · interview question

    Interview: code_decides.py, a model's choice or the code's

    Make chapter 8's triage agent ("Triage", RECOMMENDED_PROMPT_PREFIX then You are Shikhar's helpdesk. Hand each message to the right desk.) with handoffs to the three desks of task 1 (each with a handoff_description), and the classifier of task 1.

    For each message, in this order, run the triage agent and print Model decided: and the agent that answered; then classify it and print Code decided: and, from the intent, refused (policy 5.1 to 5.3) for off_limits, counsellor (policy 5.4) for counsellor, and a desk for anything else.

    bhaiya Sunday test ka paper pehle mil sakta hai?

    I feel very low these days and can't study at all

    The interviewer will ask: when should code decide the flow, and when a model? What would you put in code at Shikhar, and why?

What you will learn

  • Routing on a typed output: a classifier agent's intent picks the desk, in a Python dictionary.
  • Chaining: one agent's output is the next agent's input.
  • Parallel desks with asyncio.gather, from chapter 7.
  • A writer and a reviewer in a loop, and why the loop needs a limit.
  • When code beats a model at deciding.

More in Several agents

Build "Orchestration in code" yourself

Shikhar Classes, a Kota coaching institute, answers parents and students on WhatsApp all day. You build its helpdesk assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of OpenAI Agents SDK are free. This one comes with the all labs plan.