Skip to main content

Streaming Events

Real-time task events are delivered via Server-Sent Events (SSE) through the GET /api/v1/tasks/:id/stream endpoint. This is the recommended way to receive live updates as the AI agent processes your request.

Architecture

The API uses a fire-and-forget pattern:
  1. POST /tasks returns JSON immediately with the task ID
  2. GET /tasks/:id/stream is the unified SSE channel for all events
Task execution runs in the background. You can connect, disconnect, and reconnect to the stream at any time without affecting execution.

SSE Format

Events are delivered in the standard SSE format:
Each event consists of:
  • event - The event type identifier
  • data - JSON payload with event details
Events are separated by double newlines (\n\n).

Stream Phases

The stream delivers events in three phases:

Phase 1: History Replay

All saved conversation messages are emitted as history_message events.

Phase 2: History Complete

A single history_done event signals that history replay is complete and indicates whether the task is still running.

Phase 3: Live Events (if running)

If the task is currently executing, the stream delivers catch-up state followed by real-time events until the task completes.

Event Types

history_message

A replayed conversation message. Emitted for each saved message at the start of the stream.

history_done

Signals that all history messages have been sent.

status

Processing status updates indicating task progress.
Status values:

thought

Agent’s thinking and reasoning process. Useful for understanding how the AI approaches the task.

content

Text content from the agent’s response. May be delivered in chunks.

tool_call

Indicates a tool is being executed by the agent.
Tool call status values:

tool_result

Result of a completed tool execution.

error

Error event indicating something went wrong.

done

Task completion event with the final response.
The done event signals that the current execution turn is complete. For completed tasks, the stream ends after this event. For ongoing conversations, you can send follow-ups and reconnect.

Subagent Events

When the AI agent delegates work to specialized subagents, these events track the subagent’s lifecycle and activity. Each subagent session begins with subagent_start and ends with subagent_end, with subagent_thought and subagent_tool events in between.

subagent_start

Emitted when the agent delegates work to a subagent.

subagent_thought

Reasoning text from a running subagent.

subagent_tool

Tool usage within a subagent. Emitted at both the start and end of each tool call.

subagent_end

Emitted when a subagent finishes execution.

Parsing SSE in Code

JavaScript

Python

Connection Handling

The server sends periodic heartbeat comments (:heartbeat) to keep the connection alive. These can be safely ignored by SSE parsers.
Disconnect and reconnect to the stream at any time. The stream replays full conversation history, then catches up to the current state. The task continues executing in the background regardless of stream connections.
Closing the stream connection does NOT cancel or stop the task. Execution continues in the background. Use DELETE /api/v1/tasks/:id to explicitly cancel a task.
Tasks may take several minutes to complete. Ensure your HTTP client has appropriate timeout settings. The 15-second heartbeat keeps the connection alive.

Polling as an Alternative

If you cannot use SSE streaming (e.g., due to infrastructure limitations), you can poll the task endpoint to get real-time progress via the output field.

Output Structure

When polling GET /api/v1/tasks/{id}, the response includes an output object that works the same way for both running and completed tasks:
Use the task’s status field to determine if output.content is partial (when status is running or awaiting_approval) or final (when status is completed, failed, or cancelled).

Polling Example

SSE vs Polling

Use SSE streaming when possible for the best user experience. Fall back to polling with the output field when SSE is not available.