For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Primary navigation

Manage sessions

Find sessions, handle required actions, and delete saved work.

Store each session ID with your application’s data store. Use it to retrieve the session’s current state, handle requests from the agent, or delete the session.

Find sessions

List sessions in your project to browse previous work. SDK pagination helpers retrieve additional pages:

List sessions and retrieve the next page
from openai import OpenAI

client = OpenAI()
page = client.beta.agents.sessions.list(limit=20)
print(page.to_json())
if page.has_next_page():
    page = page.get_next_page()
    print(page.to_json())

Inspect a session

Retrieve a session to read its status, agent configuration, environment, and required_actions. Pass your API client and the conversation’s session ID:

Retrieve a session
def retrieve_session(client: OpenAI, session_id: str):
    return client.beta.agents.sessions.retrieve(session_id)

See the Retrieve session reference for the full response schema.

Handle required actions

A session with status requires_action needs your application to act before work can continue. When you receive agent.session.requires_action, retrieve the session and inspect each entry in required_actions:

  • function_call: Run the function identified by name with its arguments. Return the result on the same session using the action’s turn_id and call_id. See Function tools.
  • environment_connection: Connect the environment identified by environment_id. See Connect an environment.

The event tells your application when to check. The retrieved session tells it what to do. After a restart or stream disconnect, retrieve the session to find pending actions. After handling them, continue following events for the turn’s outcome.

For saved messages, tool calls, and turn outcomes, see Fetch items and turns. To identify which agent ran a command, see Observe delegation.

Delete a session

Delete a session when your application no longer needs it. Deletion removes the session from the API. Physical cleanup may continue asynchronously.

Delete a session
import os

from openai import OpenAI


def delete_session(client: OpenAI, session_id: str):
    return client.beta.agents.sessions.delete(session_id)


if __name__ == "__main__":
    result = delete_session(OpenAI(), os.environ["OPENAI_SESSION_ID"])
    print(result.to_json())

To stop current work and keep the conversation, cancel the active turn. See the Delete session reference for the deletion response.