agents/api
Getting started

OpenAI Agents API with Python

Install the OpenAI Python SDK and stream a managed agent session from your server.

Python server
openai package
beta.agents.sessions
Streamed events

Use the correct package

For the managed Agents API, install or update the openai package. This is different from the openai-agents package used for the Agents SDK. Keep OPENAI_API_KEY in your server environment and grant session and inference permissions.

Run a managed session

Save the example as quickstart.py and execute python quickstart.py. The context managers close the client and streaming response. The example uses the model shown in the official quickstart; account model access still applies.

Example
from openai import OpenAI

with OpenAI() as client:
    with client.beta.agents.sessions.create(
        agent={"model": "gpt-6-astra"},
        environment={"type": "none"},
        input="Explain the difference between a session and a turn.",
        stream=True,
    ) as events:
        for event in events:
            print(event.to_json(indent=None), flush=True)

Integrate with application state

Save the returned session identifier from events with the user’s conversation. Stream selected progress to your frontend or use webhooks for longer tasks. Handle failures explicitly rather than treating printed JSON as proof of success.

Prepare for production

Set timeout behavior deliberately, persist tool results, and reconcile after disconnections. Download required outputs before deleting sessions. This documentation example performs a paid request only when you run it with your own credentials.

Read the official reference

Check the source for current API fields, account requirements, and service limits.

OpenAI: quickstart