Streaming Responses
CLI Proxy API supports two methods for receiving real-time streaming responses:- Server-Sent Events (SSE): Standard HTTP streaming via the
/v1/chat/completionsendpoint - WebSocket: Bidirectional streaming via the
/v1/responsesendpoint (OpenAI Responses format)
Server-Sent Events (SSE)
Enabling Streaming
Setstream: true in your chat completions request:
SSE Response Format
The server sends chunks asdata: prefixed JSON objects:
Chunk Structure
string
Unique identifier for the completion (same across all chunks).
string
Always
chat.completion.chunk.integer
Unix timestamp when the completion was created.
string
The model used for generation.
array
Array of completion choices (typically one element).
integer
Choice index (0 for single-choice requests).
object
string
Why generation stopped.
null for intermediate chunks, set in final chunk:stop: Natural completionlength: Max tokens reachedtool_calls: Model called a functioncontent_filter: Content filtered
object
Token usage statistics (only in final chunk with
finish_reason).SSE Headers
Keep-Alive Behavior
The SSE connection uses chunked transfer encoding and flushes after each chunk. The server automatically handles:- Immediate Flush: Each chunk is sent immediately to minimize latency
- Connection Management: Graceful handling of client disconnects
- Error Streaming: Errors during streaming are sent as error chunks (not HTTP errors)
WebSocket Streaming
For more advanced use cases, CLI Proxy API provides a WebSocket endpoint that supports:- Bidirectional communication
- Multi-turn conversations without reconnecting
- Incremental input appending
- Session management
WebSocket Endpoint
wss:// for secure WebSocket connections over TLS.
Authentication
WebSocket authentication is controlled by thewebsocket_auth configuration setting:
Request Format
Send JSON text messages with one of these types:1. response.create
Start a new conversation:2. response.append
Continue an existing conversation:3. Incremental Input (Advanced)
For providers that support WebSocket v2 mode (like Codex), useprevious_response_id:
Response Events
The server sends JSON text messages with different event types:response.created
Content Deltas
Streaming content chunks:response.completed
error
WebSocket Implementation
Fromopenai_responses_websocket.go:49-194:
- Session Pinning: Once a provider is selected for the first request, subsequent requests in the session use the same provider
- Error Handling: Errors are sent as WebSocket messages, not connection closes
- Automatic Reconnect: Clients can reconnect with the same
x-codex-turn-stateheader to resume a session
JavaScript Example
Python Example (SSE)
Configuration
Disable WebSocket Authentication
For development or internal networks:Keep-Alive Settings
For long-running streams, the proxy includes keep-alive mechanisms:- SSE: Automatic flush after each chunk
- WebSocket: Ping/pong frames handled by underlying gorilla/websocket library
- HTTP Timeouts: Configurable via reverse proxy (Nginx, etc.)
Error Handling
SSE Errors
Errors during SSE streaming are sent as error chunks:WebSocket Errors
WebSocket errors are sent aserror event messages:
Best Practices
- Connection Reuse: For multiple requests, use WebSocket instead of opening new SSE connections
- Timeout Handling: Implement client-side timeouts for stalled streams
- Graceful Degradation: Fall back to non-streaming if streaming fails
- Buffer Management: Process chunks immediately to avoid memory buildup
- Error Recovery: Implement exponential backoff for reconnection attempts
The streaming implementation automatically handles provider-specific quirks and normalizes responses to the OpenAI format. See
sdk/api/handlers/openai/openai_handlers.go for the SSE implementation and openai_responses_websocket.go for WebSocket handling.