WebSocket connection
Send audio over WebSocket and receive speech, transcripts and session events.
After connecting, send session.start and wait for session.started before sending audio. Specify the model in the startup configuration, not URL query parameters. Keep long-lived API Keys on the server.
wss://api.agtcloud.cn/v1/live/sessionsConnection parameters
Startup configuration
Place this configuration in the session object of the first session.start. Wait for session.started before sending audio.
Session flow
- Connect the main WebSocket with a Bearer API Key.
- Send
session.startto configure the voice model, instructions, audio and delegation mode. - After
session.started, sendsession.input_audio.appendand continuously process transcripts and audio. - When finished, stop audio production, send
session.closeand wait for the final close event.
Prepare audio
| Encoding | Input and output requirements |
|---|---|
audio/pcm | Mono, 16-bit little-endian, sample rate 16000 or 24000; no WAV header |
audio/pcmu, audio/pcma | Mono G.711, sample rate 8000 |
Input and output use the same startup encoding. The Node.js example sends 4800 bytes of 24 kHz PCM every 100 ms and saves output to reply.pcm. The Python example only checks startup and closing.
ffmpeg -i input.wav -f s16le -acodec pcm_s16le -ac 1 -ar 24000 audio.pcm
ffplay -f s16le -ar 24000 -ac 1 reply.pcmThe Node.js example requires ws; the Python example requires websocket-client. Supply the key through the API_KEY environment variable.
Event Reference
This endpoint uses JSON events and Base64 audio, not OpenAI Realtime input_audio_buffer.*. Filter by direction, category or event name below; inspect fields on the left and copy examples on the right.
| Category | Main events |
|---|---|
| Session | session.start, session.started, session.update, session.updated, session.close, session.closed |
| Audio and transcripts | session.input_audio.append, session.output_audio.delta, session.input_transcript.delta, session.output_transcript.delta |
| Context and delegation | session.instructions.append, session.thinking.append, session.commentary.append, session.delegation.created, response.item.create, response.create, response.event |
| Usage and errors | session.usage.updated, error, info |
session.start
First command on the main WebSocket; wait for session.started before sending audio.
Configuration and delegation
| Configuration | Usage |
|---|---|
| Foreground voice | model, instructions and audio are fixed after startup. Append instructions with session.instructions.append |
client delegation | Default mode; the application handles delegated tasks. Reference the corresponding delegation_id when appending results |
responses delegation | Supply the background responses.model at startup, separately from the voice model. Update background configuration with session.update; the delegation mode cannot change |
| Append context | Each appended instructions, thinking or commentary content is limited to 500 tokens. Set delegation_id to null for general context and Responses mode |
| Fixture audio playback | session.output_audio.fixture.play plays up to 30 seconds of mono PCM16LE/24 kHz audio. Input is discarded and context appends are rejected during playback |
Usage and ending the session
session.usage.updated is a cumulative snapshot; do not sum snapshots. To end the entire session, send session.close, wait for session.closed, and save final usage before releasing the connection. A voice turn ending does not require closing the session.
Voice sessions, background Responses and tool calls may be billed separately. See Model catalog for pricing.
Common issues
| Symptom | Resolution |
|---|---|
| Handshake failure | Check the address, Bearer API Key and model access; the path is /v1/live/sessions |
| Startup failure | Use session.start as the first command and wait for session.started before sending other events |
| No sound or distorted audio | Check encoding, sample rate, byte order, channels and WAV headers |
| Instruction update rejected | session.update updates only allowed background delegation configuration; use append events for foreground instructions |
| Connection ended unexpectedly | Save the session ID, error and last usage snapshot; do not automatically replay sent audio or tool tasks |