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:
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:
# Pass your saved session ID to this helper.
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 bynamewith itsarguments. Return the result on the same session using the action’sturn_idandcall_id. See Function tools.environment_connection: Connect the environment identified byenvironment_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.
Set a session budget
A spend_control object must include limit. Set it to a positive integer in USD cents, such as 500 for $5, or null for no limit.
Budget control makes a best-effort attempt to limit spending. Spend readings can lag, and work that has already started can continue to incur charges. Actual spend can exceed the limit. If the API cannot check spend, work can continue.
Create a session with a limit
Send a POST request to /v1/agents/sessions. For a session without an execution environment, include initial input:
{
"agent": { "model": "gpt-6.1-terra" },
"environment": { "type": "none" },
"input": "Reply with exactly OK.",
"spend_control": { "limit": 500 }
}
To create an unlimited session, omit spend_control or set it to null.
Read the limit and recorded spend
Send a GET request to /v1/agents/sessions/{session_id}. Replace {session_id} with the ID returned by creation. The session response always includes a spend_control object:
{
"id": "sess_example",
"object": "agent.session",
"spend_control": {
"limit": 500,
"consumed": null
}
}
limit is the total session limit, or null if no limit is set. consumed is the best-effort recorded spend, rounded down to whole USD cents. It is null if no usable reading is available. If budget control is unavailable for the project, both values are null.
This unlimited session has recorded $1.23.
{
"spend_control": {
"limit": null,
"consumed": 123
}
}
Change or remove the limit
Send a POST request to /v1/agents/sessions/{session_id} with the new total limit. This request changes the total limit to $10 without resetting recorded spend:
{
"spend_control": { "limit": 1000 }
}
Omit spend_control from an update to keep the current limit. To remove the limit, set spend_control to null or send the following request.
{
"spend_control": { "limit": null }
}
Retrieve the session again to confirm the change. An unlimited session has spend_control.limit: null. Recorded spend can change between reads.
Continue after reaching the budget
Watch for session status budget_reached or the agent.session.budget_reached event. The API rejects new input when it detects that the session has reached its limit. A turn stopped by the budget fails with session_budget_exceeded.
Increase or remove the total limit to continue. Work stopped by the budget can resume automatically. Follow session events before sending more input.
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.
# Replace the illustrative IDs and URLs below with your own resource values.
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(), "sess_123")
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.