Use the OpenAI SDK to create an agent and a session, submit a task, and receive results through SSE.
Prerequisites
- Activate Model Studio and create an API Key.
- Install Python 3.9 or later.
- Confirm a model available in the workspace, for example
qwen3.8-max.
{workspace_id} with the actual workspace ID.
The examples use
environment.type=none. This does not create a sandbox and does not provide built-in tools such as shell or file access. To use files, commands, or environment-side MCP, create a managed environment as described in Environment.1. Create an agent
2. Create a session and receive streamed output
agent.session.idle or the turn terminal status to determine completion.
3. Query history
4. Continue the session
idle before continuing. sessions.stream subscribes to events, submits the input, and receives the result for this turn. A message sent while the session is running usually steers the current turn; if the turn ends before consuming the input, the message can be delivered in a new turn, so track ownership by the actual events and turn ID.
5. Use curl
Create an agent:
stream: true returns the first-turn SSE directly in the create response, so a fast reply is not missed by subscribing after creation. To subscribe to an existing session, use GET /agents/sessions/{session_id}/events; that subscription does not replay historical events.