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

# Events reference

> How to read a Session's events from GET /events: the cursor, long polling, deduplication by id, and ordering by position.

`GET /v3/organizations/{orgId}/sessions/{sessionId}/events` returns a Session's activity in pages. It is a poll, not a stream: each call returns the events after a cursor, and with `wait_ms` it waits for the next one.

## What you will have

* A loop that reads every event exactly once in transcript order.
* A rule for handling the same event coming back on a later page.

## Prerequisites

Create or open a Session, and have an API key with `sessions:read`. See [Build a chat UI on the API](https://docs.reason.bot/docs/build/chat-ui-api.md).

## The cursor

Start with `after_seq=0`. Each response returns:

* `items`: the events after the cursor, in ascending `seq`.
* `next_seq`: the last item's `seq`, or the same cursor when `items` is empty.
* `session`: the current status (`running`, `exit`, `error` or `suspended`) and `outcome`.

Pass `next_seq` as the next `after_seq`. Stop when a page's `items` is empty and `session.status` is no longer `running`. Add `wait_ms` (for example `20000`) so an empty page waits for the next event instead of returning at once.

```text theme={null}
GET .../events?after_seq=0&wait_ms=20000   -> items, next_seq=57, status=running
GET .../events?after_seq=57&wait_ms=20000  -> items=[], next_seq=57, status=running   (waited)
GET .../events?after_seq=57&wait_ms=20000  -> items=[...], next_seq=61, status=exit
```

## The same event can come back

A streamed event (a reply, reasoning, or a tool's live output) keeps its `id`. Each time its content grows, it returns with a higher `seq`. So a poller can receive one `id` on more than one page.

Keep the copy of each `id` with the **highest `seq`**. That copy has the most complete content.

## Order by position, not seq

`seq` is a delivery cursor. It is not the order things happened in. Sort by `position` to get the transcript:

* `position` is the `seq` an event first appeared at.
* It never changes for an event, and no two events in a Session share one.

A streamed reply can therefore show a higher `seq` than an event that happened after it. Sorting by `seq` would put it in the wrong place.

## Numbers are not counts

`seq` and `position` jump by about 4.29 billion (2^32) when a follow-up, a retry, or a machine change starts a new attempt. They also have gaps where an event is hidden. Compare them and sort with them. Do not count with them or show them to a person.

## Event types

Each event has a `type`:

| Type | Content |
| - | - |
| `status` | Lifecycle changes. `text` and `data` are null without `sessions:debug`. |
| `assistant` | A reply the agent wrote. `text` holds it. |
| `reasoning` | The agent's reasoning, when the model shares it. |
| `tool_use` | A tool call, with its input in `data`. |
| `tool_result` | A tool's result, with its output in `data`. |
| `compaction_started` | The agent started compacting the Session's context. `text` is null. See [Compaction events](#compaction-events). |
| `compaction_finished` | A compaction ended. `text` is null. See [Compaction events](#compaction-events). |

Without `sessions:debug`, `data` carries a fixed set of fields (for example `tool`, `input`, `content`, `is_error`, `duration_ms`). An owner or admin with `sessions:debug` gets the full diagnostic projection.

To read only the conversation, add `view=conversation`. It returns only `assistant`, `reasoning`, `tool_use`, `tool_result`, `compaction_started` and `compaction_finished` events, and it does not move the cursor.

## Compaction events

When a Session's context grows too large, the agent compacts it: earlier context is folded into a summary so the turn can continue. Each compaction shows up as a `compaction_started` event and, when it ends, a `compaction_finished` event. Both have `text` set to null, and neither ever contains the summary or any conversation content.

`data` on both events:

| Field | Meaning |
| - | - |
| `compaction_id` | A string that is the same on the started and finished events of one compaction. |
| `trigger` | Why it ran: `automatic`, `manual` or `overflow`. Treat an unknown value as `automatic`. |

`trigger` values:

* `automatic`: the context reached its compaction threshold or the model's input limit.
* `manual`: someone asked for it, with a `compactSession` request (`POST .../sessions/{id}/compact`) or the app's or agent's compact, in a running turn or on a settled Session.
* `overflow`: the model refused the last request as too long, so the next one is compacted first.

`compaction_finished` also carries:

| Field | Meaning |
| - | - |
| `status` | `completed`, `skipped` or `failed`. Treat an unknown value as `failed`. |
| `tokens_before` | Context size before compaction. |
| `tokens_after` | Context size after compaction. |
| `threshold_tokens` | The compaction threshold. |
| `duration_ms` | How long the compaction took. |

Each number is a non-negative integer or null.

`status` values:

* `completed`: earlier context was folded into a summary, or dropped to fit when no summary could be made.
* `skipped`: there was nothing, or nothing more, to compact, or the request arrived as the turn ended.
* `failed`: the summary could not be made. The turn continues uncompacted.

A turn cancelled mid-compaction can leave a `compaction_started` event with no `compaction_finished` event. A manual compaction of a finished Session can also look unmatched on `listSessionEvents`: if a new message starts a turn before the compaction ends, its `compaction_finished` is recorded on the earlier turn, which a poller that already moved on doesn't re-read, so treat the start of a newer turn as ending any open compaction indicator (the workspace event stream still delivers `compaction.finished`). With `sessions:debug`, the raw `status` events are still shown as well.

## Working code

The [chat UI sample](https://docs.reason.bot/docs/build/chat-ui-api.md) implements this: a `Map` keyed by `id` that keeps the highest `seq`, a cursor loop with `wait_ms`, and a sort by `position`.

## Check it works

Run the sample with a Session that answers one question. The reply prints once, in order, even though the poll saw the reply more than once while it grew.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.