> ## Documentation Index
> Fetch the complete documentation index at: https://platform.kimi.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Event Stream

> Subscribe to session and thread SSE event streams, read historical events, and resume receiving agent output after disconnects.

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](https://platform.kimi.ai/docs/hosted-agents/webhooks.md). 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](https://platform.kimi.ai/docs/hosted-agents/tools.md#custom-tools).

Every event object shares these common fields:

| Field | Description |
| - | - |
| `id` | Server-assigned event ID. Your application can use it to identify events and deduplicate them when re-reading. |
| `processed_at` | Timestamp when the event was processed and written to session history. Historical event sorting and pagination use this field. |
| `session_thread_id` | ID of the thread that emitted the event. Coordinator-level events omit this field. |
| `type` | Event type. |
| `data` | Event payload. Omitted when the event carries no data. |

For example, a complete `agent.message` event looks like this:

```json theme={null}
{
  "id": "sevt_example_message",
  "processed_at": "2026-01-01T00:00:00Z",
  "type": "agent.message",
  "data": {
    "content": [
      {"type": "text", "text": "Task completed."}
    ]
  }
}
```

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](https://platform.kimi.ai/docs/hosted-agents/sessions.md).
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.

```bash cURL theme={null}
curl -sS -N "https://api.moonshot.ai/v1/sessions/$SESSION_ID/events/stream?cursor=now" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Accept: text/event-stream"
```

```bash CLI theme={null}
kha sessions attach "$SESSION_ID" --since now
```

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`:

| Field | Description |
| - | - |
| `event` | Message type, such as `agent.message`; control and error messages use `control` and `error`, respectively. |
| `id` | Resume cursor for the SSE message. Save this value verbatim; the session event object in regular messages has its own separate `id`. |
| `data` | Message payload. Contains a session event object for regular messages, control metadata for `control` messages, and error details for `error` messages. |

The example below shows the wire format for connection setup, event delivery, keepalive comments, and stream errors:

```text theme={null}
event: control
id: 4096
data: {"cursor":"4096","reason":"tail"}

id: 4128
event: agent.message
data: {"id":"sevt_example_message","type":"agent.message","processed_at":"2026-01-01T00:00:00Z","data":{"content":[{"type":"text","text":"Task completed."}]}}

: hb

event: error
data: {"error":{"type":"server_error","message":"stream failed"}}
```

`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](https://platform.kimi.ai/docs/hosted-agents/sessions.md#start-a-task).

## 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](https://platform.kimi.ai/docs/hosted-agents/tools.md#custom-tools) and [Permissions](https://platform.kimi.ai/docs/hosted-agents/permissions.md).
`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](https://platform.kimi.ai/docs/hosted-agents/github.md#handle-repository-preparation-errors) 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`.

```bash cURL theme={null}
curl -sS "https://api.moonshot.ai/v1/sessions/$SESSION_ID/events?page_size=20" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha sessions events "$SESSION_ID" --limit 20
```

If the response includes `next_page_token`, pass it directly as `page_token` in the next request to read the next page:

```bash cURL theme={null}
curl -sS "https://api.moonshot.ai/v1/sessions/$SESSION_ID/events?page_size=20&page_token=$NEXT_PAGE_TOKEN" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha sessions events "$SESSION_ID" --limit 20 --page-token "$NEXT_PAGE_TOKEN"
```

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:

```bash cURL theme={null}
curl -sS -N "https://api.moonshot.ai/v1/sessions/$SESSION_ID/events/stream?cursor=$SSE_CURSOR" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Accept: text/event-stream"
```

```bash CLI theme={null}
kha sessions attach "$SESSION_ID" --since "$SSE_CURSOR"
```

You can also pass the same cursor in the `Last-Event-ID` request header:

```bash cURL theme={null}
curl -sS -N "https://api.moonshot.ai/v1/sessions/$SESSION_ID/events/stream" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Accept: text/event-stream" \
  --header "Last-Event-ID: $SSE_CURSOR"
```

```bash CLI theme={null}
kha sessions attach "$SESSION_ID" --since "$SSE_CURSOR"
```

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`:

| `data.kind` | How to handle |
| - | - |
| `text` | Append `data.delta` in order to the end of the currently displayed text. |
| `thinking` | Append `data.delta` in order to the end of the currently displayed thinking content. |
| `tool_use` | Correlate deltas for the same tool call by `data.tool_use_id`, and append `data.input_delta` in order to the tool input buffer. The first delta includes `name`, and also includes `mcp` when the tool comes from an MCP server. |

For example, suppose your application receives these two text deltas in sequence:

```json theme={null}
{
  "type": "agent.delta",
  "data": {
    "kind": "text",
    "delta": "Analyzing"
  }
}
```

```json theme={null}
{
  "type": "agent.delta",
  "data": {
    "kind": "text",
    "delta": " data"
  }
}
```

Appending `data.delta` from each event in order updates the live output from `"Analyzing"` to `"Analyzing data"`.

A tool input delta looks like this:

```json theme={null}
{
  "type": "agent.delta",
  "data": {
    "kind": "tool_use",
    "tool_use_id": "toolu_example_call",
    "name": "search",
    "input_delta": "{\"query\":\""
  }
}
```

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](https://platform.kimi.ai/docs/hosted-agents/multiagent-orchestration.md).

### Read thread history

```bash cURL theme={null}
curl -sS "https://api.moonshot.ai/v1/sessions/$SESSION_ID/threads/$THREAD_ID/events?page_size=20" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha sessions threads events "$SESSION_ID" "$THREAD_ID" --limit 20
```

### Subscribe to real-time thread events

```bash cURL theme={null}
curl -sS -N "https://api.moonshot.ai/v1/sessions/$SESSION_ID/threads/$THREAD_ID/events/stream?cursor=now" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Accept: text/event-stream"
```

```bash CLI theme={null}
kha sessions attach "$SESSION_ID" --thread "$THREAD_ID" --since now
```

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.
