Manage sessions
For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending
.mdto the page URL.
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
import OpenAI from "openai";
const client = new OpenAI();
let page = await client.beta.agents.sessions.list({ limit: 20 });
console.log(page.data);
if (page.hasNextPage()) {
page = await page.getNextPage();
console.log(page.data);
}
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())
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
)
ctx := context.Background()
client := openai.NewClient()
result, err := client.Beta.Agents.Sessions.List(ctx,
openai.BetaAgentSessionListParams{Limit: openai.Int(20)})
if err != nil {
panic(err)
}
fmt.Println(result.Data)
if result.HasMore {
result, err = result.GetNextPage()
if err != nil {
panic(err)
}
fmt.Println(result.Data)
}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.beta.agents.sessions.SessionListParams;
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
var result =
client.beta().agents().sessions().list(SessionListParams.builder().limit(20L).build());
System.out.println(result.items());
if (result.hasNextPage()) {
result = result.nextPage();
System.out.println(result.items());
}
require "openai"
client = OpenAI::Client.new
result = client.beta.agents.sessions.list(limit: 20)
puts result.data
if result.next_page?
result = result.next_page
puts result.data
end
page=$(curl -sS --fail-with-body "https://api.openai.com/v1/agents/sessions?limit=20&order=desc" \
-H "OpenAI-Beta: agents=v1" \
-H "Authorization: Bearer $OPENAI_API_KEY")
after=$(printf '%s' "$page" | jq -r 'select(.has_more) | .last_id // empty')
if [ -n "$after" ]; then
curl --get "https://api.openai.com/v1/agents/sessions" \
-H "OpenAI-Beta: agents=v1" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
--data-urlencode "after=$after" \
--data-urlencode "limit=20"
fi
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
// Pass your saved session ID to this helper.
async function retrieveSession(client: OpenAI, sessionId: string) {
return client.beta.agents.sessions.retrieve(sessionId);
}
# Pass your saved session ID to this helper.
def retrieve_session(client: OpenAI, session_id: str):
return client.beta.agents.sessions.retrieve(session_id)
// Pass your saved session ID to this helper.
func retrieveSession(ctx context.Context, client *openai.Client, sessionID string) (*openai.AgentSession, error) {
return client.Beta.Agents.Sessions.Get(ctx, sessionID)
}
// Pass your saved session ID to this helper.
public static AgentSession retrieveSession(OpenAIClient client, String sessionId) {
return client
.beta()
.agents()
.sessions()
.retrieve(SessionRetrieveParams.builder().sessionId(sessionId).build());
}
# Pass your saved session ID to this helper.
def retrieve_session(client, session_id)
client.beta.agents.sessions.retrieve(session_id)
end
curl \
"https://api.openai.com/v1/agents/sessions/$session_id" \
-H "OpenAI-Beta: agents=v1" \
-H "Authorization: Bearer $OPENAI_API_KEY"
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.
Delete a session
// Replace the illustrative IDs and URLs below with your own resource values.
import OpenAI from "openai";
async function deleteSession(client: OpenAI, sessionId: string) {
return client.beta.agents.sessions.delete(sessionId);
}
const result = await deleteSession(new OpenAI(), "sess_123");
console.log(result);
# 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())
// Replace the illustrative IDs and URLs below with your own resource values.
package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
)
func deleteSession(ctx context.Context, client *openai.Client, sessionID string) (*openai.AgentSessionDeleted, error) {
return client.Beta.Agents.Sessions.Delete(ctx, sessionID)
}
func main() {
client := openai.NewClient()
result, err := deleteSession(context.Background(), &client, "sess_123")
if err != nil {
panic(err)
}
fmt.Println(result)
}
// Replace the illustrative IDs and URLs below with your own resource values.
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.beta.agents.AgentSessionDeleted;
import com.openai.models.beta.agents.sessions.SessionDeleteParams;
public final class AgentsApiSessionsDeleteSessionExample {
public static AgentSessionDeleted deleteSession(OpenAIClient client, String sessionId) {
return client
.beta()
.agents()
.sessions()
.delete(SessionDeleteParams.builder().sessionId(sessionId).build());
}
public static void main(String[] args) {
var result = deleteSession(OpenAIOkHttpClient.fromEnv(), "sess_123");
System.out.println(result);
}
}
# Replace the illustrative IDs and URLs below with your own resource values.
require "openai"
def delete_session(client, session_id)
client.beta.agents.sessions.delete(session_id)
end
puts delete_session(OpenAI::Client.new, "sess_123")
curl -X DELETE \
"https://api.openai.com/v1/agents/sessions/$session_id" \
-H "OpenAI-Beta: agents=v1" \
-H "Authorization: Bearer $OPENAI_API_KEY"
To stop current work and keep the conversation, cancel the active turn. See the Delete session reference for the deletion response.