When to create a new session
Create one session per independent conversation: a chat thread, a support ticket, a scheduled job. Reuse the same session when the agent should remember what was said before.Create a session
Examples on this page assumeclient is an OpenAI client configured as in Build with the API.
A session needs an agent and an environment. Pass input to send the first message right away, or omit it and send messages later.
agent_idselects a saved agent. You can instead pass an inlineagentconfiguration if you do not want to save one.environmentdecides whether the agent gets a sandbox. Use{"type": "none"}for conversation and tools that run elsewhere. See execution environments.vault_ids(optional) attaches stored credentials for MCP tools. See credentials and vaults.metadata(optional) is a set of your own key-value labels, such as a user or ticket ID. You can change it later withsessions.update.
Send a follow-up message
There are two ways. Both add the message to the same conversation; use one or the other. Post an input event. It returns as soon as the message is accepted, whether or not a turn is running:idle. Otherwise, for example while a turn is running, it raises ValueError; send with sessions.events.create and follow the turn with sessions.events.stream instead (see streaming).
Full example
This program creates a session with a first message, waits for the turn by polling, prints the history, and then sends a follow-up using the stream helper.Read the history
sessions.items.list(session_id) returns the saved history, newest first. Items are not only messages: tool calls, tool results, and commands are items too. Check each item’s type before reading its fields. The item types table lists them.
sessions.turns.list(session_id) returns the turns, newest first. Each has a status, an error if it failed, and usage with token counts once it ends.
Both lists are paginated. A long conversation does not fit in one page. In Python, iterate the result directly (for item in sessions.items.list(...)) to fetch all pages automatically, or pass after= the last ID you saw.
Resume later
Orca stores sessions durably. To pick up a conversation hours or days later, store the session ID in your database and use it again:status before you send anything. If it is requires_action, the agent is waiting for you; see required_actions.
A session with a managed sandbox does not need to keep it awake while you are away. The sandbox goes to sleep on its own after the idle time, or you can put it to sleep at once. The conversation stays, everything in /workspace is saved, and the next message wakes it. Running programs do not come back. See when a sandbox sleeps.
Delete when finished
sessions.delete(session_id) deletes the conversation, its artifacts, and its sandbox. Download anything you need first. Deleting a session does not delete its agent, and deleting an agent is a separate call.
Common mistakes
- Reading the reply right after sending. The send call returns before the agent has answered. Wait for the turn to end first.
- Treating
idleas success.idlemeans nothing is running. The last turn may have failed; check the turn’s status. - Using an agent ID as a conversation ID. Many sessions can share one agent. Keep the session ID.
- Reading only the first page. Lists are paginated.