Skip to main content
client.stream(sessionId, options?) is the one consumer of GET /v1/sessions/{id}/stream. It yields Event objects (the core EventSchema) as an async generator, reconnecting across dropped connections without gaps or duplicates.

Signature

number
Resume point. Exclusive: events with seq > afterSeq are delivered, so resuming from the last seq you saw does not replay it. Default 0 — from the beginning.
number
Consecutive failed reconnects tolerated before the stream gives up. Default 5.
boolean
Default true: a session.idle that parks the session on its own outcome grade (stop_reason: "awaiting_delegation" while an outcome.evaluation.started on this stream has no completed yet) does not end the stream, which runs on through the grade and any revision turn to the idle after a final result. false ends at the first session.idle.

Consuming

Track event.seq as you go; if your process restarts, pass the last one back as afterSeq and the stream picks up exactly where it left off.

Reading the agent’s answer

message.completed carries both sides of the conversation: your own message is recorded as { role: "user", content }, and the agent’s reply is the same type without role: "user". Filter on role and keep the last agent message of the turn:
For a follow-up, start after your send so the previous turn is not replayed: const { accepted_seq } = await client.sessions.send(session.id, { message }), then client.stream(session.id, { afterSeq: accepted_seq }). A user message with from: "platform" is a notice from the platform (a finished media job, say), not something you sent.

Event types

Each event is { id, seq, type, session_id, data, created_at }. The types a session’s stream carries: Payloads for each are in the event reference. New types are added over time; the stream skips a type your SDK version does not know rather than failing.

Semantics worth knowing

  • Ends at session.idle. The generator returns as soon as the session yields control — it does not wait for the server to close the socket, which stays open for its own keepalive window.
  • A resume on a settled session ends once caught up. With afterSeq > 0, the stream first reads the session: if it is idle or ended, the generator delivers what is after afterSeq and returns at the session’s last event (at once, yielding nothing, when there is nothing after it), even when that event is not a session.idle. A session that is working (including one with a message you just sent) is waited on as usual.
  • Reconnects on drops. A closed connection without an idle event is retried on the same cursor, up to maxRetries consecutive failures. A delivered event resets the failure count.
  • Duplicates are impossible. Anything at or below the cursor is dropped on the floor, so an overlapping replay after a reconnect never emits twice.
  • A 4xx is not a drop. Client errors (except 429) are the server’s answer — they are thrown as ApiError immediately rather than retried.
  • A grade park is not the end. With throughGrades on (the default), the session.idle that parks a graded session on its own outcome grade does not end the generator; the idle after the grade’s final result does.

Polling instead

If SSE is awkward in your runtime, sessions.events(id, { after_seq, limit }) reads the same log as pages, with the same exclusive cursor. By default it leaves out the opt-in types the stream always sends (message.accepted, outcome.evaluation.* and the other types listed under include_accepted); pass includeAccepted: true to get them. See sessions.

In a browser

Do not call stream from a browser: it would put the control-plane key in reachable memory. The dashboard re-serves the SSE body same-origin using the send escape hatch instead, so the key stays server-side.