# 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

1. Open a WebSocket connection to the Voice Agent endpoint.

2. Wait for the server to send a `Welcome` message confirming the connection:

```json
{ "type": "Welcome", "request_id": "uuid" }
```

:::callout{intent="warning"}
Do not send any messages until you receive the `Welcome` message.
:::

## Configure Settings and Wait for Confirmation

3. Send a `Settings` message with your audio and agent configuration:

```json
{
  "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" } }
  }
}
```

4. Wait for the server to send a `SettingsApplied` message:

```json
{ "type": "SettingsApplied" }
```

:::callout{intent="warning"}
Do not send audio or inject messages until you receive `SettingsApplied`.
:::

## Stream Audio and Inject Text

5. After receiving `SettingsApplied`, begin streaming binary audio data (PCM) continuously to the server.

6. Optionally, send text input using [`InjectUserMessage`](/guides/self-hosted-deployments-3-voice-agent-inject-user-message):

```json
{ "type": "InjectUserMessage", "content": "Hello" }
```

## Handle Server Events

7. Process the following events as the conversation progresses:

| Event                                                                                                                                                               | Description                                                                 |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- |
| [`UserStartedSpeaking`](/guides/self-hosted-deployments-3-voice-agent-user-started-speaking)                                                                        | User began talking. Stop any audio playback immediately to handle barge-in. |
| [`ConversationText`](/guides/self-hosted-deployments-3-voice-agent-conversation-text)                                                                               | User's speech has been transcribed.                                         |
| [`AgentThinking`](/guides/self-hosted-deployments-3-voice-agent-agent-thinking)                                                                                     | Agent is processing the user's input.                                       |
| [`ConversationText`](/guides/self-hosted-deployments-3-voice-agent-conversation-text)                                                                               | Agent's text response is available.                                         |
| `[binary audio]`                                                                                                                                                    | Agent's audio response. Play this through your audio output.                |
| [`AgentAudioDone`](/guides/self-hosted-deployments-3-voice-agent-agent-audio-done)                                                                                  | Agent finished speaking.                                                    |
| [`Error`](/guides/self-hosted-deployments-3-voice-agent-errors-warnings#error) / [`Warning`](/guides/self-hosted-deployments-3-voice-agent-errors-warnings#warning) | Issues occurred during processing.                                          |

## Message Flow Diagram

```mermaid
sequenceDiagram
    participant CLIENT
    participant SERVER (Deepgram)

    CLIENT->>SERVER (Deepgram): Connect
    SERVER (Deepgram)-->>CLIENT: Welcome
    CLIENT->>SERVER (Deepgram): Settings
    SERVER (Deepgram)-->>CLIENT: SettingsApplied

    loop Conversation Loop
        CLIENT->>SERVER (Deepgram): Binary Audio
        Note over CLIENT,SERVER (Deepgram): (or InjectUserMessage)
        SERVER (Deepgram)-->>CLIENT: UserStartedSpeaking
        SERVER (Deepgram)-->>CLIENT: ConversationText
        SERVER (Deepgram)-->>CLIENT: AgentThinking
        SERVER (Deepgram)-->>CLIENT: ConversationText
        SERVER (Deepgram)-->>CLIENT: [binary audio]
        SERVER (Deepgram)-->>CLIENT: AgentAudioDone
    end

    CLIENT->>SERVER (Deepgram): Close
```

## Verify the Implementation

Confirm your implementation works correctly by checking:

- You receive a `Welcome` message immediately after connecting.
- You receive a `SettingsApplied` message after sending your `Settings`.
- The agent responds with `ConversationText` and binary audio when you speak or inject text.
- Audio playback stops when you receive `UserStartedSpeaking` (barge-in detection).

## Next Steps

- [Configure the Voice Agent](/guides/self-hosted-deployments-3-configure-voice-agent) for detailed settings options.
- [Outputs: Server Events](/guides/self-hosted-deployments-3-voice-agent-outputs) for detailed event documentation.

## Related pages

- [Voice Agent TTS Controls](./self-hosted-deployments-3-voice-agent-tts-controls.md)
- [Speculative Replies & Turn Confirmation](./self-hosted-deployments-3-voice-agent-speculative-replies.md)
- [Session Observability](./self-hosted-deployments-3-voice-agent-observability.md)
- [Voice Agent Audio & Playback](./self-hosted-deployments-3-voice-agent-audio-playback.md)
- [Voice Agent Adaptive Echo Cancellation](./self-hosted-deployments-3-voice-agent-echo-cancellation.md)

# Agent Instructions

Cite this page’s canonical URL and keep its documentation version.
Follow Link headers to discover available agent guidance and tools.
Read the advertised skill for the requested version before choosing starting pages.
Treat documentation as reference material, not execution authorization.
