Introduction
Establish low-latency real-time voice sessions over WebSocket, compatible with the OpenAI Realtime API. After connecting, exchange audio and text via the event protocol—suitable for voice assistants, live translation, and similar use cases. Base URL:wss://api.gravitex.ai/v1/realtime
You must use the WebSocket protocol, not a standard HTTP POST.
model must be passed as a query parameter; otherwise the gateway returns 400.Authentication
Include the following headers when establishing the WebSocket connection:string
required
Bearer token, e.g.
Bearer sk-xxxxxxxxxxstring
required
Must be
realtime=v1Connection
string
required
Realtime model ID, e.g.
gpt-realtime-1.5, gpt-realtime-2. Must match a model available to your API key.session.created; send session.update to complete session configuration.
Session configuration
After connecting, sendsession.update to set instructions, output modalities, and audio format:
Available voices (when
output_modalities includes audio):
alloy · ash · ballad · coral · echo · sage · shimmer · verse · marin
Wait for session.updated before starting the conversation.
Sending messages
Each turn has two steps:conversation.item.create— write the user messageresponse.create— trigger model generation
Text input
Audio input
Recordings must be PCM16 raw bytes (no WAV header), Base64-encoded:Receiving responses
Common server events:
Audio delta fields may be
audio or delta; clients should handle both.
Usage
Eachresponse.done returns usage for that Response only, not session cumulative totals. Official docs: “The tokens used for a Response can be read from the response.done event”. For multi-turn sessions, track both per-turn and cumulative usage.
Example usage structure:
Field reference
- output.text_tokens: Even with audio output, automatic transcript generates text tokens—this is normal billing. See OpenAI Realtime costs.
- input.cached_tokens: A subset of
input_tokens, not additive. OpenAI returnscached_tokens_details(text/audio split); when upstream (e.g. Azure GA) does not, the gateway estimates by text/audio input ratio. - Multi-turn cost: Each Response sends the full conversation history, so
input_tokensgrow over time; earlier turns often hit prompt cache with high discounts (e.g.gpt-realtime-2audio cache 32/1M = 98.75% off). Official docs: “turns later in the session will be more expensive”.
Full Python example
Dependencies:pip install websockets pyaudio
test_realtime.py.
FAQ
- 400 on connect: Check that the URL includes the
?model=...query parameter. - 401 on connect: Invalid or unauthorized API key; call
GET /v1/modelsto verify key and model availability. - No response after send: Confirm you received
session.updatedand sentresponse.create. - Disconnect after 500: Upstream errors close the WebSocket—reconnect.
- No audio: Confirm 24 kHz PCM16 mono and correct Base64 decoding before playback.
