OpenAI Agents SDK · Chapter 12 of 24

A human's approval

By the end of this chapter you can make a tool that never runs until a person says yes, read what a stopped run is waiting for, approve or reject it and carry on from where the run stopped, ask for approval only when Shikhar's rule says so, and save a stopped run to a file so it can carry on tomorrow, in another program. 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

Nidhi runs Shikhar's fees and refunds. Policy rule 2.4 is hers: "A refund above ₹50,000 is approved by the finance head before it is paid." A parent writes "My son is leaving, please process the refund", the fees desk works out ₹87,400, and the money must not move until Nidhi has looked at it.

What you know so far does not fit. A tool runs the moment the model asks for it (chapter 4). A guardrail (chapter 11) can stop a message or a call, but it decides on its own, in code, there and then; it cannot wait for a person who is in a meeting, or asleep. Telling the agent "never refund above ₹50,000 without approval" in its instructions is a request, not a rule: the model may not follow it, and nothing stops the tool if it does not. What Nidhi needs is a run that stops before the tool, waits for her, and then carries on.

What you build: 4 tasks

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

  1. Task 1

    approval.py: a refund that waits for approval

    Write a tool request_refund(student_id, reason), with a docstring saying it asks for a student's refund, that returns the JSON text of shikhar.request_refund(student_id, reason, approved_by="Nidhi"). The tool needs approval: the SDK stops the run when the model asks for it, and the tool does not run.

    Make an agent for lab-chat with the instructions You are Shikhar Classes' fees desk. Use the tools. and that tool, run it on the parent's message: My son S2061 is leaving Shikhar. Please process the refund.

    Print two lines: Waiting for approval: and the waiting call's tool name, then the call's arguments, the JSON text the model sent.

  2. Task 2

    decide.py: approve and reject

    Use task 1's tool and agent. For each message, run the agent, record Nidhi's decision for the waiting call on the stopped run's state, and run the state to carry on, with no new message.

    My son S2061 is leaving Shikhar. Please process the refund.: approve it. My son S2052 is leaving Shikhar. Please process the refund.: reject it with the message Nidhi rejected this refund: please send the fee receipts first.

    Print Approved: and the first resumed run's final output, then Rejected: and the second resumed run's final output.

  3. Task 3

    above_limit.py: approval only above Rs 50,000

    Use task 1's tool and agent, but the tool decides for itself: give needs_approval an async function of (ctx, params, call_id) that is True only when shikhar.refund_quote(student_id)["total"] is above 50000, with the student id from the call's arguments.

    Run the agent on My son S2053 is leaving Shikhar. Please process the refund. and print Done: and its final output. Then run it on My son S2061 is leaving Shikhar. Please process the refund. and print Waiting for approval: and the waiting call's tool name.

  4. Task 4 · interview question

    Interview: overnight.py, why the run must stop

    Use task 1's tool and agent. The program takes one word on the command line. With start, it runs the agent on My daughter S2052 is leaving Shikhar. Please process the refund., saves the stopped run's state as text to pending.json in this folder, and prints Saved: waiting for approval of and the waiting tool's name.

    With approve, it loads the state from pending.json, approves every waiting call, runs the state and prints the resumed run's final output. The check runs the two as separate programs. Loading a state is async, so the whole program is async, with no run_sync in it.

    The interviewer will ask: why not wait with input() inside the tool? What is in pending.json, and what is not?

What you will learn

  • needs_approval: a tool that does not run until someone approves it.
  • Interruptions: what a stopped run is waiting for.
  • The run's state: approve or reject, then run the state to carry on.
  • A rule for approval: a function that decides, call by call.
  • Saving a state to a file and loading it in another program.

More in Several agents

Build "A human's approval" 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.