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
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 responsetool- Stop tool was calledmax_turns- Turn limit reachedend_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 limitAUTH_ERROR- Authentication failedMODEL_ERROR- Model returned errorTOOL_ERROR- Tool execution failedTIMEOUT- Request timed out
custom
Custom events emitted viaemitThreadEvent() 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.pong.
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: