OpenAI Agents SDK · Chapter 23 of 24

Capstone

By the end of this chapter you have built Shikhar's helpdesk in one file: a triage agent hands each message to the right desk, every tool acts for the student who is writing and nobody else, the messages policy section 5 forbids are refused before any desk sees them, a refund above ₹50,000 waits for Nidhi, each student keeps a conversation of their own, the helpdesk keeps answering when the main model is down, and it comes with tests that need no model. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 5 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

Kavita has set the date: from the first of November the assistant answers Shikhar's students and parents on WhatsApp, at any hour. Bhanu will read every conversation for the first week. Nidhi will approve every large refund it asks for, often the next morning.

You have built each piece on its own, one chapter at a time. A real helpdesk is all of them at once, and the hard part is where they meet: which student a tool acts for, where a student's conversation lives, what waits for Nidhi and who resumes it, what happens when the model is down, and how you know it still works after next month's change. This chapter puts them together in helpdesk.py.

What you build: 5 tasks

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

  1. Task 1

    helpdesk.py: triage and desks, tools with context

    Make a dataclass Student (student_id, name, batch_id), every run's context, and three desks for the main model, named and described exactly: Fees desk (Fees, instalments, late fees and refunds.) with fee_status, refund_quote and request_refund(reason); Academic desk (Classes and the timetable, test results and doubt sessions.) with class_schedule(day), test_results and book_doubt_session(subject); Hostel desk (Hostel beds, rooms and meals.) with find_hostel_beds(gender, ac). Every tool returns json.dumps of shikhar's function of the same name, for the student in the context: no tool takes a student_id. An agent named Triage, named triage in the file, hands off to the three desks.

    async def ask(student_id, message) runs the triage on the message with that student as the context and returns the final output. Importing helpdesk.py must call no model. The check asks about an overdue instalment, a girls' AC room and a physics doubt session.

  2. Task 2

    helpdesk.py: the policy-5 guardrail

    Make guard, an agent for lab-chat-mini whose output type has off_limits (bool), needs_counsellor (bool) and reason (str), and an input guardrail on the triage that runs it on the message before the triage runs (not in parallel), tripping when either flag is true. When it trips, ask returns the module's COUNSELLOR text for a counsellor message and its REFUSAL text otherwise, both exactly as in the starter.

    The check sends a test-paper request, another student's rank and a message of distress, then a doubt booking that must go through.

  3. Task 3

    helpdesk.py: refunds that wait for Nidhi

    request_refund needs approval when refund_quote's total for the student in the context is above 50,000; it runs shikhar.request_refund(student_id, reason, approved_by="Nidhi"). While a run waits, ask returns Waiting for approval: and the waiting tools' names (Waiting for approval: request_refund), and keeps that student's waiting run.

    async def decide(student_id, approved) resumes that student's waiting run, approving or rejecting each waiting call, and returns the final output; it raises ValueError when nothing of that student's waits. The check asks for refunds for S2061 (approved), S2053 (no approval needed) and S2052 (rejected).

  4. Task 4

    helpdesk.py: a session per student, and a fallback

    Every run, in ask and in decide, uses that student's own in-memory SQLiteSession, kept for the life of the program. The triage and the desks use the model named by the environment variable SHIKHAR_MODEL (lab-chat when it is not set); when a run fails with openai.APIStatusError, run it again with RunConfig(model="lab-chat-mini"). A failed run has already put the message in the session: take it out before running again, so the model never reads it twice.

    The check asks S2041 two questions, then S2053 one; then loads your file with SHIKHAR_MODEL=lab-chat-down and asks S2041 two questions again.

  5. Task 5

    test_helpdesk.py: tests for the refund approval

    Write pytest tests for ask and decide that give the triage, each of its handoffs and the guard a scripted Model (chapter 20), so they send no request to the lab model. The check runs them on your helpdesk.py and on another correct one (both must pass), then on three copies with one careless change each, each of which must make a test fail: a refund is made at once, without waiting for approval; a rejected refund is made anyway; only the last waiting student is kept, so a decision lands on whoever waited last.

What you will learn

  • Triage and desks (chapters 4, 5 and 8): handoffs to three desks whose tools read the student from the context.
  • The policy-5 guardrail (chapter 11): a guard agent on a small model, before the triage runs.
  • Refund approval (chapter 12): a run that stops, and a decision that resumes the right one.
  • A session per student and a fallback (chapters 13 and 19).
  • Tests with a scripted model (chapter 20) for the refund approval.

More in Production

Build "Capstone" 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.