OpenAI Live API Application and Usage

OpenAI Live API uses gpt-live-1 to provide real-time voice conversations, supporting simultaneous listening and speaking, bidirectional subtitles, and background task delegation. This article introduces the complete integration process from obtaining an API Token, configuring a session, to sending and receiving audio and ending a call.

Application Process

  1. Open the jiasmart Console, register or log in, then create an application to obtain an API Token.
  2. Save the Token in your own server-side environment variables. One general API Token can call all platform services; if using credentials limited to specific services, confirm that you have obtained permission to call the OpenAI Live API.
  3. Confirm that the account has available balance; when the balance is insufficient, you can top up the general balance in the Console.

Complete parameter documentation: OpenAI Live API. Browser applications should use restricted credentials to avoid hardcoding long-term valid API Tokens in the frontend.

Basic Usage

Fill in Session Parameters

Open the OpenAI Live API parameters page, select or fill in authorization, then edit the session.start message in the request body. The JavaScript and Python examples on the right will update along with the parameters.

Field Type Description
type string The first message is fixed as session.start.
session object Required, session configuration.
session.model string Model, fixed as gpt-live-1.
session.instructions string Optional, the voice assistant's behavior and response requirements.
session.audio.format object Audio format, fixed as {"type":"audio/pcm","rate":24000}, namely 24 kHz mono PCM16.
session.audio.output.voice string Optional, select the voice at the start of the call, defaulting to marin; see the parameters page for supported voices.
session.delegation object Background task delegation method, fixed as {"type":"client"}, with your application handling background tasks.
session.store boolean Whether to save recordings, currently fixed as false.

This interface sends and receives JSON messages through WebSocket. HTTP GET is only used to establish the connection, and the request body on the page is the first WebSocket message sent after the connection succeeds. Clicking “Connect” establishes a session and sends this message; audio capture and playback need to be implemented in your application.

Establish a Connection

General or OpenAI service credentials use wss://realtime.acedata.cloud/v1/live/sessions; chat service credentials use wss://realtime.acedata.cloud/aichat2/live. The server authenticates through the request header Authorization: Bearer <token>. Browsers use the WebSocket subprotocols live and acedata-token.<token> to pass credentials.

The following uses a general API Token as an example. Replace <YOUR_API_TOKEN> with the obtained Token:

const token = '<YOUR_API_TOKEN>';
const socket = new WebSocket('wss://realtime.acedata.cloud/v1/live/sessions', [
  'live', `acedata-token.${token}`
]);
socket.onopen = () => socket.send(JSON.stringify({
  type: 'session.start',
  session: {
    model: 'gpt-live-1',
    instructions: 'Answer concisely and friendly, using the user’s language. Delegate complex questions to the background.',
    audio: { format: { type: 'audio/pcm', rate: 24000 }, output: { voice: 'marin' } },
    delegation: { type: 'client' },
    store: false
  }
}));

socket.onmessage = ({ data }) => {
  const event = JSON.parse(data);
  if (event.type === 'session.started') {
    console.log('Session has started, audio can be sent');
  } else if (event.type === 'session.input_transcript.delta' ||
             event.type === 'session.output_transcript.delta') {
    console.log(event.type, event.delta);
  } else if (event.type === 'session.output_audio.delta') {
    // Decode event.delta from Base64 into PCM16, and add it to the audio playback queue in order.
  } else if (event.type === 'session.closed') {
    console.log('Final usage', event.usage);
    socket.close();
  } else if (event.type === 'error') {
    console.error(event.error);
  }
};

Send Audio and Receive Results

After waiting for session.started, continuously send session.input_audio.append, where audio is the Base64 of 24 kHz, mono, 16-bit little-endian PCM raw audio, without a WAV file header. For example, send after the application captures an audio segment each time:

function sendAudio(base64Pcm16) {
  socket.send(JSON.stringify({
    type: 'session.input_audio.append',
    audio: base64Pcm16
  }));
}

Send audio at the actual sampling speed, and silent segments must also remain continuous.

Play session.output_audio.delta.delta in order. User and assistant subtitles come from session.input_transcript.delta and session.output_transcript.delta respectively; retain the delta, start_ms, and end_ms of each segment; subtitles for both sides may grow simultaneously. Live does not have an end event for each voice response, and the “responding” state should be updated based on the local playback queue.

Background Task Delegation

The client delegation mode is currently supported. After receiving session.delegation.created, the application calls its own backend based on voice subtitles and saved context, and returns results through session.thinking.append or session.commentary.append, carrying the corresponding delegation_id and plain-text content of up to 500 tokens. Results should come from completed backend operations; voice interruption does not mean that the background task has been canceled.

Managed Responses delegation, recording storage, session.update, session recovery, or automatic reconnection are not supported for now. The model is fixed as gpt-live-1, audio is fixed as 24 kHz PCM16, and the voice is selected at the start of the call. The old Realtime API can still be used.

End the Call

Send the following message when ending, continue receiving until session.closed is received, then close the connection and release the microphone:

socket.send(JSON.stringify({ type: 'session.close' }));

Forced disconnection may result in final usage not being confirmed, and the interface should clearly report this. The connection will end when the balance is insufficient, billing fails, or the session limit is reached. By default, a single call can last up to 30 minutes.

Billing Information

Voice is billed by active session duration: 0.01 Credits/second, namely 0.6 Credits/minute, without rounding up to whole minutes. Time when the user speaks, the assistant speaks, there is silence, both sides are silent, and background tasks are awaited is all billed. Background model or tool calls are billed separately according to the corresponding API. The USD price corresponding to Credits depends on the top-up plan and is subject to the current price in the Console.

session.usage.updated.usage.seconds is cumulative usage. For example, if 12 seconds are received first and then 15 seconds are received, the total is 15 seconds. The platform only settles the newly added duration and does not repeatedly accumulate snapshots.

Error Handling

Parameter errors are returned through the error event; error.client_event_id corresponds to the failed command. If startup fails, recording should be stopped and the error displayed to avoid repeated requests generating new charges. When credentials are invalid, check whether the Token is complete, whether it has permissions for the corresponding service, and whether the connection address used matches the credential type.