Skip to main content
This cookbook demonstrates how to build an interactive audio chat application using OpenAI’s Realtime API with WebSocket, featuring real-time voice interactions, tool calling, and comprehensive observability through Maxim.

Prerequisites

  • Node.js 18+
  • OpenAI TypeScript SDK (npm install openai)
  • Maxim TypeScript SDK (npm install @maximai/maxim-js)
  • Audio dependencies: npm install node-record-lpcm16 speaker
  • sox for audio recording: brew install sox (macOS) or apt install sox (Linux)
  • API keys for OpenAI and Maxim

Environment Variables

Project Setup

Create a new project and install dependencies:

Architecture Overview

The application uses a WebSocket connection to OpenAI’s Realtime API for bidirectional audio streaming:

Code Walkthrough: Key Components

1. Imports and Audio Dependencies

The audio dependencies are loaded dynamically with error handling to provide helpful installation messages.

2. Define Tools for the Assistant

3. Implement Tool Execution

4. Initialize Maxim and OpenAI Realtime

The wrapOpenAIRealtime function integrates Maxim with the OpenAI Realtime WebSocket, automatically capturing all interactions for observability.

5. Configure the Realtime Session

6. Handle Audio Playback

7. Handle Function Calls

8. Microphone Recording

9. Cleanup Resources


Complete Code


How to Use

  1. Set Environment Variables: Configure your API keys in a .env file.
  2. Run the Script: Execute with npx ts-node realtime_audio.ts.
  3. Interact via Voice: Press [r] to start/stop recording, or hold [space] for push-to-talk.
  4. Type Text: You can also type text messages directly at the prompt.
  5. Use Tools: Ask about weather, time, or perform calculations—the assistant will use the appropriate tool.
  6. Monitor in Maxim: All interactions are automatically logged to your Maxim dashboard.

Run the Script


Observability with Maxim

The wrapOpenAIRealtime function automatically captures all Realtime API interactions:
  • Session Events: Session creation, updates, and configuration changes
  • Audio Streams: Input and output audio events with transcriptions
  • Function Calls: Tool invocations with arguments and results
  • Responses: Complete assistant responses with metadata
  • Errors: Any errors that occur during the session

Custom Session Headers

You can pass custom headers to enrich your sessions:

Troubleshooting

  • Audio not recording
    • Ensure sox is installed: brew install sox (macOS) or apt install sox (Linux)
    • Check microphone permissions in your system settings
    • Verify node-record-lpcm16 is installed correctly
  • No audio playback
    • Check that speaker package is installed
    • Verify your system audio output is configured correctly
    • Try adjusting the speaker sample rate if needed
  • WebSocket connection fails
    • Verify your OPENAI_API_KEY is valid and has Realtime API access
    • Check your network connection and firewall settings
  • No Maxim traces
    • Ensure MAXIM_API_KEY and MAXIM_LOG_REPO_ID are set correctly
    • Verify wrapOpenAIRealtime is called before any events are sent
    • Call wrapper.cleanup() before exiting to flush pending logs
  • Tools not working
    • Ensure tools are included in the session configuration
    • Check that response.function_call_arguments.done handler is properly registered
    • Verify the tool result is sent back with the correct call_id

Resources

OpenAI Realtime API

Official OpenAI Realtime API documentation

Maxim JS SDK

Maxim TypeScript SDK on npm