OpenAI Agents SDK · Chapter 14 of 24

The response id

By the end of this chapter you can say what the Responses API keeps on OpenAI's server, continue a conversation from a response's id so the request carries only the new message, chain the requests inside one run the same way, handle the 404 when the server does not have a response, and choose between a session, a response id and an input list for Shikhar's WhatsApp line. 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

Chapter 13's session works, and Karthik showed Kavita what it sends: on every turn, the whole chat again, every message, call and result. A long hostel conversation sends more each turn. The Responses API offers another way, built into it: the server keeps each response, and the next request can point at it by its id instead of sending the conversation again. Kavita's first question is the right one: "Then where are our parents' messages kept, and for how long?" This chapter answers both, and shows when each way is the better one.

What you build: 4 tasks

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

  1. Task 1

    continue_id.py: continue with the response id

    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.

    Run it on I want to book a biology doubt session for my daughter. then on Her ID is S2033., continuing from the first run's last response id, with no session and only the new message as the second run's input.

    Print each run's final output on its own line.

  2. Task 2

    chained.py: what the request no longer carries

    Use task 1's tool and agent. Run it once on Please book a physics doubt session for S2061., with the run setting that chains the run's own requests by their response ids, so the request after the tool call sends only the tool's output.

    Print the run's final output, then the response id of each of the run's raw responses, in order, joined by a space, an arrow and a space ( -> ).

  3. Task 3

    fallback.py: the 404 and the fallback

    Use task 1's tool, and an agent like task 1's whose model settings have store=False. Run it on I want to book a maths doubt session for my son., print its final output, then try to continue with its last response id on His ID is S2041.

    The server kept nothing, so that raises openai.NotFoundError. Catch only that error, print Not kept: and the error's code, then run the agent on the first run's input list followed by a user message His ID is S2041., and print that run's final output.

  4. Task 4 · interview question

    Interview: where_it_lives.py, where the conversation lives

    Write a tool fee_status(student_id) 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.

    Run the same chat, Is any instalment pending for my son? then His ID is S2072., three ways in this order: with a SQLiteSession (id parent-98290); continuing with the first run's last response id; with the first run's input list and the answer as a user message. The program is async throughout.

    Print session:, response id: and input list:, each with a space and its way's second run's final output, one per line.

    The interviewer will ask: where does the conversation live each time, and which would you pick for Shikhar's WhatsApp line?

What you will learn

  • What the server keeps: every response, under its id.
  • previous_response_id: continuing from a response, sending only the new input.
  • auto_previous_response_id: the requests of one run, chained.
  • store=False and the 404 for a response the server does not have.
  • Session, response id or input list: where the conversation lives.

More in Memory

Build The response id 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.