agents/apiTHE ENCYCLOPEDIA
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

23 matching topics

What is the OpenAI Agents API?FoundationsAgents API vs Agents SDK vs Responses APIComparisonsAgents API quickstartGetting startedSessions, turns & durable stateCore conceptsHosted vs self-hosted sandboxesInfrastructureConnect MCP servers to Agents APITools & integrationsFunction calling & application toolsTools & integrationsMulti-agent orchestrationCore conceptsStreaming, webhooks & event handlingIntegration guidesFiles, outputs & published artifactsCore conceptsVaults & MCP authenticationTools & integrationsAgent security & credential boundariesProductionTracing, usage & debuggingProductionAgents API pricing & cost planningProductionOpenAI Agents API with PythonGetting startedAgents API with TypeScript & Next.jsIntegration guidesWhat is the OpenAI Agents SDK?FoundationsResponses API in the agent stackFoundationsAgent Skills & SKILL.mdTools & integrationsProgrammatic Tool CallingTools & integrationsRAG, file search & vector storesTools & integrationsWeb search & source citationsTools & integrationsChatKit & Agent BuilderIntegration guides
K to open Esc to close