Voice Agent Message Flow
This guide walks you through implementing the correct message flow when building a Voice Agent client. Follow these steps to establish a connection, configure settings, and handle the conversation loop.
Establish the Connection and Receive Welcome
Section titled “Establish the Connection and Receive Welcome”-
Open a WebSocket connection to the Voice Agent endpoint.
-
Wait for the server to send a
Welcomemessage confirming the connection:
{ "type": "Welcome", "request_id": "uuid" }Configure Settings and Wait for Confirmation
Section titled “Configure Settings and Wait for Confirmation”- Send a
Settingsmessage with your audio and agent configuration:
{
"type": "Settings",
"audio": {
"input": {
"encoding": "linear16",
"sample_rate": 16000
},
"output": {
"encoding": "linear16",
"sample_rate": 24000,
"container": "none"
}
},
"agent": {
"listen": { "provider": { "type": "deepgram", "model": "nova-3" } },
"think": {
"provider": { "type": "open_ai", "model": "gpt-4o-mini" }
},
"speak": { "provider": { "type": "deepgram", "version": "v2", "model": "flux-kit-en" } }
}
}- Wait for the server to send a
SettingsAppliedmessage:
{ "type": "SettingsApplied" }Stream Audio and Inject Text
Section titled “Stream Audio and Inject Text”-
After receiving
SettingsApplied, begin streaming binary audio data (PCM) continuously to the server. -
Optionally, send text input using
InjectUserMessage:
{ "type": "InjectUserMessage", "content": "Hello" }Handle Server Events
Section titled “Handle Server Events”- Process the following events as the conversation progresses:
| Event | Description |
|---|---|
UserStartedSpeaking |
User began talking. Stop any audio playback immediately to handle barge-in. |
ConversationText |
User's speech has been transcribed. |
AgentThinking |
Agent is processing the user's input. |
ConversationText |
Agent's text response is available. |
[binary audio] |
Agent's audio response. Play this through your audio output. |
AgentAudioDone |
Agent finished speaking. |
Error / Warning |
Issues occurred during processing. |
Message Flow Diagram
Section titled “Message Flow Diagram”Verify the Implementation
Section titled “Verify the Implementation”Confirm your implementation works correctly by checking:
- You receive a
Welcomemessage immediately after connecting. - You receive a
SettingsAppliedmessage after sending yourSettings. - The agent responds with
ConversationTextand binary audio when you speak or inject text. - Audio playback stops when you receive
UserStartedSpeaking(barge-in detection).
Next Steps
Section titled “Next Steps”- Configure the Voice Agent for detailed settings options.
- Outputs: Server Events for detailed event documentation.