Stream events
Consume a session's SSE event stream from the Python SDK or any HTTP client, with reconnection via since_id, heartbeats, and event filtering.
Every session publishes its events as a Server-Sent Events (SSE) stream. Use
the Python SDK when you can: it handles reconnection,
heartbeat-based stale detection, backoff, and resumption with since_id. Use
the raw SSE protocol from any other client, such as a
non-Python service, a browser, or curl.
With the Python SDK
Section titled “With the Python SDK”client.events.stream(session_id) returns an async iterator over typed events.
Basic stream
Section titled “Basic stream”async for event in client.events.stream(session.id): if event.type == "output.message.delta": print(event.data.get("delta", ""), end="", flush=True) elif event.type == "turn.completed": print() break elif event.type == "turn.failed": print(f"\n[failed: {event.data.get('error')}]") breakFor a UI, render the accumulated text keyed by message_id rather than blindly appending delta. A provider retry can restart the same message, and the completed message remains authoritative.
Tool visibility
Section titled “Tool visibility”To show what the agent is doing while it works, listen for tool.started and tool.completed:
async for event in client.events.stream(session.id): if event.type == "tool.started": tool_call = event.data.get("tool_call", {}) print(f" [tool] {tool_call.get('name')}") elif event.type == "tool.completed": status = "ok" if event.data.get("success") else "error" print(f" [tool] {event.data.get('tool_name')}: {status}") elif event.type == "turn.completed": breakGet the full final message
Section titled “Get the full final message”output.message.completed carries a complete assistant message after streaming finishes. A turn can produce several such messages, including commentary and tool calls; turn.completed marks the end of the turn:
async for event in client.events.stream(session.id): if event.type == "output.message.completed": message = event.data.get("message", {}) for part in message.get("content", []): if part.get("type") == "text": print(part["text"]) elif event.type == "turn.completed": breakWhat the SDK handles for you
Section titled “What the SDK handles for you”- Reconnection. The control plane cycles SSE connections every 5 minutes; the SDK reconnects transparently using
since_id. - Stale detection. The server sends a heartbeat every 30s; the SDK treats >45s of silence as a dead connection and reconnects.
- Backoff. Network errors trigger exponential backoff with jitter.
- Typing. Each event has
.typeand.dataattributes parsed from SSE.
Resuming with since_id
Section titled “Resuming with since_id”While a stream is open the SDK manages reconnection internally. You only need since_id when restarting your application and resuming from a previously recorded durable event ID. With no cursor, the stream is live-only; use a stored input event ID to include responses that arrived before you subscribed:
# Persisted somewhere — file, DB, etc.last_seen_id = load_cursor()
async for event in client.events.stream(session.id, since_id=last_seen_id): handle(event) if event.sequence is not None: # only persisted events are replay cursors save_cursor(event.id)The saved cursor is for application restart recovery. A JSON payload ID on an ephemeral delta is not a replay cursor. Use an SDK version that tracks durable SSE IDs for internal reconnection; older versions that track every payload ID can resume incorrectly after a delta. A durable-event-only stream avoids that ambiguity.
With raw SSE
Section titled “With raw SSE”Subscribe
Section titled “Subscribe”curl -N "https://your-host/api/v1/sessions/$SESSION_ID/sse" \ -H "Authorization: Bearer $EVERRUNS_API_KEY"Each event arrives as:
event: turn.completedid: event_01933b5a00007000800000000000001data: {"id":"event_...","type":"turn.completed","data":{...}}(Blank line terminates each event, per the SSE spec.)
Resume after disconnect
Section titled “Resume after disconnect”Pass since_id to pick up where you left off:
curl -N "https://your-host/api/v1/sessions/$SESSION_ID/sse?since_id=event_..." \ -H "Authorization: Bearer $EVERRUNS_API_KEY"Event IDs are UUIDv7 and durable events have an atomic per-session sequence number. Resume with the last durable SSE id: field to replay stored events, not the JSON payload id. Ephemeral frames omit id:; keep the previous durable cursor when handling them. Streaming deltas are ephemeral when NATS is enabled and are not part of PostgreSQL replay; use the durable completed-message event to reconcile the full content.
Heartbeats
Section titled “Heartbeats”The server sends a heartbeat every 30 seconds as a comment line:
: heartbeatComments are invisible to SSE event parsers, they don’t appear as events. Their only purpose is to keep the TCP connection alive and let your client distinguish “idle” from “dead.”
Client requirement: treat the connection as stale if no data (event or heartbeat) arrives within 45 seconds. Reconnect with the last durable SSE id: value.
Connection cycling
Section titled “Connection cycling”To avoid stale connections through proxies, the server gracefully cycles SSE connections every 5 minutes. Before closing, it sends:
event: disconnectingdata: {"reason":"connection_cycle","retry_ms":100}Clients should reconnect immediately using the last durable SSE id: value as since_id. Stored events are replayed; a missed ephemeral delta is recovered from its corresponding completed event.
Browser EventSource
Section titled “Browser EventSource”function connect(sessionId, lastEventId) { const url = new URL(`/api/v1/sessions/${sessionId}/sse`, API_BASE); if (lastEventId) url.searchParams.set("since_id", lastEventId);
const es = new EventSource(url, { withCredentials: true });
es.addEventListener("connected", () => console.log("SSE connected"));
es.addEventListener("disconnecting", (e) => { const { retry_ms } = JSON.parse(e.data); es.close(); setTimeout(() => connect(sessionId, lastEventId), retry_ms); });
["input.message", "output.message.delta", "turn.completed"].forEach((t) => { es.addEventListener(t, (e) => { const data = JSON.parse(e.data); if (e.lastEventId) lastEventId = e.lastEventId; // SSE cursor, not data.id // handle event... }); });
es.onerror = () => { es.close(); setTimeout(() => connect(sessionId, lastEventId), 2000); };}The native EventSource API uses the retry: field that every event includes (100ms during active streaming, up to 500ms while idle). You don’t need to set retry yourself.
Poll as a fallback
Section titled “Poll as a fallback”If your environment can’t hold long-lived connections (some serverless runtimes), poll instead:
curl "https://your-host/api/v1/sessions/$SESSION_ID/events?since_id=$LAST_ID" \ -H "Authorization: Bearer $EVERRUNS_API_KEY"The same since_id resumption works; latency increases by the polling interval.
See also
Section titled “See also”- Event Reference: every event type and payload.
- Events as the primary store: why the protocol is shaped this way.