Realtime
Speech-to-speech and text conversations over one WebSocket with the OpenAI Realtime API.
wss://api.ohmygpt.com/v1/realtime serves the OpenAI Realtime API: one WebSocket per conversation, carrying audio and text both ways. Client and server events follow OpenAI's Realtime format, so OpenAI's SDKs and examples work once you change the base URL, the key, and the model ID.
| Endpoint | wss://api.ohmygpt.com/v1/realtime?model={model} |
| Auth | Authorization: Bearer $OHMYGPT_API_KEY, or the WebSocket subprotocol below |
| Models | openai/gpt-realtime-2.1, openai/gpt-realtime-2.1-mini, and the other gpt-realtime models in GET /v1/models |
import asyncio
import os
from openai import AsyncOpenAI
client = AsyncOpenAI(api_key=os.environ["OHMYGPT_API_KEY"], base_url="https://api.ohmygpt.com/v1")
async def main():
async with client.realtime.connect(model="openai/gpt-realtime-2.1-mini") as conn:
await conn.session.update(session={"type": "realtime", "output_modalities": ["text"]})
await conn.conversation.item.create(item={
"type": "message",
"role": "user",
"content": [{"type": "input_text", "text": "Say hello in one short sentence."}],
})
await conn.response.create()
async for event in conn:
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)
elif event.type == "response.done":
print()
print(event.response.usage)
break
asyncio.run(main())Install the SDK with pip install "openai[realtime]". The script prints the reply, then the usage the response is billed on:
Hello there!
RealtimeResponseUsage(input_token_details=RealtimeResponseUsageInputTokenDetails(audio_tokens=0, cached_tokens=128, cached_tokens_details=CachedTokensDetails(audio_tokens=0, image_tokens=0, text_tokens=128), image_tokens=0, text_tokens=129), input_tokens=129, output_token_details=RealtimeResponseUsageOutputTokenDetails(audio_tokens=0, text_tokens=36, reasoning_tokens=29), output_tokens=36, total_tokens=165)Browsers
Browsers can't set headers on a WebSocket, so pass the key as a subprotocol, as OpenAI's browser clients do. The server selects realtime.
const ws = new WebSocket(
"wss://api.ohmygpt.com/v1/realtime?model=openai/gpt-realtime-2.1-mini",
["realtime", `openai-insecure-api-key.${apiKey}`],
);Anyone who can open the page can read the key, so give the page its own key with a spending limit. Ephemeral client secrets (POST /v1/realtime/client_secrets) and WebRTC calls (POST /v1/realtime/calls) return 501 for OhMyGPT keys; connect over the WebSocket instead.
Billing
Each response is billed when it finishes, from the usage in its response.done event, at the model's text, audio, and cached-input prices on its model page. Transcribing input audio (audio.input.transcription) is billed on its own, at the transcription model's price. If you close the connection while a response is being generated, OhMyGPT cancels the response and bills the tokens it used up to that point.
Limits
An account can have 10 sessions open at once. Opening an 11th closes it with code 1008:
websocket: close 1008 (policy violation): Too many concurrent sessions.A session that sends and receives no messages for 10 minutes closes within the next few minutes with code 1000 and Session closed after being idle.
When OhMyGPT deploys, it closes sessions between responses with code 1012 and Server restarting. Reconnect to continue. Responses that already finished are billed. Open a new session and send your session settings again.