Skip to main content
The event stream is how your application receives session execution status updates and agent output. Your application can send user events to a session and receive agent events and session events in real time over Server-Sent Events (SSE). If you only need signed HTTPS notifications when major changes occur to project resources, without maintaining a persistent connection to read detailed output, use Project Webhooks. Project Webhooks cover multiple types of project resources—including sessions, agents, triggers, and execution environments—and operate independently of the SSE event stream.

Event types

Each event object uses type to identify the event type and puts its payload in data; events with no payload omit data. Events fall into three categories:
  • User events: Inputs your application sends to a session, such as user.message, user.interrupt, and user.custom_tool_result.
  • Agent events: Outputs produced by the agent, such as agent.message, agent.thinking, agent.tool_use, agent.tool_result, and agent.custom_tool_use.
  • Session events: Updates describing session state and execution outcomes, such as session.status, session.error, session.thread_created, and session.thread_status.
agent.delta is an incremental event sent only on the real-time event stream and disabled by default: pass include_deltas=true in the stream URL when subscribing to receive it. Historical event lists never include agent.delta. Inside data.tool_result on an agent.tool_result event, content holds the content passed to the model (text, image, or video) and is always present; structured_content holds content formatted for UI display (text, file, image, or artifact). When a tool provides no separate display blocks (such as plain-text tools, MCP tools, or failed calls without display blocks), structured_content is [], and clients should fall back to rendering content. Custom tool calls use the agent.custom_tool_use and user.custom_tool_result events; see Custom tools. Every event object shares these common fields: For example, a complete agent.message event looks like this:
Note that the SSE message and the session event object inside it each have their own id:
  • The id on the SSE message is the resume cursor, used to resume the subscription after a disconnect.
  • The id on the session event object inside data is the event ID itself, used for identification and deduplication.

Integrate events

After creating a session, subscribe to the event stream first and then send the first message. Connecting before the message triggers execution ensures you do not miss early status updates or output events. For the end-to-end flow of creating a session and sending the first message, see Create a Session. This page covers the event stream endpoints and event handling patterns.

Subscribe to the real-time event stream

In the commands below, $SESSION_ID comes from id in the session creation response. Use cursor=now on your initial subscription to receive only events produced after the connection opens, without replaying prior history.
Once the connection is established, the server first sends a control SSE message reporting the current resume cursor. If the session has no events yet, that cursor is 0. Lines starting with : are keepalive comments that carry no event data and can be ignored. An SSE message consists of fields such as id, event, and data: The example below shows the wire format for connection setup, event delivery, keepalive comments, and stream errors:
control messages report the resume position, while regular messages carry session event objects. Keepalive lines start with :—the server sends : hb every 30 seconds with no event payload. event: error indicates an error on the event stream itself, after which the server closes the connection.

Send input events

After establishing the SSE connection, call POST /v1/sessions/{session_id}/events for the same session to submit user.message, user.interrupt, user.tool_confirmation, or user.custom_tool_result. When accepted, the endpoint responds with 202. Responses for queued inputs may include event_id for querying the pending queue or withdrawing the input; responses for custom tool results omit this field. Subsequent status updates and outputs arrive over the SSE connection. For the full cURL command to send the first user.message, see Create a Session.

Track task status

Receiving session.status with data.status set to idle means no turn is currently running. When data.stop_reason.type is end_turn, the current turn has finished. When it is requires_action, the session is waiting on the client, and stop_reason.event_ids lists the pending event IDs—including custom tool calls awaiting results (agent.custom_tool_use) and agent.tool_use events awaiting tool confirmation. Execution resumes once all required confirmations and results are returned; see Custom tools and Permissions. idle does not mean the session resource has ended; your application can still send new user events to the same session. session.error indicates an error during session execution, where data.error_type is a machine-readable error category and data.message describes the error. Asynchronous GitHub repository preparation failures are also reported through this event; see GitHub Repositories for the specific error types and how to handle them. Session execution errors differ from event: error SSE messages: session.error means execution failed inside the session, whereas event: error means the SSE stream itself encountered an error and the server closes the connection after sending it.

Read history and transition to real-time events

Read historical events in pages via GET /v1/sessions/{session_id}/events. items in the response are ordered by processed_at. By default (order=asc), the oldest events appear first; passing order=desc returns the newest events first, and page_token paginates backward toward earlier events. The response may include next_page_token and stream_cursor. items never include agent.delta.
If the response includes next_page_token, pass it directly as page_token in the next request to read the next page:
Replace $NEXT_PAGE_TOKEN with next_page_token from the previous response. Once you have read up to the desired point, use stream_cursor from that page’s response to subscribe to subsequent real-time events—do not pass next_page_token as cursor on the real-time stream. Because stream_cursor marks the resume position near the end of that page, subscribing from it may replay events already read at the tail of that page. Deduplicate events by the id on each session event object.

Reconnect after a disconnect

Cursors are opaque strings: store them and pass them back unchanged without parsing or concatenating them. After a network disconnect, reconnect by passing the most recently received SSE message id either as the cursor query parameter or in the Last-Event-ID request header. Use the SSE message id (the transport-level resume cursor), not the session event object id. Replace $SSE_CURSOR below with the most recent SSE message id saved by your application:
You can also pass the same cursor in the Last-Event-ID request header:
If both the cursor query parameter and the Last-Event-ID header are provided, the server uses the cursor query parameter. If the cursor is invalid, the endpoint returns an error; re-read historical events and resume the real-time subscription using the new stream_cursor.

Handle event deltas

agent.delta appears only in the real-time event stream and never in historical event lists. By default, the event stream delivers only complete events; add include_deltas=true to the stream URL when subscribing (for example, .../events/stream?cursor=now&include_deltas=true) to receive agent.delta. If your application only needs final results after generation completes, leave this parameter off and handle complete agent.message, agent.thinking, and agent.tool_use events directly. Currently, kha sessions attach and kha sessions events --watch do not append include_deltas=true automatically. To receive delta events, subscribe with cURL and include the query parameter explicitly. To render output incrementally while the agent generates it, handle deltas according to data.kind: For example, suppose your application receives these two text deltas in sequence:
Appending data.delta from each event in order updates the live output from "Analyzing" to "Analyzing data". A tool input delta looks like this:
All deltas for the same tool call share the same tool_use_id. input_delta is a fragment of the tool input JSON—individual fragments are typically not valid JSON on their own, so concatenate the full input before parsing. Deltas are for real-time display only and are not the authoritative final output. Once you receive the complete agent.message, agent.thinking, or agent.tool_use event, replace the accumulated delta content with the complete event payload.

Inspect thread events

In a multi-agent session, every thread has its own event history and real-time event stream. Session-level endpoints return events from the coordinator thread; to inspect subthreads, use the thread-level endpoints. You can obtain a subthread’s thread_id from session_thread_id on events or from the session’s thread list. For multi-agent configuration and thread relationships, see Multi-Agent Orchestration.

Read thread history

Subscribe to real-time thread events

Replace $SESSION_ID and $THREAD_ID with the actual session ID and thread ID. Thread history and real-time streams work the same way as session-level events, except that a thread cursor is valid only for that specific thread’s event stream and cannot be used on other threads or on the session stream.