Skip to main content
RealtimeAgent opens a real WebSocket to OpenAI’s Realtime API for bidirectional audio and text.
Looking for token-by-token text streaming (agent.stream('Tell me a story'))? That moved to Streaming. This page is the voice-first RealtimeAgent.

Quick Start

1

Connect and send text

2

Stream microphone audio

What happens on connect failure. connect() resolves only after the WebSocket handshake actually completes. A bad API key, an unreachable host, a close during the handshake, or a timeout all reject — the agent never reports a fake “connected” state. Wrap connect() in try/catch and expect an honest error.

How It Works

Both audio and text turns travel over the same live socket.

Events

Register handlers by event type, or use '*' to receive every server event. Convenience callbacks mirror the Python agent.
Known event types include session.created, session.updated, response.text.delta, response.audio.delta, response.done, and error. Any other type the server sends is emitted untouched.

Configuration Options

Pass realtime as a config object to tune voice and audio. Agent-level options: llm (default gpt-4o-realtime-preview), apiKey (falls back to OPENAI_API_KEY), url (endpoint override), headers, webSocket (inject a constructor), and connectTimeoutMs (default 30000).

RealtimeAgent API Reference

Full class documentation

Runtime Requirements

The WebSocket implementation is resolved at call time: an injected { webSocket } constructor first, then globalThis.WebSocket, then the optional ws package.
The browser subprotocol path puts the API key in the page. Use an ephemeral key there. It is verified at wire level only.
If no implementation is available, connect() throws with the three ways to fix it (upgrade Node, install ws, or inject a constructor) — it never pretends to connect.

Best Practices

A bad key, a bad URL, or a timeout rejects. Handle the error instead of assuming a connection succeeded.
sendText, sendAudio, commitAudio, and clearAudio throw when disconnected. Await connect() first, and re-check isConnected() after a possible server close.
The server may send event types this SDK doesn’t name. Register '*' so you see them all rather than dropping frames.
Browsers cannot set handshake headers, so credentials travel as a subprotocol — visible in the page. Mint a short-lived key for that path.
sampleRate is a local playback hint and is not sent to the API. The low-level SQLiteAdapter in praisonai/db/sqlite is unrelated to this agent and still degrades to a Map for legacy callers.

Streaming

Token-by-token text streaming

Voice

Voice interactions

Audio

Audio input and output

Agent

Create agents