OpenAI Agents SDK · Chapter 16 of 24
Dynamic instructions and hooks
By the end of this chapter you can write one set of instructions that fits every desk and every student, make desks as clones of one agent, show a tool only to the students it is meant for, and watch a run from the outside with hooks: a log of what happened, and a meter of what it cost. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Memory
- 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
Shikhar's helpdesk now has several desks and four hundred students. Bhanu wants each desk to say who it is and whom it is talking to, without four hundred copies of the instructions. Some tools make sense only for some students: a room change only for a student who lives in the hostel. And Nidhi wants to know what each conversation costs, and Karthik what the agents did, step by step, without opening every tool to add a print.
Chapter 5 made the instructions a function of the context. This chapter uses the other half of that function, the agent, and two more places where your code runs during a run: a tool's is_enabled, and hooks.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
per_student.py: instructions per desk and per student
Make a Student dataclass (student_id, name, parent_name, hostel_bed) filled from shikhar.get_student(student_id), used as the run's context. Write the instructions as a function of the context and the agent that returns exactly: You are the <agent's name> of Shikhar Classes. You are writing to <parent_name>, parent of <name>. Use the tools.
Make the Fees desk for lab-chat with those instructions and a tool fee_status(ctx) for the student in the context, and the Hostel desk as its clone() with the name Hostel desk and a tool find_hostel_beds(gender) instead (the JSON text of shikhar.find_hostel_beds(gender)). Make only one Agent(...).
Run the Fees desk for S2041 on Is any instalment pending?, then the Hostel desk for S2030 on Is any bed free in the girls hostel?, and print each run's final output on its own line.
- Task 2
hostel_only.py: a tool only some students get
Use task 1's Student and fee_status. Write a tool book_hostel_bed(ctx, bed_id), a room change for the student in the context, returning the JSON text of shikhar.book_hostel_bed(student_id, bed_id), and enable it only when the student has a hostel bed, with is_enabled on the decorator.
Make an agent for lab-chat with the instructions You are Shikhar Classes' helpdesk. Use the tools. and both tools. Run it on I want a room change to GH-202-A. for S2030, then for S2027, and print each as the student id, a colon and a space, then the run's final output.
- Task 3
cost_meter.py: a cost meter
Use task 1's Student and fee_status, and an agent for lab-chat with the instructions You are Shikhar Classes' fees desk. Use the tools. and that tool. Write a subclass of RunHooks that prints Model: N in, N out (the response's input and output tokens) on each model response, and Tool: and the tool's name when a tool starts, and adds up the run's cost at Rs 200 per million input tokens and Rs 800 per million output tokens.
Run the agent for S2041 on Is any instalment pending? with an instance of it as the run's hooks, then print the run's final output and Cost: ₹ followed by the cost with 4 decimals.
- Task 4 · interview question
Interview: hooks_log.py, hooks for a log
Use task 1's Student and fee_status and task 2's book_hostel_bed (here with no is_enabled). Make, for lab-chat: the Fees desk (handoff_description Fees, instalments and refunds.) with fee_status; the Hostel desk (handoff_description Hostel beds and room changes.) with book_hostel_bed; and Triage, with the instructions You are Shikhar Classes' helpdesk. Hand the parent to the right desk. and handoffs to both desks.
Write a RunHooks subclass that prints agent: and the agent's name when an agent starts, handoff: and the two agents' names joined by -> on a handoff, and tool: and the tool's name when a tool starts. Run Triage for S2041 on Is any instalment pending? with it, then print the run's final output.
The interviewer will ask: why hooks, and not a print in every tool and every agent? What can a hook not change?
What you will learn
- Instructions from the context and the agent, shared by every desk.
- clone() per desk: one agent, copied with another name and tools.
- is_enabled: a tool offered only when your function says so.
- RunHooks and AgentHooks: code that runs when an agent starts, a model answers, a tool runs, a handoff happens.
- A log and a cost meter from hooks.
More in Memory
Build "Dynamic instructions and hooks" 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.