Full code on GitHub
A FastAPI server, a browser page with a live transcript and event log, and a terminal probe that streams a sample caller through it.
What you’ll build
- A Python server that holds one Dialog-RSN-1 connection per browser tab and forwards the caller’s audio to it.
- Streaming speech: Cartesia speaks each reply from its first few words, while Dialog-RSN-1 is still writing it.
- Barge-in: talk over the agent and it stops as soon as Dialog-RSN-1 confirms it’s you, and the model learns which words you heard.
- A browser page with a microphone picker, voice and turn-taking settings, a live transcript with per-turn latency, and a raw event log.
Prerequisites
- Python 3.12 and uv.
- A Dialog-RSN-1 API key. Sign up or log in at studio.poly.ai and create one in the API Keys tab. The quickstart walks through it.
- A Cartesia API key. The free tier works.
- Chrome, Edge or Safari, and headphones if you’ll use the microphone.
Run it
http://127.0.0.1:3000. Click Start conversation and talk. Or click Play the sample caller: a recorded caller asks about a trip to Lisbon, then talks over the answer, so you see a barge-in without a microphone.
To check the whole pipeline from a terminal, leave the server running and stream the same sample through it. The probe counts from audio_end_ms on speech_stopped, where Dialog-RSN-1 marks the end of speech in the caller’s audio. That mark falls a little after the caller’s last word:
Step 1: Open one socket and configure the session
Each browser tab gets its ownCall, which opens a Dialog-RSN-1 WebSocket with the key in a header. The browser never sees the key.
config.DIALOG_REALTIME_URL defaults to wss://api.studio.poly.ai/v1/realtime. Set DIALOG_REALTIME_URL in .env to point somewhere else.
Then it sends one session.update:
output_modalities: ["text"]. This agent speaks through its own Cartesia stream, so it asks Dialog-RSN-1 for text. Sessions start with["audio"], where the server speaks the reply itself, so set this or the caller hears two voices.rate: 24000. The page records at 24 kHz, and Cartesia returns 24 kHz, so the page never resamples.create_response: true. When Dialog-RSN-1 confirms the caller has finished, it starts the reply itself. You don’t sendresponse.create.
session.updated before it sends any audio, so Dialog-RSN-1 reads every frame at the right rate.
Step 2: Forward the caller’s audio
The page sends the microphone as binary PCM16, 100 ms per frame. The server wraps each frame ininput_audio_buffer.append and passes it on:
Step 3: Stream the reply into speech
Dialog-RSN-1 sends the reply asresponse.output_text.delta events. Each delta goes straight to Cartesia:
cartesia_tts.py keeps one Cartesia WebSocket open for the whole call. Each response gets its own Cartesia context, named after the response_id. Text goes in with continue: true, so Cartesia keeps the rhythm of one sentence across many small pieces. Two details matter:
- Send whole words. A delta can end mid-word.
push()holds the text after the last space until the word is complete. - Lower
max_buffer_delay_ms. Cartesia waits up to this long for more text before it speaks. Its default, 3 seconds, favors prosody. The demo sets 250 ms, so Cartesia starts speaking soon after the first words arrive.
Step 4: Stop talking when the caller does
When the caller starts speaking, Dialog-RSN-1 sendsinput_audio_buffer.speech_started. It has already checked that the sound is the caller and not noise, so the server acts on it at once:
Step 5: Tell Dialog-RSN-1 what the caller heard
Only the page knows what reached the speakers. Its player worklet counts the samples it has played and reports every 250 ms. The server turns those counts into Dialog-RSN-1 playback reports:spoken_text comes from Cartesia’s word timestamps: every word whose audio had started by played_ms.
Dialog-RSN-1 cuts the reply in its history back to spoken_text, so the model knows what the caller missed. In the sample, the agent had written its whole reply, but the caller heard only up to “The Alfama neighborhood”. The page strikes through the words the caller never heard. The reports also keep the barge-in check running for exactly as long as the page plays the reply. Playback reports explains both, and what happens without them.
Tune turn-taking with instructions
Dialog-RSN-1 already waits through pauses and slow speech, and replies quickly once the caller has finished. To shift how patient it is, describe your callers in the instructions. The page’s Turn-taking menu adds one line to them:Troubleshooting
Where to go next
- Using your own text-to-speech explains each part of this app for any provider, with an ElevenLabs example in a terminal.
- Turn-taking covers turn-taking instructions in depth.
- The API reference lists every event this app uses, including the
poly.output_audio.*playback reports.

