Skip to main content

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
The Gateway provides a WebSocket server that proxies connections to provider realtime endpoints (currently OpenAI’s Realtime API).
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

  1. Client establishes WebSocket connection to Gateway
  2. Gateway creates outgoing WebSocket connection to provider
  3. Messages are proxied bidirectionally with observability
  4. Gateway tracks events, tokens, and costs in real-time
  5. 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

From src/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

  • alloy
  • echo
  • fable
  • onyx
  • nova
  • shimmer

Audio Formats

  • pcm16 - 16-bit PCM audio at 24kHz
  • g711_ulaw - G.711 μ-law audio at 8kHz
  • g711_alaw - G.711 A-law audio at 8kHz

Error Handling

Best Practices

WebSocket connections can drop. Implement exponential backoff reconnection:
Buffer audio chunks to prevent choppy playback and handle network jitter appropriately.
Implement ping/pong or heartbeat to detect stale connections:
Always cleanup when done:
Use Portkey Virtual Keys instead of hardcoding API keys for better security and management.

Streaming

HTTP streaming responses

Multi-Modal

Audio and vision capabilities

Timeouts

Configure connection timeouts

Observability

Monitor realtime API usage