Skip to main content

Overview

The WebSocket stream provides real-time bidirectional communication between clients and threads. It enables:
  • Receiving message chunks as they’re generated
  • Getting complete message updates
  • Tracking token usage and costs
  • Receiving custom events from tools
  • Keeping connections alive with ping/pong

Connecting

Connection Parameters

string
required
The unique thread identifier to stream
string
Optional user identifier for authorization and tracking

Server → Client Messages

message_data

Sent when a complete message is created or updated.
Message
Complete message object with all fields

message_chunk

Sent during streaming as tokens are generated. Use to display text in real-time.
string
ID of the message being streamed
string
Text chunk to append to the message
number
Sequence number for ordering chunks
Append chunks in socket order and keep provisional buffers until their terminal message_data records arrive. Do not erase already-painted text merely because a provider retry or reconnect exposes another messageId; retain it as a reconciliation alias (or as a separate keyed draft). The final message_data event contains the authoritative complete content.

telemetry

Sent after each LLM request with usage statistics.
string
Model used for the request
number
Number of input tokens consumed
number
Number of output tokens generated
number
Estimated cost in USD
number
Number of cached tokens (if applicable)

stop

Sent when a turn or conversation stops.
string
Why execution stopped:
  • response - AI returned text response
  • tool - Stop tool was called
  • max_turns - Turn limit reached
  • end_conversation - Conversation ended
string
Which side stopped: "a" or "b"

error

Sent when an error occurs during processing.
string
Human-readable error description
string
Error code for programmatic handling:
  • RATE_LIMIT - Provider rate limit
  • AUTH_ERROR - Authentication failed
  • MODEL_ERROR - Model returned error
  • TOOL_ERROR - Tool execution failed
  • TIMEOUT - Request timed out

custom

Custom events emitted via emitThreadEvent() in tools.
string
Custom event name defined by tool
unknown
Event payload (any JSON-serializable data)

pong

Response to client ping for connection health.
number
Server timestamp in milliseconds

Client → Server Messages

ping

Send periodically to keep the connection alive.
Server responds with pong.
Send a ping every 30 seconds to prevent connection timeout on Cloudflare Durable Objects.

sync

Request missed messages after reconnection.
string
required
ID of the last message received. Server sends all messages created after this.

Handling Messages

Reconnection Strategy

Implement automatic reconnection for reliability:

React Integration

Use the @standardagents/react package for built-in WebSocket handling:
See React Integration for full documentation.