deniz.in

Markets

Weather

Loading weather

· via dev.to (home feed)

OpenAI's Two WebSocket Modes Explained: Responses API for Agents, Realtime for Voice

A developer guide on dev.to explains OpenAI's two WebSocket API modes: the Responses API for tool-heavy agents and the Realtime API for voice, and when each beats plain HTTP.

OpenAI's Two WebSocket Modes Explained: Responses API for Agents, Realtime for Voice

OpenAI's APIs are not limited to request-response HTTP. The company also exposes two WebSocket modes, one attached to the Responses API for agent-style workloads and another forming the Realtime API for speech, and the choice between them can be worth double-digit performance gains. A hands-on guide published on dev.to walks through both, reporting that agent workflows making twenty or more sequential tool calls run up to roughly 40% faster end-to-end when moved onto a persistent socket.

The overhead HTTP imposes

Each plain HTTP call to OpenAI carries costs that repeat even within a single session: opening a connection, authenticating, and sending conversation context the server has effectively already seen. According to the dev.to guide, that fixed cost lands on every tool call an agent makes and on every chunk exchanged in a voice session.

A WebSocket connection removes it. The client performs one HTTP upgrade handshake, then holds a single long-lived, bidirectional connection. The server keeps session state for that connection, and the two sides exchange discrete JSON events rather than matched request-response pairs.

Responses API mode: for tool-calling agents

The Responses API WebSocket endpoint targets agentic workflows, such as coding assistants, orchestration systems, and autonomous agents, that chain many tool calls in a row.

The mechanism is a connection-local in-memory cache holding the most recent response. To continue a turn, the client sends only a reference to the last response plus the new input items, such as tool results or follow-up messages, and the server reuses the cached state instead of reprocessing the full history. Combined with the eliminated per-request setup, OpenAI's figures, as cited in the guide, put workflows with twenty or more tool calls at up to about 40% faster than their HTTP equivalents.

The guide also notes compatibility with both Zero Data Retention and store=false configurations, which keeps the mode viable for privacy-sensitive deployments.

Realtime API mode: for speech

The second mode is built for low-latency, speech-to-speech applications. Audio chunks stream in as they are captured; transcriptions and generated audio stream back, with text input and output supported alongside for hybrid interfaces.

Two features do most of the work for natural conversation. Interruption handling stops generation when the user starts speaking again, and semantic voice activity detection distinguishes a finished utterance from a mere pause. The mode gives direct access to GPT-4o's native multimodal abilities across audio and text, and it is designed for latencies under 500ms, which the guide presents as the target for real-time voice.

When HTTP still wins

The guide is explicit that a persistent socket is not always the better option. WebSocket pays off once a workflow reaches roughly ten or more sequential tool calls, or when the application is latency-sensitive or needs the server to push updates without polling.

Below that threshold, HTTP remains simpler to implement and debug, and it behaves well behind load balancers and proxies. Stateless workloads gain nothing from session state, and environments such as serverless functions or certain proxies may not permit long-lived connections at all.

The trade-offs are real on the WebSocket side too. The guide's comparison rates memory overhead as medium for Responses mode and medium-to-high for Realtime sessions, with implementation complexity similarly medium to high. Connection setup costs roughly 100 to 300ms either way, but HTTP pays it on every request while a WebSocket client pays it once, numbers the guide attributes to OpenAI's documentation and community testing rather than independent benchmarks.

Why it matters

Agentic loops and voice interfaces are exactly the workloads where per-call overhead compounds: an agent making dozens of tool calls, or a voice assistant that must respond in under half a second, cannot afford to renegotiate a connection each turn. Framing transport choice as a measurable performance lever, with a reported ceiling of about 40% on agent workflows, gives developers a concrete decision rule. Occasional, stateless calls stay on HTTP; anything with high call volume or streaming latency moves to a socket.

The caveat is provenance. The headline numbers come from OpenAI's own documentation and community testing relayed in a single blog post, so teams adopting either mode should validate the gains against their own workloads before committing to the extra implementation and infrastructure complexity.

  • #openai
  • #websocket
  • #api
  • #realtime
  • #latency

Related posts