OpenAI Agents SDK · Chapter 13 of 24
Sessions
By the end of this chapter you can say why an agent forgets the last message, keep a conversation in a session so the next run carries it, read what the session added to the request, keep one session per parent, take a wrong turn out of a session, and keep sessions in a file that outlives the program. 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
Parents do not write one perfect message. Bhanu sees it all day on the WhatsApp line: "Is there a free bed in the hostel?", the helpdesk asks "boys' or girls'?", and the parent answers "For my daughter." Every run you have written so far starts from nothing. The second message reaches the agent alone, and "For my daughter." means nothing without the question before it:
for q in ["Is there a free bed in the hostel?", "For my daughter."]:
result = await Runner.run(agent, q)
print(result.final_output)
Happy to help. Could you tell me whether it is the boys' or the girls' hostel?
Thanks for your message. How can Shikhar Classes help you today?
The parent answered the question, and the helpdesk greeted them as a stranger. Kavita has heard about this from parents: "it keeps asking me the same thing".
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
two_turns.py: a two-turn chat
Write a tool fee_status(student_id), with a docstring saying what it gives, that returns the JSON text of shikhar.fee_status(student_id), and an agent for lab-chat with the instructions You are Shikhar Classes' fees desk. Use the tools. and that tool.
Make one SQLiteSession with the id parent-98290 and run the agent on two messages, in this order, with that session each time: Is any instalment pending for my son? then His ID is S2041.
Print each run's final output on its own line. The program is async throughout (the session and Runner.run are async), with no run_sync in it.
- Task 2
per_parent.py: one session per parent
Use task 1's tool and agent. Run four messages in this order, each with the session of its phone number, whose id is the number: 98290-11111 Is any instalment pending for my son?, 98290-22222 Is any instalment pending for my daughter?, 98290-11111 His ID is S2041., 98290-22222 Her ID is S2052.
Print one line per message: the phone number, a colon and a space, then the run's final output. Each parent's runs must carry only that parent's chat.
- Task 3
undo.py: undo a wrong turn
Write a tool book_doubt_session(student_id, subject) that returns the JSON text of shikhar.book_doubt_session(student_id, subject), and an agent for lab-chat with the instructions You are Shikhar Classes' academic desk. Use the tools. and that tool.
With one SQLiteSession (id parent-98290), run the agent on I want to book a chemistry doubt session for my daughter. and then on S2O30 (a letter O for the zero). Then take that wrong turn out of the session: its two items, the parent's message and the agent's reply. Then run the agent on S2030.
Print the last run's final output, then Items: and the number of items the session holds at the end.
- Task 4 · interview question
Interview: remembers.py, the model remembers nothing
Use task 1's tool and agent, and a session kept in a file: SQLiteSession("parent-98290", "chats.db"). The program takes one argument on the command line. With forget, it clears the session and prints Forgotten, without calling the model. With anything else, it runs the agent on that message with the session, and prints the run's final output, then Items: and the number of items the session holds after the run.
The check runs it as separate programs: Is any instalment pending for my daughter?, Her ID is S2052., forget, Her ID is S2052. again.
The interviewer will ask: the second program knew the question. Did the model remember it? Where was it?
What you will learn
- Why the model remembers nothing between requests.
- SQLiteSession and session= on a run: the conversation kept for you.
- What a session adds to the request: the earlier input items.
- get_items, pop_item and clear_session: reading, correcting and emptying a session.
- One session per parent, and sessions in a file.
More in Memory
Build "Sessions" 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.