> ## Documentation Index
> Fetch the complete documentation index at: https://assemblyai.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# AssemblyAI-STT-3.7-Realtime-Agent API

> Build a custom voice agent on AssemblyAI's streaming WebSocket with turn detection, barge-in, and interruption handling without a third-party orchestrator.

## Overview

This guide is for voice agents that connect AssemblyAI's [AssemblyAI-STT-3.7-Realtime-Agent](https://assemblyai.com/docs/streaming/getting-started/transcribe-streaming-audio.md) WebSocket directly to a custom LLM and TTS, with no LiveKit, Pipecat, or other orchestrator in the loop.

AssemblyAI-STT-3.7-Realtime-Agent is AssemblyAI's most accurate real-time model for voice agents, with entity-aware turn detection, native code-switching across 32 languages, and prompting support. The protocol is documented in detail on the [AssemblyAI-STT-3.7-Realtime-Agent quickstart](https://assemblyai.com/docs/streaming/getting-started/transcribe-streaming-audio.md) and [message sequence](https://assemblyai.com/docs/streaming/message-sequence.md) pages. This guide focuses on the voice-agent loop and how to handle barge-in and interruptions correctly.

## Quickstart

A minimal Python consumer that connects to the streaming WebSocket and reacts to `Begin`, `Turn`, `SpeechStarted`, and `Termination` events:

```python expandable theme={null}
import json
import websocket
from urllib.parse import urlencode

API_KEY = "YOUR_API_KEY"
SAMPLE_RATE = 16000

CONNECTION_PARAMS = {
    "sample_rate": SAMPLE_RATE,
    "speech_model": "assemblyai-stt-3-7-realtime-agent",
    "mode": "balanced",
}

API_ENDPOINT = (
    "wss://streaming.assemblyai.com/v3/ws?" + urlencode(CONNECTION_PARAMS)
)


def on_message(ws, message):
    data = json.loads(message)
    msg_type = data.get("type")

    if msg_type == "Begin":
        print(f"Session started: {data.get('id')}")

    elif msg_type == "Turn":
        transcript = data.get("transcript", "")
        end_of_turn = data.get("end_of_turn", False)
        if end_of_turn:
            # Final transcript - send to your LLM
            print(f"Final: {transcript}")
        else:
            # Partial - optionally start pre-emptive LLM generation
            print(f"Partial: {transcript}")

    elif msg_type == "SpeechStarted":
        # User started speaking - interrupt the agent's TTS if it's playing
        print("Speech detected, interrupt agent if speaking")

    elif msg_type == "Termination":
        print("Session ended")


ws = websocket.WebSocketApp(
    API_ENDPOINT,
    header={"Authorization": API_KEY},
    on_message=on_message,
)
ws.run_forever()
```

For the full message protocol, including all event fields, audio framing, and termination, see the [message sequence](https://assemblyai.com/docs/streaming/message-sequence.md) reference.

## Turn detection

AssemblyAI-STT-3.7-Realtime-Agent doesn't end turns on silence alone. When the speaker pauses, the model judges from the transcript whether the speaker is finished, and holds the turn open when it ends in an entity that may still continue, such as part of a phone number. Two parameters control the timing, and the `mode` preset sets their defaults:

| `mode` | `min_turn_silence` | `max_turn_silence` |
| - | - | - |
| `min_latency` | `128` ms | `640` ms |
| `balanced` (default) | `128` ms | `1280` ms |
| `max_accuracy` | `512` ms | `2560` ms |

* `min_turn_silence` is the silence before the model runs an end-of-turn check. If the turn reads as complete, it ends.
* `max_turn_silence` is the maximum silence before the turn is forced to end, regardless of content.

See [Turn detection](https://assemblyai.com/docs/streaming/turn-detection.md) for entity-aware endpointing, tuning guidance, and manual mode, and the [message sequence reference](https://assemblyai.com/docs/streaming/message-sequence.md) for the full event protocol.

## Interruption handling

While the agent is speaking, users often produce backchannel utterances ("mhm", "yeah", "um", "okay") that you don't want to treat as interruptions. A barge-in trigger that fires on every `SpeechStarted` (or every short `Turn`) will cause the agent to stop mid-sentence even though the user didn't intend to interrupt.

The recommended fix is a single combined filter applied to each `Turn` event during agent speech: skip the barge-in if the transcript is short **or** if every token is a known backchannel. Reset the filter once the agent has finished speaking.

```python expandable theme={null}
import json
import string
import time
import websocket
from urllib.parse import urlencode


# "yes" / "no" deliberately omitted - in a booking flow a bare "yes"
# is a real confirmation. Edit for your domain.
BACKCHANNELS = frozenset({
    "mhm", "mm", "mmhm", "mmhmm",
    "uh", "uhhuh", "huh",
    "um", "umm", "uhm",
    "er", "erm",
    "hmm", "hm",
    "ah", "oh",
    "yeah", "yep", "yup",
    "okay", "ok",
    "right", "alright", "gotcha",
})

_PUNCT_STRIP = str.maketrans("", "", string.punctuation)
MIN_WORDS = 2          # Utterances below this are treated as filler
FILTER_GRACE_S = 1.0   # Keep filtering for 1s after agent stops speaking


# These flags are owned by your TTS layer.
agent_speaking = False
last_speaking_at = 0.0


def _is_all_backchannel(text: str) -> bool:
    tokens = text.lower().translate(_PUNCT_STRIP).split()
    return bool(tokens) and all(tok in BACKCHANNELS for tok in tokens)


def _should_suppress_interrupt(text: str) -> bool:
    now = time.monotonic()
    if agent_speaking:
        globals()["last_speaking_at"] = now
    elif now - last_speaking_at > FILTER_GRACE_S:
        return False

    word_count = len(text.split())
    return word_count < MIN_WORDS or _is_all_backchannel(text)


def on_message(ws, message):
    data = json.loads(message)
    msg_type = data.get("type")

    if msg_type == "Turn":
        transcript = data.get("transcript", "")
        end_of_turn = data.get("end_of_turn", False)

        if _should_suppress_interrupt(transcript):
            # Backchannel during agent speech - drop it.
            return

        if end_of_turn:
            handle_user_turn(transcript)  # send to LLM
        else:
            handle_partial(transcript)

    elif msg_type == "SpeechStarted":
        if agent_speaking:
            # Don't interrupt yet - wait for the Turn event,
            # which is gated by _should_suppress_interrupt above.
            return
        # Otherwise: normal barge-in path.
```

**How it works:**

1. While the agent is speaking (plus a 1-second grace window after speech ends), each `Turn` event is checked.
2. `_should_suppress_interrupt` returns `True` when the transcript has fewer than `MIN_WORDS` tokens **or** when every token is a known backchannel. Either condition drops the event.
3. Utterances with any non-filler content past the threshold (e.g., "yeah I'd like the suite") always pass through.
4. `SpeechStarted` is gated through the same `Turn`-level check rather than firing barge-in directly. This prevents a race where a backchannel triggers `SpeechStarted` before the gating logic sees the transcript.

> [!NOTE]
>
> The `BACKCHANNELS` set is domain-customizable. "yes" and "no" are deliberately excluded because they often represent genuine confirmations in booking or IVR scenarios. Edit the set for your use case. `MIN_WORDS = 2` is a reasonable default. Raise it if you see frequent two-word filler ("uh okay", "yeah right") slipping through.

> [!TIP]
>
> If you're building on LiveKit, prefer LiveKit's adaptive interruption handling. See [LiveKit > Interruption handling](https://assemblyai.com/docs/voice-agents/livekit.md#interruption-handling). The strategy on this page is the equivalent for direct-WebSocket integrations.

## Recommended configuration

Three presets covering most voice-agent use cases:

```python theme={null}
# Fast - quick confirmations, IVR, yes/no questions
fast_params = {
    "speech_model": "assemblyai-stt-3-7-realtime-agent",
    "mode": "min_latency",
}

# Balanced - most voice agent conversations (default)
balanced_params = {
    "speech_model": "assemblyai-stt-3-7-realtime-agent",
    "mode": "balanced",
}

# Accurate - when a small added delay is acceptable
accurate_params = {
    "speech_model": "assemblyai-stt-3-7-realtime-agent",
    "mode": "max_accuracy",
}
```

For cross-cutting topics like dynamic configuration updates, scaling, latency budgeting, and evals, see the [voice agent best practices](https://assemblyai.com/docs/voice-agents/best-practices.md) guide.

## Related

* [AssemblyAI-STT-3.7-Realtime-Agent quickstart](https://assemblyai.com/docs/streaming/getting-started/transcribe-streaming-audio.md)
* [Turn detection](https://assemblyai.com/docs/streaming/turn-detection.md)
* [Message sequence](https://assemblyai.com/docs/streaming/message-sequence.md)
* [LiveKit integration](https://assemblyai.com/docs/voice-agents/livekit.md)
* [Pipecat integration](https://assemblyai.com/docs/voice-agents/pipecat.md)
* [Voice agent best practices](https://assemblyai.com/docs/voice-agents/best-practices.md)
