OpenAI Agents SDK · Chapter 5 of 24
Context
By the end of this chapter you can give a run your own object as its context, read it in a tool through RunContextWrapper (so the model never has to ask for a student ID), write instructions as a function of the context, let a tool write to the context and read it after the run, prove with lab requests that the context is never sent to the model, and say why it beats a global variable. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Your first 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 chapter 4 every message carried a student ID: "S2041 ki kist kitni late hai?". Real messages do not. A student signed in to Shikhar's app writes "Meri fees kitni baki hai?", and the app already knows who they are. Give the fee tool a student_id parameter and the model has to fill it, so it asks the student for an ID they already gave the app, or worse, writes one from somewhere else in the conversation.
Bhanu puts it plainly: the student is not something the model should decide. Your program knows it. This chapter gives your program a place to keep it for the run: the context.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
my_fees.py: the student as context
Write a dataclass Student with two str fields, student_id and name, and make one for S2041 from shikhar.get_student("S2041").
Write a tool fee_status whose only parameter is the run's context wrapper, ctx: RunContextWrapper[Student]. It has a docstring and returns json.dumps of shikhar.fee_status for the context's student_id. Give it to an agent for lab-chat with the instructions You are Shikhar Classes' helpdesk assistant. and run it on the student's message exactly, with the student as the run's context:
Meri fees kitni baki hai? Late fee lagi kya?
Print the run's final output. The tool must not have a student_id parameter.
- Task 2
greeting.py: instructions from the context
Write a dataclass Student with three str fields: student_id, name and language. Write a function of the context wrapper and the agent that returns the instructions, with the context's name and language in them: You are Shikhar Classes' helpdesk assistant. The student is (the name). Greet them by name and answer in (the language).
Make one agent for lab-chat with that function as its instructions. Run it for S2030 and then S2041, each Student made from shikhar.get_student, on the question Is there a class on Sunday? and print one line per run: the student's name, a colon and a space, then the final output.
- Task 3
doubt_booking.py: a tool that writes to the context
Write a dataclass Student with a student_id (a str) and a bookings list, empty by default. Write a tool book_doubt_session that takes the context wrapper, a subject and a day (both str), with a docstring that says what it does. It books with shikhar.book_doubt_session for the context's student_id and, when the answer's ok is true, appends its booking_id to the context's bookings; it returns the answer as json.dumps.
Give it to an agent for lab-chat with the instructions You book doubt sessions for Shikhar Classes' students. and run it for Student("S2061") on: Kal chemistry ka doubt session chahiye
Print the final output, then Booked: and the booking ids from your Student's bookings, joined with a comma and a space.
- Task 4 · interview question
Interview: two_students.py, why not a global?
Write a dataclass Student with a student_id (a str) and a tool refund_quote whose only parameter is the context wrapper; it has a docstring and returns json.dumps of shikhar.refund_quote for the context's student_id.
Make one agent for lab-chat with the instructions You are Shikhar Classes' refund desk. and the tool. Run it for S2053 and then S2061, each with its own Student as the context, on the same message: Agar abhi chhod de to refund kitna milega?
Print one line per run: the student ID, a colon and a space, then the final output.
The interviewer will ask why the student goes in the context and not in a global variable the tool reads.
What you will learn
- context= on a run: your own object, a dataclass of the student.
- RunContextWrapper and ctx.context in a tool.
- Dynamic instructions: a function of the context.
- A tool that writes to the context, read after the run.
- What is never sent: the context, proved in the request log.
More in Your first agents
Build "Context" 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.