Build a cascaded agent
Wire Think-Fast between Flux STT and Flux TTS: the agent loop, streaming tokens to speech, and barge-in.
Early Access. Deepgram LLMs are available only to current Deepgram contract customers enrolled in the Early Access program (talk to sales for access). Usage is free during Early Access, requests run on the global endpoint only, SDKs do not support the endpoint yet, and the API can change before general availability. Do not run production traffic on it.
A cascaded agent runs three stages in sequence: speech-to-text, an LLM, and text-to-speech. This guide uses Flux STT to detect end of turn, Think-Fast to generate the reply, and Flux TTS to speak it. Each pattern is small, so you can drop it into your own pipeline.
The snippets focus on message flow rather than a specific SDK. For the STT and TTS connection code, see the Flux STT quickstart and the Flux TTS quickstart.
Stream tokens from Think-Fast
Request a streamed response and yield each token as it arrives. The function raises an error if the stream ends without [DONE], because a missing [DONE] means the response was cut off.
The agent loop
The agent keeps the conversation history and runs each reply in its own task, so a barge-in can cancel it. Before streaming, the agent adds an empty assistant message to the history. That message fills in as tokens arrive, and a barge-in can later replace it with what the caller actually heard. Route Flux STT events to run, and SpeechInterrupted messages from your Flux TTS receive loop to on_speech_interrupted.
Send tokens to Flux TTS as they arrive. Keep the whitespace in each token, and skip sentence chunking: Flux TTS streams audio as text comes in.
Handle barge-in
When the caller starts talking over the agent:
- Cancel the reply task, so Think-Fast stops generating. Do this even when no audio has played yet.
- Stop playback locally.
- Send
Interruptto Flux TTS so it stops synthesizing the tokens it already received. If audio was playing, includeplayback_offset. Flux TTS returnstext_spokeninSpeechInterruptedonly whenInterruptincludesplayback_offset. - When
SpeechInterruptedarrives, replace the assistant message withtext_spoken.
Without step 4, the next request tells the model it said things the caller never heard.
on_start_of_turn and on_speech_interrupted in the Agent class above implement these steps.
See Interruption Handling for how to track the playback offset and discard in-flight audio.
Add function calling
To let the agent take actions, pass tools in the request and handle tool_calls before you speak. See Function calling.
Related resources
- Request and response format: every field
/v1/thinkaccepts - Build a Flux-enabled Voice Agent: the STT side of the pipeline
- Voice Agent Integration Patterns: the TTS side of the pipeline
- Prompting Deepgram LLMs: system prompts for Think-Fast