Skip to navigation

Murf’s WebSocket implementation is based on the concept of a Context ID - a unique identifier used to track a specific TTS request. Context ID ensures continuity in the conversation, especially when the input text is generated in real time (e.g., via an LLM), comes in parts, or when the interaction is interrupted. It serves as a proxy for a single turn in the interaction between a user and the agent.

Why Use a Context ID?

  • Maintains Conversational Flow: Links input and output across split or partial messages.
  • Handles Interruptions Gracefully: If a user interrupts the agent, the Context ID ensures that the current request can be cancelled or skipped.
  • Supports Multi-Turn Interactions: Essential for structured flows like bookings, troubleshooting, or guided forms.
  • Simplifies Handoff and Debugging: If support is needed, the Context ID allows you to trace exactly what happened in a specific interaction turn.
Single connection with a context ID One connection with multiple contexts

Working with Context IDs

Input Stream

  • Assign a new context_id for each turn in the conversation.
  • The end parameter should be set to true at the end of each turn. This clears the context and allows the next turn to start.
  • Ensure input text includes proper punctuation for better prosody and consistent audio output.

If you omit context_id, your text goes to the default context. Audio, final, and warnings for it have context_id: null. Use the missing id to match these responses. Don’t store null as a key.

Voice settings and Context IDs

Each context has its own voice settings. If you send text without a context_id, it goes to the default context.

Three contexts on one connection: the default context and ctx_a each use their own voice_config, and ctx_b has none, so it gets the default voice and a NO_VOICE_CONFIG warning.
  • Always set the voice. Send voice_config with a valid voice_id before the first text of a turn. Use the same context_id on both. For the default context, omit context_id on both.
  • A voice_config applies only to its own context. Other contexts don’t use it, including the default context.
  • While a turn is open, you don’t need to send voice_config again. Send each text fragment, and set end: true on the last one. You get one final for the turn.
  • end: true clears that context’s voice_config. The connection stays open. Before you send more text on that context_id, send voice_config again. This also applies to the default context.
  • You can send voice_config in its own frame or in the same frame as the first text.
  • Don’t rely on the default voice. If text arrives without voice_config, Falcon uses a default voice and returns a NO_VOICE_CONFIG warning. If voice_config has a missing or unknown voice_id, you get a fatal INVALID_VOICE error and no audio, so a typo never plays in the wrong voice. See Errors & Warnings.

Send the fragments of one turn as separate frames. To use turn_1 again for the next turn, send voice_config again first.

{
"voice_config": {
"voice_id": "Gordon",
"locale": "en-US"
},
"context_id": "turn_1"
}
{
"context_id": "turn_1",
"text": "Sure, "
}
{
"context_id": "turn_1",
"text": "when would you like to travel?",
"end": true
}

Output Audio

Client frames voice_config, text, and text with end: true, and server frames audio and final, all carrying context_id ctx_a.
  • The response will include the same context_id, allowing you to match responses with their corresponding inputs.
  • A final flag will indicate that all audio for that context has been sent.
  • Output is streamed in the same order that input text was received.

Concurrency

ctx_a ends and frees its slot, and ctx_d takes it while ctx_b and ctx_c stay active.

Each simultaneously active context_id counts toward your streaming concurrency limit. Closing a turn with {"end": true} frees its slot shortly after its final message. Each final message reports the region, its limit and the contexts in use, and with include_usage=true a session_info message reports the same when a context starts. If a new context goes over the limit, you receive an ACTIVE_CONTEXT_LIMIT_EXCEEDED error and the connection is closed with code 4029. See Rate Limits & Concurrency and Errors & Warnings.

Handling Interruptions

On interruption, send clear for ctx_b, then voice_config and text with end: true for ctx_c, as three separate frames.
  • If the user interrupts the agent mid-response, start a new context_id for the next turn.
  • Murf’s WebSocket supports multiplexing, so multiple context_ids can run independently over the same connection.
  • To cancel a pending or in-progress turn, use the clear parameter. If audio generation hasn’t started, the request is cancelled. If audio generation has started, the audio will still play to completion.

clear semantics

  • {"clear": true} discards the context and any pending audio. This is what you use for barge-in. If you are using word timestamps, also drop every timestamp whose start_s is past the current playback position. Frames the client has already received stay on the client, and text sent after the clear starts a new audio clock near 0.
  • {"clear": false} means “do not clear” and is safely ignored, so it is fine to send a fully-populated payload with the key present.
  • text sent in the same frame as clear is dropped, and you receive a TEXT_IGNORED_WITH_CLEAR warning. Send the clear and the new text as two separate frames.

Example for using Context ID in a Voice Agent

Let’s say you’re building a voice agent that helps users book flights.

1

Establish WebSocket Connection

You open a WebSocket connection when the conversation starts. This connection stays open and allows real-time audio streaming between user and agent.

2

Use a Context ID for Each Agent Response (Turn)

Turn 1

User says: “I want to book a flight to Paris.”

Your backend processes this and generates an agent response:

Agent text: “Sure, when would you like to travel?”

First configure turn_1, then send text with the same context_id. Each JSON object is a separate WebSocket frame:

{
"voice_config": {
"voice_id": "Gordon",
"locale": "en-US",
"style": "Conversational"
},
"context_id": "turn_1"
}
{
"context_id": "turn_1",
"text": "Sure, when would you like to travel?",
"end": true
}

Murf returns audio with context_id: “turn_1” so you can play it back to the user.

Turn 2

User says: “Next Friday.”

Agent response: “Got it. Do you prefer morning or evening flights?”

Because turn_2 is a new context, configure it separately. It does not inherit the voice settings from turn_1:

{
"voice_config": {
"voice_id": "Gordon",
"locale": "en-US",
"style": "Conversational"
},
"context_id": "turn_2"
}
{
"context_id": "turn_2",
"text": "Got it. Do you prefer morning or evening flights?",
"end": true
}

Murf returns audio tagged with context_id: “turn_2”.

3

Handle Interruptions

If the agent is mid-response and the user interrupts (e.g., says “Wait, make that Saturday”):

  • Stop playback of the current audio. The clear: true flag cancels any pending or incomplete responses tied to earlier contexts.
  • Clear the previous context:
{
"context_id": "turn_2",
"clear": true
}
  • Configure the new context:
{
"voice_config": {
"voice_id": "Gordon",
"locale": "en-US",
"style": "Conversational"
},
"context_id": "turn_3"
}
  • Send the updated agent reply with the same new context_id:
{
"context_id": "turn_3",
"text": "Saturday works. Do you want to fly direct or stopover?",
"end": true
}

Murf returns audio tagged with context_id: “turn_3”.

Send these as three separate frames. If you put clear and text in the same frame, the text is dropped and you receive a TEXT_IGNORED_WITH_CLEAR warning.

Why This Matters

  • Each context ID represents one agent turn in conversation.
  • You maintain clean tracking of each response, even with interruptions.
  • WebSocket handles all turns over a single connection, supporting real-time, fluid interaction.