Learning resourcesReviewed · xAI

xAI Responses API WebSocket Mode (documentation)

This xAI documentation page explains WebSocket mode for the Responses API to developers building AI agents with many sequential tool calls. The main catch is that the up to ~20% latency gain is xAI's own internal benchmark, and the in-memory chaining only works on the open connection, which the server closes after 59 minutes. The page explains the response.create message flow, warmups with generate: false, previous_response_id chaining, serial turn handling, reconnect strategies and two WebSocket-specific errors.

Researched 15 Nov 2026

In short.

This is xAI's documentation page on running its Responses API over a single long-lived WebSocket, written for developers who build agent loops with many tool calls; the main catch is that the up to ~20% latency improvement is xAI's own internal benchmark and the fast path depends on one open connection that the server closes after 59 minutes. Technically, the client connects to [wss://api.x.ai/v1/responses], sends response.create messages, and after the first turn sends only new input items plus previous_response_id. The server keeps the latest response state in a per-connection in-memory cache, which is why the mode works with store=false and Zero Data Retention. The page also documents warmups (generate: false), serial processing of turns on one socket, reconnect paths, and the errors previous_response_not_found and websocket_connection_limit_reached. Nothing here was run or tested; pricing, rate limits, SDK support and licence terms are not on the page.

Researched and written by an AI model and checked by a second AI model. How it works

This report covers only the complete selected page at [https://docs.x.ai/developers/advanced-api-usage/websocket-mode.md]. The pages it links to (Streaming, Function Calling and the Responses API reference) were not read, so their content is excluded. The page carries no licence or terms notice. Nothing was executed: no WebSocket connection was opened, no code snippet was run, and the latency claim, error behaviour and 59-minute limit are reported as the page states them, not as verified.

The overview

What this resource is

This is a single documentation page titled WebSocket Mode, filed under Advanced API Usage in xAI's developer docs, at [https://docs.x.ai/developers/advanced-api-usage/websocket-mode.md]. It describes an alternative transport for xAI's Responses API: instead of opening a new HTTP request for each turn, a client keeps one WebSocket open to /v1/responses and sends each turn as a message on it.

It is a guide to an API feature, not a library, product or model. The evidence names no author or version for the page itself. The code examples use the model name grok-4.7.

Overall assessment

For its narrow purpose the page is concrete and usable. It gives the endpoint, the authentication header, the message type, the fields to send on follow-up turns, the cache behaviour, the connection limits, the reconnect options and two error payloads. A developer can build a first client from it without leaving the page.

The gaps are the usual ones for vendor documentation. The performance claim is the vendor's own and cannot be checked from the page. Pricing, rate limits, idle or keepalive behaviour and the full list of streaming event types are not covered here. Nothing was executed for this review, so every behaviour described below is what the page states, not something confirmed by a run.

Who it is for

The page says WebSocket mode is aimed at agentic workloads with many sequential tool calls, and names coding agents and orchestration loops that go back and forth with the model dozens of times. It suits developers who already use, or plan to use, the xAI Responses API and want to cut per-turn overhead.

The page's own reasoning is that the savings add up over long rollouts. The page targets long sequences of tool calls; latency benefits for short exchanges are not quantified. Beginners will need to be comfortable with API keys, JSON messages and a WebSocket client library; the page does not teach those basics.

What it lets you do

Per the page, you can run a multi-turn, tool-using conversation over one socket. After the first response, each follow-up turn sends only the new input items (for example function_call_output items and the next user message) together with previous_response_id, rather than the whole conversation.

It works with Zero Data Retention and with store=false, because continuing from the latest response uses the per-connection in-memory cache and does not read persistent storage.

Warmups are supported. Sending response.create with generate: false prepares the request state (tools, instructions, system messages) without running the model. It returns no output but still emits a response ID that you can chain from later, so the real generation turn starts faster.

The event types and ordering are described as identical to the existing Responses streaming format. For parallel turns you open several connections.

Using it

What you need

You need access to the xAI API and an API key. The examples read it from the XAI_API_KEY environment variable and send it as an Authorization: Bearer header during the WebSocket upgrade. You also need a WebSocket client. The Python example uses create_connection from the websocket package, and the JavaScript example uses the ws package. The examples are labelled as being without an SDK.

The page does not say which account tiers, regions or models support WebSocket mode. It does not say whether an official xAI SDK wraps it. These points are unknown from this page.

How it works

After the WebSocket upgrade, the client starts every turn by sending a response.create message. Its body has the same shape as the Responses create body, minus transport-only fields such as stream and background, because responses are always streamed back as events on the socket.

Chaining uses previous_response_id as it does over HTTP, with an extra shortcut. Each open connection holds the state of its most recent response in a per-connection cache, so continuing from that response avoids storage entirely. If you reference an older ID that is no longer in the cache, then with store=true the server may rehydrate it from persisted state (losing the latency benefit). With store=false or ZDR there is no fallback, and the turn fails with previous_response_not_found.

A turn that fails with a 4xx or 5xx evicts its previous_response_id from the cache, so a retry does not continue from broken state. One connection processes turns serially: a second response.create sent while one is in flight queues instead of running in parallel.

Setup steps

From the page, the setup is: obtain an xAI API key and expose it as XAI_API_KEY; install a WebSocket client; open [wss://api.x.ai/v1/responses] with the Authorization header; once the upgrade succeeds, send a response.create JSON message with the model, input items and tools; read the streamed events; then send follow-up response.create messages that carry previous_response_id and only the new input.

The page does not demonstrate how to extract the response ID from the event stream. Streaming and the Responses API reference are listed on the page as related guides, alongside Function Calling; their contents are outside the supplied evidence. The JavaScript snippet in the captured text shows backslash-escaped backticks in its template literal. That looks like a rendering artifact, and you would write a normal template literal in real code.

Worked example from the page

The first turn sends a message like {type: response.create, model: grok-4.7, store: false, input: [a user message with an input_text item reading Find fizz_buzz()], tools: []}. The follow-up turn sends the same type and model, store: false, previous_response_id set to resp_123, and an input array holding a function_call_output item (with call_id and output) plus a new user message reading Now optimize it.

Both examples are given in Python and JavaScript. The tools list is empty in both, so they show the message shape and chaining only. The initial JavaScript example parses each incoming message with JSON.parse(data.toString()) and logs it to the console. Extracting the response ID, handling specific event types and running a complete tool-calling loop are not shown. These are the page's examples; they were not run for this review.

Assessment & evidence

Limitations and risks

Documented limits: one connection handles turns serially, with extra requests queued; a connection stays open for at most 59 minutes, after which the server sends websocket_connection_limit_reached and closes it; and only the most recent response per connection is held in the fast cache.

With store=false or ZDR, losing the connection or referencing an older response ID means the chain cannot be resumed. You must restart with the full input context. A client therefore has to keep enough of its own conversation state to rebuild context. This is a design cost of the mode, and the page spells out the recovery path.

Unknowns: the benchmark method and workloads behind the 20% figure, idle timeouts, keepalive or ping behaviour, maximum message size, concurrent-connection limits, rate limits, and the full event schema. None of these is covered on the page. Behaviour was not tested.

Why this assessment

The page is specific. It gives exact error codes and payload shapes, a precise connection cap, and a clear statement of which fallback applies in which storage mode. That makes it useful as a working reference.

The ~20% figure is labelled by the page as xAI's internal measurement on agentic workloads with many tool calls, compared with repeated HTTP requests that use the same previous_response_id chaining. It is a vendor claim with no method shown, so treat it as a reason to measure your own workload, not as an expected gain. The privacy point (works with ZDR and store=false) follows directly from the documented design, in which the continuation state lives in memory on the open socket.

Alternatives

The page compares WebSocket mode only with repeated HTTP requests to the same endpoint that chain with previous_response_id. That remains the simpler option, and it is the baseline for the latency claim. Resending the full conversation each turn is the other implied option, and it is the pattern that WebSocket mode's new-items-only turns avoid.

Comparisons with other vendors' streaming or realtime APIs are not made on the page and were not assessed. For parallelism within WebSocket mode, the page's answer is to open several connections.

Evidence base

The source is xAI's WebSocket Mode page in the Advanced API Usage section of its developer documentation, at [https://docs.x.ai/developers/advanced-api-usage/websocket-mode.md]. The page contains prose on when the mode helps, opening a connection, warmups, continuing a run, chaining on the socket, connection limits, reconnecting and errors. It also has four code snippets (two in Python and two in JavaScript, all labelled as written without an SDK), two JSON error payloads, and links to three related guides: Streaming, Function Calling and the Responses API reference.

The examples and the documented behaviour are untested. No code was run, no connection was opened, and the latency benchmark was not repeated. Statements about behaviour are the page's statements.

Access & freshness

Costs

The page states no prices. Token costs, any extra charge or discount for WebSocket mode, and rate limits are unknown from this evidence. Using it requires an xAI API key, so usage is presumably tied to the user's xAI API account terms, but the page does not say so.

The page makes a performance claim, not a cost claim: sending only new input items rather than the full conversation reduces per-turn work, and xAI reports up to about 20% lower end-to-end latency in its internal benchmarks. Whether this lowers billed tokens is not stated.

Rights and licence

The captured page contains no licence statement, copyright notice or terms of use. Whether the page may be redistributed, whether the code snippets may be reused, and which xAI terms apply to API use are not established by this evidence and should be checked in xAI's own terms.

Freshness

The page was captured on 2026-11-15. The examples name grok-4.7 and use the /v1/responses endpoint; the page provides no publication date, update date or page revision. The page does not say whether other models support WebSocket mode. Since this is live vendor documentation for an API, limits such as the 59-minute cap, the error codes and the availability of the feature could change. Check the live page before relying on specifics.

Suggested next steps

If you run long agent loops on the xAI API, build a small prototype of both transports with the same prompts and tools and measure end-to-end latency yourself before switching.

Design for reconnects from the start. Keep your own copy of the conversation so you can restart a chain with full input when you see previous_response_not_found, when the 59-minute limit is reached, or after a dropped socket. Handle websocket_connection_limit_reached as an expected event, not a fault.

Do not send overlapping turns on one socket expecting parallelism; use several connections for that. Before production use, check xAI's separate pages for pricing, rate limits, the event schema and terms of use, as none of these is on this page.

Evidence for this report

selected-page.txtselected · checked 2026-11-15