Overview
Realtime APIs enable bidirectional, low-latency communication with LLMs over WebSocket connections. This powers use cases like:- Voice assistants with real-time transcription and responses
- Interactive chat with streaming function calls
- Live translation and interpretation
- Real-time audio processing
Realtime APIs are different from HTTP streaming. They use WebSocket for full-duplex communication, allowing you to send and receive messages simultaneously.
How It Works
- Client establishes WebSocket connection to Gateway
- Gateway creates outgoing WebSocket connection to provider
- Messages are proxied bidirectionally with observability
- Gateway tracks events, tokens, and costs in real-time
- Connection closes when either side disconnects
Supported Providers
Currently supported:- OpenAI Realtime API (gpt-4o-realtime-preview, gpt-4o-mini-realtime-preview)
Getting Started
WebSocket Connection
Connect to the Gateway’s realtime endpoint:Authentication
Include Portkey headers in the WebSocket upgrade request:You can also use Virtual Keys instead of passing the OpenAI API key directly:
Usage Examples
OpenAI Realtime API Events
Client Events (Send to Gateway)
Create Conversation Item
Request Response
Update Session
Server Events (Receive from Gateway)
Session Created
Response Text Delta
Response Audio Delta
Response Done
Audio Streaming
Send Audio Input
Receive Audio Output
Implementation Details
Gateway WebSocket Handler
Fromsrc/handlers/realtimeHandler.ts:
Event Parsing and Observability
The Gateway parses WebSocket events to track:- Token usage (input/output)
- Cost calculation
- Response latency
- Error rates
- Custom metadata
Advanced Patterns
Function Calling
Multi-Turn Conversation
Voice Assistant
Configuration Options
Session Configuration
Voice Options
alloyechofableonyxnovashimmer
Audio Formats
pcm16- 16-bit PCM audio at 24kHzg711_ulaw- G.711 μ-law audio at 8kHzg711_alaw- G.711 A-law audio at 8kHz
Error Handling
Best Practices
Implement Reconnection Logic
Implement Reconnection Logic
WebSocket connections can drop. Implement exponential backoff reconnection:
Handle Audio Buffering
Handle Audio Buffering
Buffer audio chunks to prevent choppy playback and handle network jitter appropriately.
Monitor Connection Health
Monitor Connection Health
Implement ping/pong or heartbeat to detect stale connections:
Cleanup Resources
Cleanup Resources
Always cleanup when done:
Use Virtual Keys
Use Virtual Keys
Use Portkey Virtual Keys instead of hardcoding API keys for better security and management.
Related Features
Streaming
HTTP streaming responses
Multi-Modal
Audio and vision capabilities
Timeouts
Configure connection timeouts
Observability
Monitor realtime API usage