Streaming
Updated
- Connect
- Use an SDK
- Message flow
- Client messages
- Server messages
- Shared fields
- Ordering and sequence
- Limits
- Errors and close codes
- Manual WebSocket
A stream sends changing state from your server and returns structured decisions over a WebSocket connection. Append new text or array items, replace state, and read the decisions that cover accepted changes.
For a single decision over HTTP as events, read Event stream reply.
Connect
Streaming is server to server only. Use Node 22 or later or Python 3. Browsers cannot set the Authorization header on the WebSocket upgrade. Keep the key in the server process.
Use the API origin with WSS and /v1/system-zero/stream. Authenticate on the upgrade with Authorization: Bearer <key>. Request only system-zero.stream.v1 in Sec-WebSocket-Protocol.
Use text JSON messages. Send no browser Origin, URL credential, cookie authentication, compression, binary data, query or trailing slash.
Use an SDK
Install an SDK, then read the key from the server environment. Both languages export SystemZeroStreamClient.
TypeScript requires upgrade headers. Supply a server constructor through WebSocket if native support is missing.
Await each mutation for its acknowledgement. Read decision for the reply and request ID, and error for its envelope. The clients answer heartbeats and pace changes using the advertised limits.
Reconnect restores accepted state with its idempotency keys. It does not resume a durable stream. Read possibly_decided when a lost connection leaves an unknown outcome. Reopening does not prove the work was uncounted.
Message flow
RightNow sends hello. You send start within 10 seconds of hello. RightNow sends ack. You send append_text. RightNow sends ack. You send decide. RightNow sends scheduled. RightNow sends ping. You send pong within 10 seconds of ping. RightNow sends decision. RightNow sends goodbye. A ping arrives every 30 seconds. An idle stream closes after 120 seconds. A stream lives 60 minutes.
Read hello and its limits, then send start before its deadline. Questions, routing, scheduling and the cache key stay fixed for the connection. Read whole decisions in a stream with hello, ack, decision, error frames. The stream does not send individual tokens.
Use append_text for string state, append_items for whole array elements, or replace for a complete new state. Objects are replaced whole; field-level patches are not supported. append_attachments changes only the inline attachment list. Each accepted mutation receives ack and advances state_version.
With on_change, accepted updates may be combined into a decision. With on_request, send decide(state_version) for the current accepted version and read scheduled. A decision embeds the ordinary HTTP reply. Each decision reports its snapshot's state_version and covered_event_ids. Check state_version before using the answer because newer state may already have arrived. Use on_change (the default) when you want an answer for every state change: the decision starts the moment the event is accepted. on_request starts a decision only when you ask for one, after the acknowledgement of your event, so each decision adds one network round trip from your client. measured 2026-11-10 02:42Z, three sessions from a US west-coast client: 80-150 ms from event accepted to decision started in on_request mode; the server's own step in between took 2-5 ms.
Answer each JSON ping with pong and the same nonce within 10000 milliseconds of the completed ping write. Native WebSocket controls are separate. A missed pong closes with 1013 and reason slow_reader.
Idle = 120 seconds with no client application message and no decide in flight; the clock restarts when a decide settles; heartbeats do not count. The client ends normally with WebSocket close code 1000. There is no client goodbye or client ping JSON message.
A server close can send goodbye first. Durable stream resume is unsupported. Reconnect and replay the full desired state with the same per-state idempotency key and byte-identical request. Successful keys have a 24-hour answer window and retain an identity tombstone without request or answer content for 30 days after that window; after answer expiry, the same body returns 409 idempotency_replay_unavailable and a changed body returns 400 idempotency_key_reuse.
On a new connection the body is the same only if start carried your own prompt_cache_key. Without one the server uses the new stream_id, and the key returns 400 idempotency_key_reuse. If a decision did not arrive before the connection closed, the same key and body can return the completed answer. Replay availability is not guaranteed. Read Idempotency. Without a key, a decision lost in transit cannot be retrieved. Send the state again; that starts new work.
Never reconnect after 1002, 1003, 1007, 1008 or 1009. Reconnect at once after 1001. Wait 500 milliseconds after 1012. After 1013 wait the hint, 300 seconds for maintenance or 1 second for a slow reader, else back off. Back off after 1011. After 1000 reconnect when you need the stream. Then replay the full state with the same keys and read possibly_decided.
Update an image
Save prepared PNG files as shelf-before.png and shelf-after.png. This Python example prints four client messages. Send them individually over your authenticated server connection after hello.
Send start, wait for ack at version 1, then send decide. Read that decision before sending replace. Wait for its ack at version 2, then request the second decision. Keep answering heartbeats and honoring the advertised limits.
Replacement supplies the complete image list. Omit it to retain the previous list; use append_attachments only when you want to keep existing images and add more. All retained files must still fit the ordinary count and shared byte limits.
Each new answered snapshot charges every image it contains, including retained images: USD 0.003 per image, USD 0.0001 per answered question and USD 0.00001 per answered call, with residual text charged separately. Each of these two snapshots includes one image fee if answered.
An acknowledgement is not a billable answer. Media-token cache hits do not remove file fees. There is no durable media cache. This example requests separate snapshots; automatic decisions may combine updates.
Client messages
start
- idempotency_key
- Optional. string. Minimum length: 1.
- type
- Required. "start"
- event_id
- Required. "evt_1"
- request
- Required. Start request. No extra fields.
- decision_mode
- Required. "on_change" or "on_request"
- min_interval_ms
- Optional. integer. Minimum: 0. Maximum: 60000. Default: 0.
append_text
- idempotency_key
- Optional. string. Minimum length: 1.
- type
- Required. "append_text"
- event_id
- Required. string
- text
- Required. string. Minimum length: 1.
append_items
- idempotency_key
- Optional. string. Minimum length: 1.
- type
- Required. "append_items"
- event_id
- Required. string
- items
- Required. array of JSON value. Minimum items: 1. Maximum items: 4096.
append_attachments
- idempotency_key
- Optional. string. Minimum length: 1.
- type
- Required. "append_attachments"
- event_id
- Required. string
- attachments
- Required. Inline attachment array. Minimum items: 1.
replace
- idempotency_key
- Optional. string. Minimum length: 1.
- type
- Required. "replace"
- event_id
- Required. string
- state
- Required. string, object or array
- attachments
- Optional. Inline attachment array. Maximum items: 16.
decide
- type
- Required. "decide"
- state_version
- Required. integer. Minimum: 1. Maximum: 100000.
pong
- type
- Required. "pong"
- nonce
- Required. integer. Minimum: 1. Maximum: 9007199254740991.
Server messages
hello
- type
- Required. "hello"
- stream_id
- Required. string
- seq
- Required. integer. Minimum: 1. Maximum: 9007199254740991.
- protocol
- Required. "system-zero.stream.v1"
- request_id
- Required. string. Minimum length: 1. Maximum length: 128.
- limits
- Required. Advertised limits. No extra fields.
- rate_limit
- Required. Rate limit snapshot or null
ack
- type
- Required. "ack"
- stream_id
- Required. string
- seq
- Required. integer. Minimum: 1. Maximum: 9007199254740991.
- event_id
- Required. string
- state_version
- Required. integer. Minimum: 1. Maximum: 100000.
- state_bytes
- Required. integer. Minimum: 0.
- synthesized_body_bytes
- Required. integer. Minimum: 0. Maximum: 3665920.
- scheduled
- Required. boolean
scheduled
- type
- Required. "scheduled"
- stream_id
- Required. string
- seq
- Required. integer. Minimum: 1. Maximum: 9007199254740991.
- state_version
- Required. integer. Minimum: 1. Maximum: 100000.
- in_flight
- Required. boolean
ping
- type
- Required. "ping"
- stream_id
- Required. string
- seq
- Required. integer. Minimum: 1. Maximum: 9007199254740991.
- nonce
- Required. integer. Minimum: 1. Maximum: 9007199254740991.
decision
- type
- Required. "decision"
- stream_id
- Required. string
- seq
- Required. integer. Minimum: 1. Maximum: 9007199254740991.
- state_version
- Required. integer. Minimum: 1. Maximum: 100000.
- covered_event_ids
- Required. array of string. Maximum items: 4096. Items are unique.
- reply
- Required. HTTP decision reply. No extra fields.
- request_id
- Required. string. Minimum length: 1. Maximum length: 128.
- http_status
- Required. 200
- server_timing
- Required. string or null
- latency_ms
- Required. number or null. Minimum: 0.
- rate_limit
- Required. Rate limit snapshot or null
- queue_ms
- Required. number. Minimum: 0.
- decide_ms
- Required. number. Minimum: 0.
error
- type
- Required. "error"
- stream_id
- Required. string
- seq
- Required. integer. Minimum: 1. Maximum: 9007199254740991.
- phase
- Required. "protocol" or "decide" or "lifecycle"
- state_version
- Required. integer. Minimum: 0. Maximum: 100000.
- event_id
- Required. string or null
- covered_event_ids
- Required. array of string. Maximum items: 4096. Items are unique.
- http_status
- Required. integer. Minimum: 400. Maximum: 599.
- envelope
- Required. HTTP error envelope. No extra fields.
- retry
- Required. Retry fields. No extra fields.
- rate_limit
- Required. Rate limit snapshot or null
- server_timing
- Required. string or null
- fatal
- Required. boolean
goodbye
- type
- Required. "goodbye"
- stream_id
- Required. string
- seq
- Required. integer. Minimum: 1. Maximum: 9007199254740991.
- reason
- Required. "idle" or "max_lifetime" or "auth" or "policy" or "shutdown" or "maintenance" or "overload" or "slow_reader" or "lease_lost" or "store_unavailable" or null
- state_version
- Required. integer. Minimum: 0. Maximum: 100000.
- last_decision_state_version
- Required. integer. Minimum: 0. Maximum: 100000.
- resume
- Required. object. No extra fields.
Shared fields
State is a JSON object, array or string. Nested numbers, booleans and null remain JSON. Use compact keys and send large integers or exact decimals as numeric strings when precision matters. Supply your own units, coordinate frames and freshness data.
Each decision follows the ordinary decide API's validation, availability, billing and limits. Keep choice options and score levels within the model list's limits. A larger schema bound is not a serving promise. An ack accepts state into connection memory; it grants no input availability and confirms no decision, charge or durable save.
Question names use at most 64 ASCII letters, digits and underscores, starting with a letter or underscore. Attachment names use at most 64 ASCII letters, digits, underscores or hyphens, starting with a letter. Attachment names must be unique.
If start omits prompt_cache_key, the server uses stream_id. For byte-identical replay across connections, send an explicit stable prompt_cache_key.
A request policy goes in start.request.
Omitting replace.attachments retains the current list. An empty list removes every attachment. Text and array appends keep attachments unchanged. Inline data has no separate MIME field. Document files must be inline PDF data. PDF and video (preview) use the same limits as HTTP. Read Attachments. The stream does not take upload references.
Start request
- model
- Required. string. Minimum length: 1. Maximum length: 200.
- state
- Required. string, object or array
- questions
- Required. Named questions. Minimum fields: 1. Maximum fields: 1024.
- attachments
- Optional. Inline attachment array. Maximum items: 16.
- prompt_cache_key
- Optional. string. Minimum length: 1. Maximum length: 256.
- route
- Optional. "standard" or "committee". Held feature. "committee" is refused with 404 committee_not_available and is unbilled
- mode
- Optional. "standard" or "deliberate". Held feature. "deliberate" is refused with 404 deliberate_not_available and is unbilled
Boolean question
- type
- Required. "boolean"
- instructions
- Required. nonempty string, object or array
- criteria
- Optional. object. No extra fields.
Choice question
- type
- Required. "choice"
- instructions
- Required. nonempty string, object or array
- criteria
- Required. object with named entries of string or null. Minimum fields: 1. Maximum fields: 255. Maximum key length: 256.
Score question
- type
- Required. "score"
- instructions
- Required. nonempty string, object or array
- criteria
- Required. array of string. Minimum items: 2. Maximum items: 10. Items: Minimum length: 1. Items must be nonblank strings.
Inline data
- name
- Required. string
- type
- Required. "document" or "image" or "audio" or "video"
- data
- Required. string. Must begin with data:.
Advertised limits
- max_duration_ms
- Required. 3600000
- idle_timeout_ms
- Required. 120000
- start_timeout_ms
- Required. 10000
- heartbeat_interval_ms
- Required. 30000
- native_heartbeat_interval_ms
- Optional. 10000
- receipt_timeout_ms
- Optional. 240000
- max_control_queue_frames
- Optional. 2
- pong_timeout_ms
- Required. 10000
- max_message_bytes
- Required. 4194304
- max_decide_body_bytes
- Required. 3665920
- max_pending_events
- Required. 4096
- max_events
- Required. 100000
- max_events_per_second
- Required. 5
- max_send_queue_bytes
- Required. 4194304
- max_send_queue_frames
- Required. 64
- min_interval_ms
- Required. 0
- event_burst
- Required. 10
- max_messages_per_second
- Required. 40
- message_burst
- Required. 80
- max_streams_per_process
- Required. 16
- max_native_control_frames_per_second
- Required. 8
- native_control_frame_burst
- Required. 16
- max_native_control_bytes_per_second
- Required. 512
- native_control_byte_burst
- Required. 1024
Rate limit snapshot
- limit
- Required. integer. Minimum: 0.
- remaining
- Required. integer. Minimum: 0.
- reset_epoch_seconds
- Required. integer. Minimum: 0.
HTTP decision reply
- id
- Required. string
- model
- Required. string
- served_by
- Required. string
- created
- Required. integer. Minimum: 0. Maximum: 9007199254740991.
- context
- Required. object. No extra fields.
- answers
- Required. object
- usage
- Required. object. No extra fields.
HTTP error envelope
- error
- Required. HTTP error. No extra fields.
- request_id
- Required. string
HTTP error
- message
- Required. string
- type
- Required. "invalid_request_error" or "authentication_error" or "permission_error" or "not_found_error" or "rate_limit_error" or "overloaded_error" or "api_error" or "server_error"
- param
- Required. string or null
- code
- Required. string or null
- retryable
- Required. boolean
- details
- Optional. object with named entries of JSON value
Retry fields
- should_retry
- Required. boolean
- retry_after_ms
- Required. number or null. Minimum: 0.
- retry_after
- Required. string or null
Named questions
- <question name>
- Required. Boolean question, choice question or score question. Minimum fields: 1. Maximum fields: 1024.
Inline attachment array
- <item>
- Optional. Inline data. Maximum items: 16.
goodbye.resume fields
- supported
- Required. false
- action
- Required. "reconnect_and_replay"
- retry_after_ms
- Required. integer. Minimum: 0.
Boolean question.criteria fields
- true
- Optional. string
- false
- Optional. string
HTTP decision reply.context fields
- route
- Required. "direct" or "long"
- tokens
- Required. integer. Minimum: 0. Maximum: 9007199254740991.
HTTP decision reply.usage fields
- input_tokens
- Required. integer. Minimum: 0. Maximum: 9007199254740991.
- cached_input_tokens
- Required. integer. Minimum: 0. Maximum: 9007199254740991.
- media_tokens
- Required. integer. Minimum: 0. Maximum: 9007199254740991.
- long_input_tokens
- Required. integer. Minimum: 0. Maximum: 9007199254740991.
- media_items
- Required. integer. Minimum: 0. Maximum: 9007199254740991.
- thought_tokens
- Required. integer. Minimum: 0. Maximum: 9007199254740991.
Ordering and sequence
Each accepted mutation receives ack and increments state_version once.
IDs are precisely evt_1, evt_2 and so on; a rejected update leaves the next ID available for a corrected retry.
Text appends concatenate exactly, arrays append complete values, and replace changes the complete string, array or object state.
Use replace to change an object state; attachments can change independently.
An ack means accepted state in gateway memory, not a decision, bill or durable save.
Every accepted mutation, including an attachment append or replacement, creates a new version with its own supplied key; an unchanged version retains its key.
Rejected mutations leave the previous key intact; an unkeyed mutation clears it.
The gateway maintains one decide in flight and one pending intent.
Dense changes coalesce into the latest state.
In on_request mode, decide must name the current accepted version; its snapshot is chosen when dispatch becomes eligible.
state_version is its cumulative coverage watermark; covered_event_ids names only the mutation batch newly frozen into that dispatch.
An ack precedes any decision that covers it.
Only a successful decision, never an ack or error, resolves an event-to-decision observation.
During shutdown, valid mutations are applied and acknowledged with scheduled: false; they never start another decide on this connection.
Server seq starts at 1 and increases for every server message.
Idempotency keys contain 1 to 255 printable ASCII characters after trimming surrounding ASCII spaces. An unkeyed mutation clears the previous key.
Reconnect creates a new stream with new event numbering.
Replay the full desired state in start, then append only mutations absent from that replay.
Use full-jitter reconnect backoff, honoring any longer server retry delay.
Heartbeats neither bill inference nor extend idle/lifetime.
Limits
Read the limits advertised in hello. Stream limits do not enlarge ordinary HTTP limits. Idle time excludes heartbeats and time spent waiting for a decision. Account stream capacity is one stream socket across machines, for free, credit-only and paid accounts.
receipt_timeout_ms is a legacy wire hint, not a complete receipt deadline. Continue checking peer liveness and receipt inactivity. Both clients use a receipt deadline of 3818000 ms from command send; confirmed liveness can refresh inactivity timers but cannot extend it, and JSON-only EventTarget transports use a 3848000 ms heartbeat fallback without extending the receipt deadline.
- max_duration_ms
- 3600000 milliseconds
- idle_timeout_ms
- 120000 milliseconds
- start_timeout_ms
- 10000 milliseconds
- heartbeat_interval_ms
- 30000 milliseconds
- native_heartbeat_interval_ms
- 10000 milliseconds
- receipt_timeout_ms
- 240000 milliseconds
- max_control_queue_frames
- 2 frames
- pong_timeout_ms
- 10000 milliseconds
- max_message_bytes
- 4194304 UTF-8 bytes
- max_decide_body_bytes
- 3665920 UTF-8 bytes
- max_pending_events
- 4096 events
- max_events
- 100000 events
- max_events_per_second
- 5 events per second
- max_send_queue_bytes
- 4194304 bytes
- max_send_queue_frames
- 64 frames
- min_interval_ms
- 0 milliseconds
- event_burst
- 10 events
- max_messages_per_second
- 40 messages per second
- message_burst
- 80 messages
- max_streams_per_process
- 16 streams
- max_native_control_frames_per_second
- 8 frames per second
- native_control_frame_burst
- 16 frames
- max_native_control_bytes_per_second
- 512 bytes per second
- native_control_byte_burst
- 1024 bytes
- maximum_decision_interval
- 60000 milliseconds
- mutation_burst
- 10 events
- application_message_rate
- 40 messages per second
- application_message_burst
- 80 messages
- max_pending_probes
- 2 probes
- write_progress_timeout
- 5000 milliseconds
- decision_settlement_timeout
- 5000 milliseconds
- close_handshake_timeout
- 5000 milliseconds
- consecutive_retryable_failures
- 3 failures per state version
- protocol_refusal_limit
- 3 refusals per window
- protocol_refusal_window
- 60000 milliseconds
- account_decision_burst
- 5 starts
- account_decision_ceiling
- 60 starts per minute
Errors and close codes
Read error.envelope and error.retry. Decision failures keep the ordinary HTTP status and error envelope. A rejected mutation leaves state, version and its next event ID unchanged. A fatal error requires a following close.
| Code | Status | Meaning |
|---|---|---|
stream_invalid_message | 400 | The stream message is invalid. |
stream_state_type | 400 | The stream state does not support this mutation. |
stream_event_order | 409 | The stream event does not name the next state version. |
stream_version_mismatch | 409 | The requested stream version is not current. |
stream_not_started | 400 | The stream has not started. |
stream_already_started | 409 | The stream has already started. |
stream_upgrade_required | 426 | This endpoint accepts WebSocket connections only. |
stream_limit_exceeded | 429 | The stream limit has been reached. |
invalid_request | 400 | Invalid attachment list, item or repeated name. |
media_unsupported | 400 | An attachment count exceeds its kind limit. |
payload_too_large | 413 | The complete decision request exceeds its byte limit. |
limiter_unavailable | 503 | Rate-limit state cannot be read. Honor the retry delay. |
service_unavailable | 503 | Opening capacity is full or the server is stopping. Honor the retry delay. |
In on_change mode, a 429 or 503 other than maintenance, or HTTP 409 idempotency_conflict, with retry permitted by the HTTP response schedules the latest state again after its Retry-After delay.
The gateway emits an error frame for each failure and stops automatic retries at three consecutive retryable failures for the same version.
The third error has fatal: true and retains the HTTP retry fields.
It is followed by goodbye with reason overload, the Retry-After delay in resume.retry_after_ms, and close 1013 so the client reconnects and replays.
The same fatal outcome applies immediately when the next automatic retry would reach or exceed the stream's remaining lifetime.
On_request never retries itself.
Retaining the key does not guarantee recovery of an original reply after a dropped connection.
Retry-After-Ms takes precedence over Retry-After.
| Code | Condition | Reason | Reconnect |
|---|---|---|---|
| 1000 | Normal client close or application idle for 120 seconds | idle | lazy |
| 1001 | Maximum lifetime of one hour | max_lifetime | immediate |
| 1002 | RFC 6455 framing protocol error | null | never |
| 1003 | Binary data frame | null | never |
| 1007 | Invalid UTF-8 or malformed JSON | null | never |
| 1008 | Refused key/account or 404 unknown_endpoint, start timeout, message/event-rate, native-control frame/byte budget or total-event limit, 3 protocol refusals within 60 seconds (outside shutdown drain), or WebSocket fragment/chunk policy | auth, policy | never |
| 1009 | WebSocket data-frame/message size, queued incoming bytes, or pending-event count bound exceeded | null | never |
| 1011 | Unexpected internal failure | null | backoff |
| 1012 | Server restart | shutdown | after hint. retry after ms: 500 milliseconds |
| 1013 | Maintenance, overload, slow reader (including a missed native or JSON pong), lost/unrenewable lease or unavailable store | maintenance, overload, slow_reader, lease_lost, store_unavailable | after hint or backoff. maintenance retry after ms: 300000 milliseconds. slow reader retry after ms: 1000 milliseconds |
Honor goodbye. and any longer server retry delay. Use reconnect backoff. A goodbye is best effort; a transport close can arrive without it.
The server never sends 1006; clients can observe it after abrupt loss and reconnect with backoff.
Manual WebSocket
Illustrative JSON messages from a server runtime. Replace MODEL_ID and SERVED_BY with values from authenticated discovery. Timing, counts and probabilities illustrate the reply shape. In this exchange the server restarts after the decision, sends goodbye, then closes with code 1012. Wait at least 500 milliseconds before reconnecting.