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

Run and continue sessions

Start work, follow progress, and continue the conversation.

A session keeps an agent’s configuration, conversation, and saved work over time. Reuse the same session to send follow-up messages and continue the work.

Sessions and turns

A turn is one cycle of work within a session. A message sent to an idle session starts a new turn. A message sent during an active turn steers that turn.

Turns run asynchronously. Your application can follow progress through streaming or receive session state changes through webhooks.

Start work

Create a session with an agent configuration and initial input. Set stream to true to receive events from the first turn in the same request.

With your API key and SDK configured, run this example to create and run a script. OpenAI manages its environment:

Create a session and stream its first turn
from openai import OpenAI

with OpenAI() as client:
    with client.beta.agents.sessions.create(
        agent={
            "model": "gpt-6-astra",
            "instructions": "Write clean code, run it, and report the actual output.",
        },
        environment={"type": "openai_hosted"},
        input="Create tree.py, a Python script that prints a readable tree of the files in the current directory. Run it and show me the output.",
        stream=True,
    ) as events:
        for event in events:
            print(event.to_json(indent=None), flush=True)

Store the session_id with your application’s conversation state. Use it to send follow-up messages and retrieve saved work for that conversation.

See Configuring Agents for reusable agent settings and Architecture for environment choices. Sessions with environment.type: "none" require initial input. The Create session reference lists the request fields.

Follow progress and handle outcomes

Events report output and changes as the agent works. Check the turn’s outcome: completion, failure, or cancellation. An idle session alone does not mean the turn succeeded.

Look for agent.session.turn.completed, agent.session.turn.failed, or agent.session.turn.cancelled. Inspect the agent’s output too: a completed turn does not guarantee every tool succeeded.

If the session needs a function result or an environment connection, retrieve it and inspect required_actions. Your code must handle the function call or connect the environment so work can continue.

See Events and Items for event types and payloads.

Continue or steer the work

Send another agent.session.input.message to the same session. If the agent is working, the message steers the active turn. If the session is idle, it starts a new turn with the existing conversation.

Use the conversation’s session ID to send input. Subscribe to its event stream before sending the message so your application receives the turn’s early events.

Pass your API client, session ID, and message to a function in your application:

Send a follow-up message
def send_message(client: OpenAI, session_id: str, text: str) -> None:
    client.beta.agents.sessions.events.create(
        session_id,
        events=[
            {
                "type": "agent.session.input.message",
                "input": [
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "input_text",
                                "text": text,
                            }
                        ],
                    }
                ],
            }
        ],
    )

For a combined send-and-stream example, see Events and Items.

Retrieve saved work

Events show live progress. Items are the saved messages and tool calls, including completed responses. Retrieve them to display previous work or inspect results after a turn ends:

Retrieve session items
def list_items(client: OpenAI, session_id: str):
    return client.beta.agents.sessions.items.list(session_id, order="asc", limit=100)

See Managing sessions to inspect session state and turn outcomes. Retrieve files through Files and artifacts.

Streams do not replay missed events. After a disconnect, retrieve the session and its saved items to recover the work. See Recover a disconnected stream for the reconnection procedure.

Cancel an active turn

Cancel the current turn when you want the agent to stop. The session and its previous work remain available:

Cancel the active turn
def cancel_turn(client: OpenAI, session_id: str) -> None:
    client.beta.agents.sessions.events.create(
        session_id, events=[{"type": "agent.session.input.cancel"}]
    )