For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
GPT-Live

Build your first voice application with GPT-Live

In this lesson, we’re going to connect to GPT-Live and build our first voice application.

Streaming audio

Let’s start with how our application exchanges audio with the model.

In a text-based application, we can wait until someone finishes typing their message before we send it. With live voice, we need to send audio as the person speaks, without waiting for a complete recording.

When GPT-Live replies, we also need to play the audio as the model generates it, rather than wait for the whole answer.

That means we need a connection that stays open for the whole conversation and lets audio flow in both directions at once.

Let’s look at two ways of doing that: WebSocket and WebRTC.

WebSocket

A WebSocket connection lets our application and GPT-Live send messages back and forth at any time. It can carry audio, but it wasn’t built specifically for audio streaming, so our application needs to handle more of the work.

For example, GPT-Live sends audio as it generates a reply, but network delays can make the pieces arrive at uneven intervals. Our application needs to buffer a little audio so a short delay doesn’t leave a gap in what the user hears.

Playing audio from a WebSocketFour numbered audio pieces travel from GPT-Live to the application, which holds them until they can play in order. Piece 3 arrives late, while piece 2 is playing. The small reserve of audio lets playback continue without a gap. This animation illustrates timing rather than playing a recording.GPT-LiveStreams audio as it’s generated1234Your applicationBuffers audio for playback1234SpeakerPlays audio smoothly1234Playing audio from a WebSocketFour numbered audio pieces travel from GPT-Live to the application, which holds them until they can play in order. Piece 3 arrives late, while piece 2 is playing. The small reserve of audio lets playback continue without a gap. This animation illustrates timing rather than playing a recording.GPT-LiveStreams audio as it’s generated1234Your applicationBuffers audio for playback1234SpeakerPlays audio smoothly1234

WebRTC

WebRTC, on the other hand, was built for live audio and video. It handles much of that work for us, including compressing the audio and keeping playback smooth when pieces arrive unevenly.

That’s why we recommend WebRTC for GPT-Live audio connections in browser, mobile, and server applications. It’s what we’ll use throughout this training.

If you’re building an agent that people call over the phone, you can also connect through a phone provider using SIP, a standard used to set up internet phone calls. Although we won’t cover SIP in this training, you can find more details in the SIP guide.

WebRTC walkthrough

Let’s follow a browser app from the first microphone request to the end of a GPT-Live session. Select Show code to follow along in the browser and server files.

Step 1 of 15

Browser and server

Let’s say we’re building a voice app that people use in their browser, with a server running behind it. We’ll use both to set up a WebRTC connection between the browser and OpenAI, where GPT-Live runs.

Request microphone access

First, the browser asks for permission to use the microphone so it can capture the user’s voice.

Prepare the microphone and speaker

Once we have that permission, we can add the microphone’s outgoing audio track to the WebRTC connection. We also set up the browser to play the track coming back from GPT-Live through the speaker.

Create a data channel for messages

Next, we add a data channel to the same connection. The browser can use it to receive transcripts and session updates from OpenAI. It can also send commands back over that channel, like a request to end the conversation.

Generate the browser’s SDP offer

WebRTC compresses audio, so the browser and OpenAI need a format they both support. They also need to know how to reach each other. The browser generates an SDP offer with its audio formats, support for the data channel, and the network addresses and ports OpenAI can try.

Send the offer to our application server

The browser asks our server to create a GPT-Live session and includes its SDP offer. The request to OpenAI needs our app’s API key, so the server makes that call on the user’s behalf.

Add the session settings

On the server, we put the offer into a request to OpenAI. We include the model name and the instructions we want GPT-Live to follow during the conversation.

Create a session with OpenAI

Our server sends the request with its API key. OpenAI creates a session for the conversation using those settings and prepares the connection details the browser needs to join it.

Pass OpenAI’s answer back to the browser

OpenAI returns those details in an SDP answer. It tells the browser which audio formats OpenAI accepts and where to reach it. Our server passes the answer back to the browser.

Use the answer to finish connecting

The browser uses the addresses in the answer to reach OpenAI and finish setting up the WebRTC connection. From here, audio and messages can travel directly between the browser and GPT-Live.

Wait for the session to start

Once the data channel opens, OpenAI uses it to confirm that the session is ready. The browser waits for that session.started message before sending any commands.

Send microphone audio and hear the reply

During the conversation, the user’s voice goes to GPT-Live on one audio track, and the model’s voice comes back on the other. Both tracks can be active at once, so GPT-Live can keep listening while it speaks.

Display the transcript as it arrives

As GPT-Live speaks, OpenAI sends transcript updates over the data channel. Your app can display the text in the browser as each update arrives.

Context compaction

GPT-Live automatically compacts the conversation history when the context window is over 90% full. The session continues with your instructions, recent messages, and any available summary. Store any key details you’ll need later in your application because the summary could omit them.

Disconnect from GPT-Live

The browser can ask OpenAI to close the session. For example, it might do this when the user ends the conversation or when your app ends it after a period of inactivity. Once OpenAI confirms the session is closed, the browser disconnects and releases the microphone.

Your application

Browser

Server

OpenAI

GPT-Live

Connecting your server to the session

As you saw in the walkthrough, our server helps set up the WebRTC connection. But once it’s open, the browser and GPT-Live exchange audio and messages directly.

To let our server follow the conversation too, we can connect it to the same session over a WebSocket. That’s called a sideband connection. The server can receive transcripts and delegation requests while the browser handles the call audio. It can also send instructions back to GPT-Live.

Your application

Browser

MicrophoneSpeaker

Server

Sideband WebSocket
Transcript received
OpenAI

GPT-Live

Instruction received

Try the application yourself

Build a WebRTC voice app
@OpenAI Developers, build and run a browser voice app that connects to GPT-Live 1.1 over WebRTC. Show each step as it happens, from microphone access to the browser’s offer, the server’s session request, and the answer that comes back to the browser. Once I’m connected, let me talk to GPT-Live and watch the transcript arrive over the data channel. Lesson 3: https://developers.openai.com/api/training/gpt-live/3-build-your-first-voice-application-with-gpt-live.md