The Concept
Realtime API Architecture
| Feature | Realtime API | Traditional Pipeline |
|---|
| Services | 1 (OpenAI) | 3 (ASR + LLM + TTS) |
| Latency | ~300-500ms | ~400-600ms |
| Audio format | PCM16 24kHz | Varies |
| Voice options | OpenAI voices | Any TTS provider |
| Customization | Limited | Full control |
| Cost | Per audio token ($32 in / $64 out per 1M, gpt-realtime-2.1) | Varies by provider |
| Barge-in | Built-in | Manual |
What's the main trade-off of using OpenAI's Realtime API vs a custom ASR+LLM+TTS pipeline?
The Realtime API collapses ASR+LLM+TTS into one service, which is simpler (one WebSocket, one provider) but less customizable. You can't use Deepgram for STT (better accuracy), Cartesia for TTS (lower latency), or Claude for LLM (different personality). You're locked into OpenAI's voices (alloy, echo, fable, etc.), GPT-6.1 Sol models, and their pricing ($0.06/min for audio). The latency is comparable (~300-500ms), and barge-in is built-in. The trade-off: simplicity and speed-to-market vs control and flexibility. Use Realtime API for prototypes and simple agents; use custom pipeline for production agents needing specific providers.
WebSocket Events
| Direction | Event | Purpose |
|---|
| Client → API | session.update | Configure voice, instructions, tools |
| Client → API | input_audio_buffer.append | Send audio chunk |
| Client → API | input_audio_buffer.commit | End of user speech |
| Client → API | response.create | Trigger response |
| API → Client | response.output_audio.delta | Audio chunk from agent |
| API → Client | response.output_audio.done | Agent finished speaking |
| API → Client | conversation.item.input_audio_transcription | ASR transcript |
| API → Client | response.function_call_arguments.done | Tool call result |
Session Configuration
import websockets
import json
async def create_realtime_session():
"""Create an OpenAI Realtime API session."""
url = "wss://api.openai.com/v1/realtime?model=gpt-realtime-2.1"
headers = {"Authorization": f"Bearer {OPENAI_API_KEY}"}
async with websockets.connect(url, additional_headers=headers) as ws:
# Configure session: voice and turn detection live under session.audio
session_config = {
"type": "session.update",
"session": {
"type": "realtime",
"instructions": "You are a helpful voice agent. Keep responses to 1-3 sentences.",
"audio": {
"input": {
"format": {"type": "audio/pcm", "rate": 24000},
"turn_detection": {
"type": "server_vad",
"threshold": 0.5,
"silence_duration_ms": 500
}
},
"output": {"format": {"type": "audio/pcm"}, "voice": "marin"}
},
"tools": [
{
"type": "function",
"name": "check_flight_status",
"description": "Check the status of a flight",
"parameters": {
"type": "object",
"properties": {
"flight_number": {"type": "string"}
},
"required": ["flight_number"]
}
}
]
}
}
await ws.send(json.dumps(session_config))
Server-Side VAD
The Realtime API has built-in server-side VAD with configurable threshold, set at session.audio.input.turn_detection. (It also offers semantic_vad, which decides end-of-turn from what was said rather than from silence; "turn_detection": null turns detection off for push-to-talk.)
"turn_detection": {
"type": "server_vad", # OpenAI handles VAD
"threshold": 0.5, # Sensitivity (0-1)
"silence_duration_ms": 500 # Endpointing silence threshold
}
Available Voices
The Realtime API's voices are alloy, ash, ballad, coral, echo, sage, shimmer, verse, marin and cedar. OpenAI recommends marin or cedar for the best quality. Pick by listening: play the same agent script through two or three of them with a few real callers before you commit, because how a voice lands depends on your audience and use case.
Handling Audio
async def handle_realtime_audio(ws):
"""Handle bidirectional audio with Realtime API."""
# Send audio to API
async def send_audio():
while True:
audio_chunk = await mic_queue.get()
event = {
"type": "input_audio_buffer.append",
"audio": base64.b64encode(audio_chunk).decode()
}
await ws.send(json.dumps(event))
# Receive audio from API
async def receive_audio():
async for message in ws:
data = json.loads(message)
if data["type"] == "response.output_audio.delta":
audio = base64.b64decode(data["delta"])
await speaker_queue.put(audio)
elif data["type"] == "response.output_audio.done":
print("Agent finished speaking")
await asyncio.gather(send_audio(), receive_audio())
Function Calling
# The API calls your function and waits for the result
async def handle_function_call(ws, data):
"""Handle function call from Realtime API."""
if data["type"] == "response.function_call_arguments.done":
tool_name = data["name"]
args = json.loads(data["arguments"])
# Execute the function
if tool_name == "check_flight_status":
result = await check_flight_status(**args)
# Send result back to API
await ws.send(json.dumps({
"type": "conversation.item.create",
"item": {
"type": "function_call_output",
"call_id": data["call_id"],
"output": json.dumps(result)
}
}))