<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aqib</title>
    <description>The latest articles on DEV Community by Aqib (@aqibnawab).</description>
    <link>https://dev.to/aqibnawab</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3728536%2F8aaac18d-d76d-4ce0-8b33-6ff351bbb215.jpg</url>
      <title>DEV Community: Aqib</title>
      <link>https://dev.to/aqibnawab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aqibnawab"/>
    <language>en</language>
    <item>
      <title>Sending LLM Tokens to TTS: Where to Cut the Text in a Voice Agent</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Tue, 24 Nov 2026 16:38:25 +0000</pubDate>
      <link>https://dev.to/aqibnawab/sending-llm-tokens-to-tts-where-to-cut-the-text-in-a-voice-agent-53nn</link>
      <guid>https://dev.to/aqibnawab/sending-llm-tokens-to-tts-where-to-cut-the-text-in-a-voice-agent-53nn</guid>
      <description>&lt;p&gt;In a voice agent, the LLM streams its answer one token at a time, and the text to speech engine has to turn that stream into audio the caller can listen to. Somewhere between those two steps you have to decide when a piece of text is ready to be spoken. That decision sounds small, but it affects two things callers notice right away: how long they wait before the agent starts talking, and whether the agent sounds like a person or like a robot reading words off a list.&lt;/p&gt;

&lt;p&gt;This post walks through the problem and a simple chunker in TypeScript that I'd use as a starting point. It isn't tied to one TTS provider, so check your provider's docs for the parts that differ.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why you can't just send every token
&lt;/h2&gt;

&lt;p&gt;The obvious fast option is to forward each token to TTS as soon as it arrives. The problem is that TTS models decide pitch, stress and pauses from the words around each word. If you give the engine "I can" and then, a moment later, "book that", it has no idea the first part belongs to a longer sentence. You often get odd stress, little breaks between chunks, or a falling tone in the middle of a sentence, as if the agent finished speaking and then changed its mind.&lt;/p&gt;

&lt;p&gt;The obvious safe option is to wait for the whole answer and send it at once. That sounds great, but now the caller waits for the full LLM response before hearing anything. For a three sentence answer that can easily add a second or more of silence, and on a phone call silence feels much longer than it does in a chat window.&lt;/p&gt;

&lt;p&gt;So the real job is in between. You want to send text in pieces that are big enough to sound natural and small enough that the first audio starts quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sentences are the natural unit
&lt;/h2&gt;

&lt;p&gt;A full sentence is usually the best chunk. It carries enough context for the TTS engine to get the tone right, and the end of a sentence is a place where a short pause sounds normal anyway. If the boundary between two chunks falls where a person would pause, the caller won't hear the seam.&lt;/p&gt;

&lt;p&gt;The first chunk is special. Everything the caller hears later can be generated while earlier audio is playing, but nothing hides the wait before the first sound. So I treat the first chunk differently: I'm happy to send a short opening like "Sure." or "Okay, let me check." on its own, because getting sound out early matters more than perfect flow for that first bit. After that, I prefer longer chunks, because the audio already playing gives the next chunk time to be ready.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the end of a sentence is harder than it looks
&lt;/h2&gt;

&lt;p&gt;Splitting on a period breaks quickly on real text. A few examples from the kind of answers a booking or support agent gives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Your appointment is with Dr. Khan at 3.30 tomorrow." has two periods before the real end.&lt;/li&gt;
&lt;li&gt;"Check example.com for the form." has a period inside a word.&lt;/li&gt;
&lt;li&gt;"The total is 3.5 hours." has a decimal point.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is also a timing problem. When the stream ends with "Dr." you can't know yet whether the sentence is over, because the next token might be " Khan". The chunker has to wait for at least one more character before it decides. Here's a small version that handles these cases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;ABBREVIATIONS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"dr"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"mr"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"mrs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ms"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"st"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"vs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"etc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"e.g"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"i.e"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Returns the index just after a safe cut point, or -1 if we should keep waiting.&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findCut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minChars&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;isEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"!"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"?"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isEnd&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;minChars&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// can't tell yet, wait for more text&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"\n"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!/&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "3.5", "example.com"&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;lastWord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;+/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="no"&gt;ABBREVIATIONS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastWord&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "Dr. Khan"&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;minChars&lt;/code&gt; value is what makes the first chunk special. For the first chunk I pass a small number so "Sure." goes out right away. For later chunks I pass something like 40, which merges very short sentences with the next one so the agent doesn't speak in tiny bursts. The abbreviation list will never be complete, and that's fine. A wrong cut costs a slightly odd pause, not a broken call, so start with the words your agent actually says and grow the list from your transcripts.&lt;/p&gt;

&lt;h2&gt;
  
  
  The streaming loop
&lt;/h2&gt;

&lt;p&gt;With &lt;code&gt;findCut&lt;/code&gt; in place, the loop that sits between the LLM and TTS is short:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;TtsStream&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// tell the engine no more text is coming for this turn&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AsyncIterable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TtsStream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;cut&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cut&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findCut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trimStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things are easy to forget here. The first is the leftover text at the end. Plenty of LLM answers end without a final period, or with a question mark that is the very last character in the stream, so whatever is left in the buffer has to be sent when the stream closes. The second is telling the TTS engine that the turn is over. Many streaming TTS APIs hold back the last bit of text while they wait for more context, so if you never signal the end, the final words may come out late or not at all. How you signal it depends on the provider, so look for a flush or end-of-input message in its docs.&lt;/p&gt;

&lt;h2&gt;
  
  
  A sentence that never ends
&lt;/h2&gt;

&lt;p&gt;Sometimes the model writes one long sentence full of commas, and your chunker sits there waiting for a period while the caller hears nothing. A simple fix is a fallback: if the buffer grows past a limit, say around 150 characters, cut at the last comma or semicolon instead. You can also add a timer, so if no cut has happened for a few hundred milliseconds and there is a usable comma in the buffer, you send up to it. Commas are weaker pauses than full stops, but a slightly early pause is much better than a long silence.&lt;/p&gt;

&lt;p&gt;Prompting helps too. Asking the model to answer in short, plain sentences, the way people talk on the phone, makes the chunker's job easier and usually makes the agent sound better anyway.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clean the text before it is spoken
&lt;/h2&gt;

&lt;p&gt;LLMs like to format their answers for a screen. A TTS engine will happily read out asterisks, hash signs or a bulleted list in a strange flat voice. Tell the model in the prompt that its output is spoken, not shown, and then strip leftover markdown in code before you send the chunk, because the prompt won't win every time.&lt;/p&gt;

&lt;p&gt;Numbers, times, prices and dates are the other trap. Engines handle "$1,200", "10:30" or "2026-11-24" in different ways, and some read phone numbers as one huge number. Don't guess how yours behaves. Write a short list of the formats your agent really says, run them through your TTS once, and listen. Then normalize only the ones that come out wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the caller interrupts
&lt;/h2&gt;

&lt;p&gt;Chunking also matters for barge-in. When the caller starts talking over the agent, you need to stop the audio and also throw away whatever text is still in the buffer and whatever the TTS engine has queued. If you only stop playback, the next chunk can start a second later and the agent seems to ignore the caller. Smaller chunks after the first one make this cheaper too, because less audio is already generated and wasted when you cancel.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to measure
&lt;/h2&gt;

&lt;p&gt;When you tune &lt;code&gt;minChars&lt;/code&gt; and the fallback limit, measure the time from the end of the caller's speech to the first agent audio, and also listen to real calls. The numbers tell you whether the first chunk is fast enough. Only listening tells you whether the joins between chunks sound natural. If they don't, try longer chunks after the first one before you try anything clever, since that change alone fixes most of the choppy sound.&lt;/p&gt;

</description>
      <category>voiceai</category>
      <category>ai</category>
      <category>typescript</category>
      <category>llm</category>
    </item>
    <item>
      <title>Let Me Talk to a Person: Handing Off a Voice Agent Call Without Making the Caller Repeat Themselves</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Sat, 21 Nov 2026 16:40:49 +0000</pubDate>
      <link>https://dev.to/aqibnawab/let-me-talk-to-a-person-handing-off-a-voice-agent-call-without-making-the-caller-repeat-themselves-57i4</link>
      <guid>https://dev.to/aqibnawab/let-me-talk-to-a-person-handing-off-a-voice-agent-call-without-making-the-caller-repeat-themselves-57i4</guid>
      <description>&lt;p&gt;When a caller says "let me talk to a person", a voice agent has one job left: get them to a person quickly, and make sure that person already knows why they're calling. It sounds simple, but it's one of the places where voice agents most often annoy people. The agent asks the caller to explain the problem one more time, or tries to "help with that first", or transfers the call and the human answers with "Hi, how can I help you?" as if the last three minutes never happened.&lt;/p&gt;

&lt;p&gt;The voice assistant I built for a dental course platform could hand off to an instructor, and the rule I'd keep for any agent like it is this: transfer on the first clear ask, and pass a short summary along so nobody has to repeat themselves. This post walks through how to do that on a Twilio phone line, with small TypeScript snippets for each part.&lt;/p&gt;

&lt;h2&gt;
  
  
  Treat the request as an action, not a topic
&lt;/h2&gt;

&lt;p&gt;The first mistake is letting the LLM decide whether a handoff request is worth honoring. If the caller says "can I speak to someone?" and the model replies "Sure, but first, can you tell me more about the issue?", you've already lost that caller's trust. A person who asked for a human once and got pushed back will ask again, louder, and the rest of the call goes badly.&lt;/p&gt;

&lt;p&gt;So I give the model a tool for it, something like &lt;code&gt;transfer_to_human&lt;/code&gt;, and the prompt says plainly that when the caller asks for a person, the agent calls the tool right away. I also don't rely on the model alone. A cheap check on the final transcript of each caller turn catches the common phrasings even if the model gets distracted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;HANDOFF_PATTERNS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;talk&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;speak&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;transfer&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;\&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;\&lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;human&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;someone&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;representative&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;instructor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;\&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;breal&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;\&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;boperator&lt;/span&gt;\&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;wantsHuman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;callerText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;HANDOFF_PATTERNS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;callerText&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This list will have false positives. "I already talked to someone yesterday" matches the first pattern. That's why the check doesn't transfer the call by itself. It flags the turn, and if the model didn't call the tool on a flagged turn, the agent asks one short question: "Do you want me to connect you to someone now?" A yes goes straight to the transfer. That one confirmation is fine. What isn't fine is a loop of clarifying questions before you let the caller go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the transfer happen only once
&lt;/h2&gt;

&lt;p&gt;Tool calls can repeat. The model might call &lt;code&gt;transfer_to_human&lt;/code&gt; twice in one reply, or the regex flag and the tool can fire for the same turn. If each of those starts a new outbound dial, the human's phone rings twice, or the second update cancels the first one halfway through.&lt;/p&gt;

&lt;p&gt;I keep the handoff as a small piece of call state and make the transfer function check it first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;HandoffState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"none"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"requested"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"dialing"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"connected"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"failed"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;startHandoff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CallSession&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;handoff&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"none"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// already in progress&lt;/span&gt;
  &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;handoff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"requested"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;summary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buildSummary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;transferCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because a single call is handled by one WebSocket connection on one server, a plain field on the session object is enough here. If your call state lives somewhere shared, like Redis, you'd want an atomic "set if still none" instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Moving the call off the media stream
&lt;/h2&gt;

&lt;p&gt;If your agent runs on a bidirectional Media Stream, the call is executing &lt;code&gt;&amp;lt;Connect&amp;gt;&amp;lt;Stream&amp;gt;&lt;/code&gt; TwiML and your server is exchanging audio over a WebSocket. To hand the call to a human, you give the call new TwiML through the REST API. Twilio stops the current instructions, which ends the stream, and runs the new ones instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;twilio&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"twilio"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;twilio&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TWILIO_ACCOUNT_SID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TWILIO_AUTH_TOKEN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;transferCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CallSession&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;vr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;twilio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;twiml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;VoiceResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;vr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;say&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Okay, I'm connecting you to someone now."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;dial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`${BASE_URL}/handoff-status?call=${call.id}`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;dial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`${BASE_URL}/whisper?call=${call.id}`&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;HUMAN_NUMBER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;handoff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"dialing"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;callSid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;twiml&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;vr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two attributes do most of the work here. The &lt;code&gt;url&lt;/code&gt; on &lt;code&gt;&amp;lt;Number&amp;gt;&lt;/code&gt; is a "whisper": Twilio fetches that URL when the human picks up and plays the TwiML it returns to them only, before the two calls are bridged. The &lt;code&gt;action&lt;/code&gt; on &lt;code&gt;&amp;lt;Dial&amp;gt;&lt;/code&gt; is the URL Twilio requests after the dialed leg ends, and it includes a &lt;code&gt;DialCallStatus&lt;/code&gt; parameter such as &lt;code&gt;completed&lt;/code&gt;, &lt;code&gt;busy&lt;/code&gt;, &lt;code&gt;no-answer&lt;/code&gt;, &lt;code&gt;failed&lt;/code&gt; or &lt;code&gt;canceled&lt;/code&gt;. That's how you find out the transfer didn't work.&lt;/p&gt;

&lt;p&gt;One small detail: say the "connecting you now" line in the new TwiML, not with your TTS right before the update. If you send TTS audio over the stream and then update the call, the stream can close before Twilio has played that audio, and the caller hears silence and then ringing.&lt;/p&gt;

&lt;h2&gt;
  
  
  The summary is the part people notice
&lt;/h2&gt;

&lt;p&gt;The whisper endpoint returns a short spoken brief for the human:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/whisper"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sessions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;vr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;twilio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;twiml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;VoiceResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;vr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;say&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;summary&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;"Transferred call from the voice assistant."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"text/xml"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What goes into that summary matters more than the plumbing. A good one answers three questions in a couple of sentences: who is calling, what they want, and what the agent already did. For a course platform that might be: "Sara, enrolled in the November batch. She's asking why her quiz score didn't save. I checked and the quiz shows as submitted with no score."&lt;/p&gt;

&lt;p&gt;I build the summary mostly from structured call state rather than asking the model to retell the whole conversation. The caller's name, the course or account the agent looked up, and the tools it ran with their results are all facts the system already has. The model only writes the one line about what the caller wants. That keeps the summary short enough to hear in a few seconds, and it stops the model from inventing things that sound right but never happened.&lt;/p&gt;

&lt;p&gt;This is also where the last post's fix pays off. If your conversation history only contains what the caller actually heard, the summary won't claim the agent told them something it never got to say.&lt;/p&gt;

&lt;p&gt;A spoken whisper is easy to forget, so I'd also write the same summary somewhere the human can read it, such as the ticket, the CRM record or a message in the support channel, keyed by the caller's number.&lt;/p&gt;

&lt;h2&gt;
  
  
  When nobody picks up
&lt;/h2&gt;

&lt;p&gt;Transfers fail. It's after hours, everyone is busy, or the call just isn't answered within the timeout. When the &lt;code&gt;action&lt;/code&gt; URL reports anything other than a completed call, the worst thing you can do is drop the caller back into the bot with no explanation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/handoff-status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sessions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DialCallStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;vr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;twilio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;twiml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;VoiceResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;handoff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"connected"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;vr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hangup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;handoff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"failed"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;createCallbackTicket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;vr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;say&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Sorry, nobody could take the call right now. I've passed your details on, and someone will call you back."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"text/xml"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;completed&lt;/code&gt; here means the human answered and the bridged call has since ended, so hanging up is right. For the failure case, be honest and specific. Don't promise a callback time you can't control, and don't send the caller back to the agent unless they ask for it. If you check your hours before dialing, you can skip the ringing entirely and go straight to the callback message.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things I'd test
&lt;/h2&gt;

&lt;p&gt;Most of this can be checked with a handful of test calls:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ask for a human in the very first sentence of the call, before the agent knows anything. The summary should still say something useful, even if it's only "caller asked for a person right away".&lt;/li&gt;
&lt;li&gt;Ask for a human in the middle of the agent's sentence, so the request arrives together with an interruption.&lt;/li&gt;
&lt;li&gt;Say "I already spoke to someone yesterday" and check that it doesn't trigger a transfer without asking.&lt;/li&gt;
&lt;li&gt;Make the human's phone not answer and listen to what the caller hears afterwards.&lt;/li&gt;
&lt;li&gt;Ask twice in a row and make sure the human's phone rings once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of this needs a fancy model. The handoff is mostly about respecting the caller's request quickly and making sure the human starts the conversation already knowing what's going on.&lt;/p&gt;

</description>
      <category>voiceai</category>
      <category>twilio</category>
      <category>typescript</category>
      <category>ai</category>
    </item>
    <item>
      <title>When the Caller Cuts In: Keeping a Voice Agent's Memory to What Was Actually Heard</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Thu, 19 Nov 2026 16:38:09 +0000</pubDate>
      <link>https://dev.to/aqibnawab/when-the-caller-cuts-in-keeping-a-voice-agents-memory-to-what-was-actually-heard-28lh</link>
      <guid>https://dev.to/aqibnawab/when-the-caller-cuts-in-keeping-a-voice-agents-memory-to-what-was-actually-heard-28lh</guid>
      <description>&lt;p&gt;When a caller cuts off a voice agent halfway through a sentence, two things have to happen. The audio has to stop right away, and the agent's memory of the conversation has to be fixed so it only contains the words the caller actually heard. Most of us get the first part working in an afternoon. The second part is easy to miss, and when it's missing the agent starts acting on things the caller never heard.&lt;/p&gt;

&lt;p&gt;This post walks through both parts on a Twilio phone line, with a short note on how the OpenAI Realtime API handles the same problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the transcript drifts
&lt;/h2&gt;

&lt;p&gt;LLMs and TTS engines produce audio faster than real time. By the time the caller hears the first word of a reply, your server may have already generated and sent the whole thing. So your conversation history says the assistant spoke three full sentences, while the caller only heard the first five words before saying "no, wait".&lt;/p&gt;

&lt;p&gt;Here's a simple example. The agent is booking a dentist appointment:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Agent (generated): "I can book you for Tuesday at 3 PM. I'll also cancel your Friday slot so you don't have two bookings. Is that okay?"&lt;/p&gt;

&lt;p&gt;Caller (heard): "I can book you for Tuesday at..." and then says "Actually, can we do Wednesday?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the full reply stays in the history, the model now believes it told the caller about cancelling Friday, and that the caller moved on without objecting. A few turns later it might say "as I mentioned, your Friday slot is cancelled." The caller never heard that. Worse, if your code treats "the agent said it and the caller didn't object" as a kind of consent, you've built a real bug.&lt;/p&gt;

&lt;p&gt;The fix is to keep a record of what was played, separate from what was generated, and cut the history down to the played part when an interruption happens.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: stop the audio for real
&lt;/h2&gt;

&lt;p&gt;On a bidirectional Twilio Media Stream (the kind you start with &lt;code&gt;&amp;lt;Connect&amp;gt;&amp;lt;Stream&amp;gt;&lt;/code&gt;), you send audio to the call as &lt;code&gt;media&lt;/code&gt; messages. Twilio buffers that audio and plays it. If you only stop your LLM and TTS when the caller speaks, Twilio keeps playing whatever is already in its buffer, which can be a few seconds of speech.&lt;/p&gt;

&lt;p&gt;To flush that buffer, you send a &lt;code&gt;clear&lt;/code&gt; message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;stopPlayback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;streamSid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"clear"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;streamSid&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is easy to forget when you use a framework adapter, so it's worth testing on a real phone call and not only in a browser demo. Talk over the agent and listen for how long it keeps going.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: track what was played with marks
&lt;/h2&gt;

&lt;p&gt;Twilio gives you one tool for knowing what was played: the &lt;code&gt;mark&lt;/code&gt; message. After you send some audio, you send a mark with a name. When that audio finishes playing, Twilio sends the mark back with the same name. If you send &lt;code&gt;clear&lt;/code&gt;, Twilio empties the buffer and also sends back any marks that were still waiting.&lt;/p&gt;

&lt;p&gt;That last detail matters. A returned mark doesn't tell you whether the audio finished or got cleared. The message looks the same in both cases. So your own code has to know which state it's in. A simple rule works: any mark that comes back after you sent &lt;code&gt;clear&lt;/code&gt; belongs to audio that was thrown away.&lt;/p&gt;

&lt;p&gt;I send audio in sentence-sized chunks and put a mark after each one. Each chunk keeps its text and its length in milliseconds. Twilio expects mu-law audio at 8000 Hz, which is one byte per sample, so 8 bytes of decoded audio equals 1 millisecond. That makes the duration math simple.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;durationMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;startedAt&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// when we believe it started playing&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlaybackTracker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;spoken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;clearing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;seq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;streamSid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mulaw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;mark&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`chunk-${++this.seq}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;durationMs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mulaw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 8000 bytes per second&lt;/span&gt;
    &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"media"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;streamSid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;media&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;mulaw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"base64"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"mark"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;streamSid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;mark&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;durationMs&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startedAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;onMark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mark&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clearing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;spoken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startedAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clearing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: work out the cut point
&lt;/h2&gt;

&lt;p&gt;When the caller barges in, the first chunk in the queue is the one that was playing. Everything before it was heard in full, because its mark already came back. Everything after it was never heard.&lt;/p&gt;

&lt;p&gt;For the playing chunk you have to estimate. You know roughly when it started (when the previous mark came back) and how long it is, so the fraction played is the elapsed time divided by its duration. Then you keep that fraction of the words. Add this method to the tracker:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;interrupt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;streamSid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clearing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"clear"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;streamSid&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startedAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;elapsed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startedAt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ratio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;elapsed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;durationMs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;kept&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ratio&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kept&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;spoken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kept&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;spoken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an estimate. Network delay and Twilio's own buffering shift the timing a bit, and words aren't all the same length. A few things make it better:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use word timings if your TTS gives them.&lt;/strong&gt; Some TTS providers can return a timestamp for each word or character. Then you cut at the last word whose end time is before the elapsed time, instead of guessing from word count.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Round down.&lt;/strong&gt; If you're not sure whether a word was heard, drop it. It's safer for the agent to repeat something than to assume the caller heard it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep chunks short.&lt;/strong&gt; Sentence-sized chunks mean the estimate only has to cover one sentence, not a whole paragraph.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 4: rewrite the history
&lt;/h2&gt;

&lt;p&gt;Once you have the spoken text, replace the assistant's last message with it before the next LLM call. I also add a small note so the model knows it was cut off. Without that, the model sometimes picks up as if it had finished its point.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;applyInterruption&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;spokenText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;role&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"assistant"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;spokenText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="sb"&gt;`${spokenText} [interrupted by caller]`&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"[interrupted before speaking]"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, the dentist example becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;assistant: I can book you for Tuesday at... [interrupted by caller]
user:      Actually, can we do Wednesday?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the model knows Friday was never mentioned, so it won't claim it was.&lt;/p&gt;

&lt;h2&gt;
  
  
  The tool call trap
&lt;/h2&gt;

&lt;p&gt;There's one case where trimming text isn't enough. Sometimes the agent calls a tool and then speaks about it, like "Done, I've cancelled your Friday slot." If the caller interrupts that sentence, the words get cut, but the cancellation already happened.&lt;/p&gt;

&lt;p&gt;So keep tool results in the history even when the speech about them was cut. The history should show what actually happened in your systems, plus what the caller actually heard. If those two don't match, the model needs both facts so it can tell the caller about the change on its next turn. For risky actions, the safer design is to confirm before calling the tool at all, but that's a topic on its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you use the OpenAI Realtime API
&lt;/h2&gt;

&lt;p&gt;The Realtime API has this built in. When the caller interrupts, you send a &lt;code&gt;conversation.item.truncate&lt;/code&gt; event with the assistant item's &lt;code&gt;item_id&lt;/code&gt;, a &lt;code&gt;content_index&lt;/code&gt; of 0, and &lt;code&gt;audio_end_ms&lt;/code&gt; set to how much audio was actually played. The server cuts the audio at that point and removes the text transcript past it, so the model's context only holds what the user heard. It replies with a &lt;code&gt;conversation.item.truncated&lt;/code&gt; event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="n"&gt;realtimeWs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"conversation.item.truncate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;item_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;currentAssistantItemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;content_index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;audio_end_ms&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;playedMs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You still have to know &lt;code&gt;playedMs&lt;/code&gt; yourself, though. The server can't see your playback buffer, and on a phone line that buffer lives at Twilio. So the mark tracking above is still the part you build. One more thing to watch: if &lt;code&gt;audio_end_ms&lt;/code&gt; is longer than the real audio, the server returns an error, so clamp it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not every sound is a barge-in
&lt;/h2&gt;

&lt;p&gt;One last thing. All of this assumes the interruption was real. A caller saying "mm-hmm" or a cough on the line shouldn't stop the agent and trim its reply. If your barge-in trigger is just "voice activity detected," you'll cut the agent off all the time and fill the history with "[interrupted by caller]" notes that mean nothing. It helps to wait for a short stretch of real speech, or a couple of transcribed words, before you call &lt;code&gt;interrupt()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Send &lt;code&gt;clear&lt;/code&gt; to Twilio on barge-in, not just stop generation.&lt;/li&gt;
&lt;li&gt;Put a mark after each audio chunk and keep the chunk's text and duration.&lt;/li&gt;
&lt;li&gt;Treat marks that come back after &lt;code&gt;clear&lt;/code&gt; as unplayed audio.&lt;/li&gt;
&lt;li&gt;Estimate the cut point in the playing chunk, and round down.&lt;/li&gt;
&lt;li&gt;Replace the last assistant message with the spoken part and mark it as interrupted.&lt;/li&gt;
&lt;li&gt;Keep tool results even when the speech about them was cut.&lt;/li&gt;
&lt;li&gt;On the Realtime API, send &lt;code&gt;conversation.item.truncate&lt;/code&gt; with the played duration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The test I use is simple: interrupt the agent mid-sentence on a real call, then ask it "what did you just say?" If it repeats words you never heard, the history is still wrong.&lt;/p&gt;

</description>
      <category>voiceai</category>
      <category>twilio</category>
      <category>typescript</category>
      <category>ai</category>
    </item>
    <item>
      <title>Person or Voicemail? Handling the First Seconds of an Outbound Voice Agent Call</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Tue, 17 Nov 2026 16:46:07 +0000</pubDate>
      <link>https://dev.to/aqibnawab/person-or-voicemail-handling-the-first-seconds-of-an-outbound-voice-agent-call-3can</link>
      <guid>https://dev.to/aqibnawab/person-or-voicemail-handling-the-first-seconds-of-an-outbound-voice-agent-call-3can</guid>
      <description>&lt;p&gt;When a voice agent makes an outbound call, the first thing it has to work out is who, or what, answered. It might be a person saying "Hello?", a voicemail greeting, a company phone tree, or hold music. Outbound calling is part of what I work on in Quick Start, and this first moment shapes the rest of the call more than people expect. If the agent guesses wrong, it either pitches to a voicemail box for thirty seconds or sits quietly while a real person says "hello?" twice and hangs up.&lt;/p&gt;

&lt;p&gt;Meta's Muse recently started placing calls to US businesses for its users, and those calls run straight into the same problem from the other side: phone trees, long holds, and recorded "please stay on the line" messages that sound a lot like a person. So I want to walk through how answering machine detection actually works, where it goes wrong, and how I'd structure an agent around it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How answering machine detection decides
&lt;/h2&gt;

&lt;p&gt;Most telephony providers offer answering machine detection (AMD), and it's less magical than the name suggests. Twilio's docs describe their version plainly: it isolates speech in the greeting and measures the periods of speech and silence, then uses that pattern to decide who answered.&lt;/p&gt;

&lt;p&gt;The idea is that people and machines answer differently. A person usually says something short like "Hello?" and then stops to wait for you. A voicemail greeting is a longer stretch of speech, like "Hi, you've reached Sara, I can't take your call right now, please leave a message after the tone."&lt;/p&gt;

&lt;p&gt;Twilio exposes the timing rules as parameters, and the defaults tell you a lot about the design:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Parameter&lt;/th&gt;
&lt;th&gt;Default&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;MachineDetectionSpeechThreshold&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2400 ms&lt;/td&gt;
&lt;td&gt;Speech shorter than this leans human, longer leans machine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;MachineDetectionSpeechEndThreshold&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1200 ms&lt;/td&gt;
&lt;td&gt;Silence after speech that counts as "they finished talking"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;MachineDetectionSilenceTimeout&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;5000 ms&lt;/td&gt;
&lt;td&gt;Initial silence after which the result is &lt;code&gt;unknown&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;MachineDetectionTimeout&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;30 s&lt;/td&gt;
&lt;td&gt;Total time AMD may take before returning &lt;code&gt;unknown&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The result arrives in an &lt;code&gt;AnsweredBy&lt;/code&gt; field. With &lt;code&gt;MachineDetection=Enable&lt;/code&gt; you get &lt;code&gt;human&lt;/code&gt;, &lt;code&gt;machine_start&lt;/code&gt;, &lt;code&gt;fax&lt;/code&gt; or &lt;code&gt;unknown&lt;/code&gt;. With &lt;code&gt;DetectMessageEnd&lt;/code&gt;, Twilio keeps listening on a machine until the greeting ends, so you get values like &lt;code&gt;machine_end_beep&lt;/code&gt; or &lt;code&gt;machine_end_silence&lt;/code&gt;, which is what you want if you plan to leave a message after the tone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the timing rules break
&lt;/h2&gt;

&lt;p&gt;Because the decision is based on timing, the hard cases are the ones where people and machines swap their usual patterns.&lt;/p&gt;

&lt;p&gt;The first is the long human greeting. Someone answering a business line might say "Good afternoon, Northside Dental, this is Maria, how can I help you?" That's easily longer than 2.4 seconds of continuous speech, so it can be marked as a machine. Twilio calls this a False Machine, and raising the speech threshold reduces it, at the cost of taking longer to detect real machines.&lt;/p&gt;

&lt;p&gt;The second is the short voicemail greeting. Twilio's docs note that short voicemails often have 1 to 2 seconds of audio, then 1.2 to 2.4 seconds of silence, then more audio before the beep. To the detector, "Hi, it's Sara" followed by a pause looks exactly like a person waiting for you to speak. That's a False Human. Their suggested fix is raising the speech end threshold to around 2500 ms, so the pause counts as a gap inside the greeting. The trade-off is that every human detection now takes about that much longer too.&lt;/p&gt;

&lt;p&gt;So you can't tune your way out of both errors at once. You pick which mistake costs you less for your use case, and you handle the other one in the agent itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Synchronous vs async detection
&lt;/h2&gt;

&lt;p&gt;This is the part that matters most for how the call feels.&lt;/p&gt;

&lt;p&gt;With synchronous AMD, which is Twilio's default, your call instructions run only after the detection result is ready. That's simple to code, but it means the person who picked up hears nothing while the detector is still deciding. They say "Hello?", wait, say it again, and some of them hang up before your agent has said a word.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;AsyncAmd=true&lt;/code&gt;, the call connects and your instructions run right away, while detection keeps going in the background. The result is posted to your &lt;code&gt;AsyncAmdStatusCallback&lt;/code&gt; URL when it's ready. That lets the agent start a short, neutral opening while the answer is still unknown, and then switch behavior once the callback lands.&lt;/p&gt;

&lt;p&gt;There's one detail in Twilio's docs that's easy to miss if your agent streams audio over Media Streams. While async AMD is analyzing a call, it uses one of the four forked audio streams allowed per call, and that limit is shared with Media Streams, SIPREC and real-time transcription. If you start several of those at once, you can hit the limit, so Twilio suggests starting them after the async AMD callback arrives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Treat detection as a hint, not a verdict
&lt;/h2&gt;

&lt;p&gt;I prefer to treat the AMD result as one input to a small state machine, rather than as the final answer. The agent also hears the audio itself, so it can correct a wrong guess. A rough version looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;CallState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"connecting"&lt;/span&gt;   &lt;span class="c1"&gt;// answered, AMD still running&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"human"&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"voicemail"&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"phone_tree"&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"on_hold"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;onAmdResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answeredBy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CallState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CallState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"connecting"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the agent already learned more&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answeredBy&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"human"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"human"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answeredBy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"machine"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"voicemail"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"connecting"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "unknown": keep listening instead of guessing&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important line is the first check. If the transcript already showed the other side answering a question, a late &lt;code&gt;machine_start&lt;/code&gt; callback shouldn't make the agent drop into voicemail mode in the middle of a real conversation. The reverse also helps: if the agent said hello and the other side kept talking right over it with "please leave a message after the tone", that's voicemail, whatever the detector said.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;unknown&lt;/code&gt; result deserves its own handling. It usually means a long silence or a strange greeting, so the safe move is a short prompt like "Hi, is this a good time?" and then listening, rather than launching into the full script.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phone trees and hold are a different problem
&lt;/h2&gt;

&lt;p&gt;AMD is built to separate people from voicemail. It wasn't designed for an agent that has to get through "press 1 for billing" and then wait on hold, which is what a Muse-style assistant does. In that case the agent needs a few extra behaviors.&lt;/p&gt;

&lt;p&gt;On a phone tree, the agent should listen to the whole menu before choosing, then answer with DTMF tones rather than speech when the menu asks for a keypress. Twilio supports this with &lt;code&gt;&amp;lt;Play digits="..."&amp;gt;&lt;/code&gt; in TwiML, or the &lt;code&gt;SendDigits&lt;/code&gt; parameter when the call is created if you already know the path.&lt;/p&gt;

&lt;p&gt;On hold, the agent should stay silent. Hold music and repeated recordings like "your call is important to us" will reach the speech-to-text step, and an agent that treats every transcript as a turn will start replying to the music. I'd keep the agent in an &lt;code&gt;on_hold&lt;/code&gt; state where transcripts are only checked for signs of a live person, such as a new voice asking "how can I help you?" or a direct question that expects an answer.&lt;/p&gt;

&lt;p&gt;The trickiest moment is the jump from recording to person. Many hold loops repeat a recorded voice every so often, and it can say "Hi, thanks for waiting" in a friendly human tone. One check I'd use is repetition: if the same sentence came up earlier in the call, it's almost certainly the recording.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I'd test it
&lt;/h2&gt;

&lt;p&gt;Timing rules and state machines are only as good as the audio you test them against, so I'd build a small set of real greetings from calls you're allowed to record. That set should include short and long voicemails, business greetings, people who answer with a long pause, phone trees, and hold loops. Then score each case on two things: whether the agent chose the right state, and how long the person waited in silence before the agent spoke.&lt;/p&gt;

&lt;p&gt;That second number is the one users actually feel. A detector that's right every time but leaves people saying "hello?" into silence still loses the call.&lt;/p&gt;

</description>
      <category>voiceai</category>
      <category>twilio</category>
      <category>typescript</category>
      <category>ai</category>
    </item>
    <item>
      <title>The TV Said Yes: Confirming Risky Actions in Voice Agents</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Sat, 14 Nov 2026 16:25:54 +0000</pubDate>
      <link>https://dev.to/aqibnawab/the-tv-said-yes-confirming-risky-actions-in-voice-agents-41ca</link>
      <guid>https://dev.to/aqibnawab/the-tv-said-yes-confirming-risky-actions-in-voice-agents-41ca</guid>
      <description>&lt;p&gt;Bland published a page for Tofu-1, its new speech-to-speech model, and one section on it has stuck with me. They call it the TV Test. The agent asks, "Just to confirm, you want to cancel your policy?" The caller says nothing because she's still looking for her policy number. A TV in the room says "Yes!" A transcript-based agent can take that yes as her answer and cancel the policy.&lt;/p&gt;

&lt;p&gt;Bland marks this as an illustrative scenario, not a recorded model output, so I'm not treating it as a benchmark result. But the failure it describes is real for anyone running a cascade (STT, then LLM, then TTS). The transcript says "yes", and nothing in the text tells you who said it.&lt;/p&gt;

&lt;p&gt;I build Quick Start AI, a product for inbound and outbound AI calling, campaigns and lead qualification, so I think about this a lot. This post is about the parts you can control today without a new model: how audio cleanup helps, where it stops helping, and how to design confirmations for risky actions so one stray word can't trigger them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the transcript loses the speaker
&lt;/h2&gt;

&lt;p&gt;In a cascade, the STT turns audio into words, and the words are what the LLM sees. Bland's page puts it well: each step discards part of the call, like who was speaking, who they were speaking to, and whether they had finished.&lt;/p&gt;

&lt;p&gt;A TV is the hard case because what it plays is clear speech, often louder than the caller. A noise filter is trained to keep speech and remove everything else, so the TV's "Yes!" passes straight through. On a phone line it gets worse because you usually get one mixed mono channel. Twilio Media Streams, for example, sends &lt;code&gt;audio/x-mulaw&lt;/code&gt; at 8000 Hz with one channel. There's no separate track for the TV.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background voice cancellation helps, with limits
&lt;/h2&gt;

&lt;p&gt;There are models built for this. Krisp's Background Voice Cancellation (BVC) is designed to remove other people's voices around the user, plus noise and reverb, without voice enrollment. LiveKit ships it for agents through its Python and Node.js plugins, with a standard &lt;code&gt;BVC()&lt;/code&gt; model and a &lt;code&gt;BVCTelephony()&lt;/code&gt; variant for phone calls. In Node it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;BackgroundVoiceCancellation&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@livekit/noise-cancellation-node'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;AudioStream&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@livekit/rtc-node'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AudioStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;track&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;noiseCancellation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BackgroundVoiceCancellation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'd turn this on for any agent that takes calls in homes, cars or offices. But I wouldn't let it carry the safety of a destructive action by itself, for three reasons.&lt;/p&gt;

&lt;p&gt;First, read the fine print. Krisp's own docs say its BVC model works at 32 kHz, works well above 8 kHz, and isn't compatible with narrowband audio at 8 kHz or lower. That's exactly what a PSTN call looks like, which is why the telephony variant exists, and you should test that variant on your own call audio before trusting it. Second, any model makes mistakes on some share of calls, and a cancelled policy is expensive to undo. Third, Bland makes a point I agree with: volume can't settle the question. A loud TV can be irrelevant, and a quiet correction from the caller can be the most important thing on the call.&lt;/p&gt;

&lt;p&gt;So treat audio cleanup as a layer that lowers how often the bad input shows up. The confirmation design is what decides what happens when it does show up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Split your tools by risk
&lt;/h2&gt;

&lt;p&gt;Not every tool call needs the same care. Looking up an appointment slot is cheap to get wrong. Cancelling a policy, sending a payment, deleting an account, or marking a lead as do-not-call is not. I like to tag each tool with a risk level in code, so the rule doesn't live only in the prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Risk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'read'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s1"&gt;'reversible'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s1"&gt;'destructive'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;tools&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;getPolicy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;risk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'read'&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;Risk&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;bookCallback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;risk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'reversible'&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;Risk&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;cancelPolicy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;risk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'destructive'&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;Risk&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reads run right away. Reversible actions run, and the agent tells the caller what it did. Destructive actions go through a confirmation gate that the LLM can't skip.&lt;/p&gt;

&lt;h2&gt;
  
  
  A confirmation a TV can't easily pass
&lt;/h2&gt;

&lt;p&gt;The weak pattern is a yes/no question. "Do you want to cancel?" can be answered by anyone in the room with one word. A better pattern asks the caller to say something only they would say in that moment, tied to the specific action.&lt;/p&gt;

&lt;p&gt;Compare these two prompts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Do you want to cancel your policy?"&lt;/li&gt;
&lt;li&gt;"I'm about to cancel the home policy ending in 4471. If that's right, please say the last four digits back to me."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The second one is longer, but it does several jobs. It names the exact thing being changed, so a confused caller can catch a mistake. It also asks for an answer a TV is very unlikely to say at that moment. A bare "yes" from anyone doesn't count.&lt;/p&gt;

&lt;p&gt;On phone calls there's another option: keypad input. Twilio's bidirectional Media Streams send a &lt;code&gt;dtmf&lt;/code&gt; event when someone presses a key, and the event says which digit was pressed. "Press 1 to confirm the cancellation" is clumsy for small talk, but for one high-stakes step it's a clear signal that someone holding the phone made a choice. Keypad tones can still show up in the audio from other sources, so I'd treat DTMF as strong evidence, not proof.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checks the gate can run
&lt;/h2&gt;

&lt;p&gt;Once a confirmation answer arrives, the gate can look at more than the words. Here's a sketch of what it checks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;PendingAction&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;tool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'cancelPolicy'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;policyId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;last4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;askedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// ms, when the agent finished asking&lt;/span&gt;
  &lt;span class="n"&gt;expiresAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;HeardAnswer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;confidence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;speaker&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;startedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// ms, when this speech began&lt;/span&gt;
  &lt;span class="n"&gt;callerSpeaker&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;checkConfirmation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PendingAction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HeardAnswer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'ok'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s1"&gt;'reask'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s1"&gt;'expired'&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expiresAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'expired'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Must contain the specific digits, not just "yes".&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;endsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'reask'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Speech that started before the question ended is not an answer to it.&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startedAt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;askedAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'reask'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Low ASR confidence on the key words means we ask again.&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;confidence&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'reask'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// If diarization says a different speaker answered, ask again.&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;callerSpeaker&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
      &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;speaker&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;speaker&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;callerSpeaker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'reask'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'ok'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few notes on this. The digit check assumes your STT writes spoken numbers as digits; Deepgram's &lt;code&gt;smart_format&lt;/code&gt; option does that kind of formatting, and other providers have their own settings. The 0.8 threshold is a placeholder, not a tuned number; you'd pick yours from your own call data. The speaker check depends on your STT. Deepgram, for example, puts a &lt;code&gt;speaker&lt;/code&gt; integer on each word when diarization is on, but in live streaming it doesn't return the &lt;code&gt;speaker_confidence&lt;/code&gt; field that pre-recorded results have. Diarization on a short "4471" can also be shaky. That's why the speaker check here only makes the gate stricter. It never approves anything on its own.&lt;/p&gt;

&lt;p&gt;The most important rule isn't in the function. The &lt;code&gt;cancelPolicy&lt;/code&gt; tool on the server should refuse to run unless there's a pending action with a matching id that passed this check. If the confirmation only lives in the prompt, a model that hears "yes" can still call the tool directly. Put the gate in the code path that does the work.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a good re-ask sounds like
&lt;/h2&gt;

&lt;p&gt;When the gate says &lt;code&gt;reask&lt;/code&gt;, the agent shouldn't repeat the same sentence louder. It should say why it's asking again, briefly, and keep the specifics:&lt;/p&gt;

&lt;p&gt;"Sorry, I didn't catch that clearly. To cancel the policy ending in 4471, please say those four digits, or press 1 on your keypad."&lt;/p&gt;

&lt;p&gt;If the gate fails twice, stop trying to finish the action on this call. Offer a callback, a human handoff, or a confirmation link by SMS, depending on what your product supports. Bland's page has a line I like here: ignoring the television must never become a reason to guess at what the caller said. Guessing is the one thing the gate exists to prevent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add the TV Test to your evals
&lt;/h2&gt;

&lt;p&gt;You don't need a new model to test this. Take a recorded or synthetic call where the agent asks a confirmation question, then mix in a second voice saying "yes" while the caller is silent. Run it through your pipeline and check one thing: did the destructive tool run? Then try variations, like the second voice speaking over the agent's question, the caller saying the digits quietly while the TV is loud, and the caller saying "wait, no" right after the TV's yes.&lt;/p&gt;

&lt;p&gt;These cases are cheap to build, and they test the part that matters. A model that hears better makes them pass more often. A gate in your tool layer makes sure a wrong pass doesn't cost a caller their policy.&lt;/p&gt;

</description>
      <category>voiceai</category>
      <category>ai</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How OpenAI Made Voice AI Feel Like a Real Conversation: 8 Engineering Changes</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Fri, 13 Nov 2026 19:02:20 +0000</pubDate>
      <link>https://dev.to/aqibnawab/how-openai-made-voice-ai-feel-like-a-real-conversation-8-engineering-changes-4p3p</link>
      <guid>https://dev.to/aqibnawab/how-openai-made-voice-ai-feel-like-a-real-conversation-8-engineering-changes-4p3p</guid>
      <description>&lt;p&gt;When a voice agent feels slow, most people blame the model. In practice, the model is only one of several places where time is lost.&lt;/p&gt;

&lt;p&gt;A voice call has a long path. The call has to connect. Audio has to travel. The system has to decide when you are done talking. Something has to think. Something has to speak. And all of this has to keep running for a huge number of calls at once.&lt;/p&gt;

&lt;p&gt;OpenAI has written about changes at almost every step of that path. This post walks through them in the order a call actually happens:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;connecting a call → starting it faster → scaling it → making the talk natural → handling hard tasks → saving CPU time → switching models → securing direct connections&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each section starts with a real problem, then shows the fix. Most sections have a small interactive diagram on the &lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;original post&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  First: how a voice agent call works, in plain words
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;voice agent&lt;/strong&gt; is a program that talks to you on a call. It can live on a website, in an app, or behind a normal phone number. Every time you speak, the same loop runs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Hear.&lt;/strong&gt; Your voice is cut into tiny pieces of audio and sent over the network to a server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Think.&lt;/strong&gt; A model works out what to say. It may also look something up or use a tool.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Speak.&lt;/strong&gt; The answer is turned into voice and sent back over the network.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The word you will see most in this post is &lt;strong&gt;latency&lt;/strong&gt;. It just means delay: the time between you stopping and the agent starting to answer.&lt;/p&gt;

&lt;p&gt;Why is this so strict for voice? In studies of conversations in many languages, the gap between one person finishing and the next one starting is usually around &lt;strong&gt;200 milliseconds&lt;/strong&gt;. That is a fifth of a second. An agent that takes one second to reply already feels slow. Several seconds of silence feels like a dropped call.&lt;/p&gt;

&lt;p&gt;And the delay does not come from one place. It is lost in the connection setup, in the network trips, in waiting to be sure you finished, in the model thinking and in making the voice. That is why OpenAI worked on so many parts at once. Here is the map for the rest of the post:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Map: where each change sits on a call&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The map has four boxes, in the order a call happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Connect the call&lt;/strong&gt; (before anyone can talk): 2. WARP + Instant Connect, and 1. Phone: SIP + webhook&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge and media&lt;/strong&gt; (carry the audio packets): 3. Relay + Transceiver, and 6. Python to Go&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The model&lt;/strong&gt; (listen, think, speak): 4. Full-duplex, 5. Background reasoning, and 7. Model handoffs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your app&lt;/strong&gt; (how it connects and stays safe): 8. Ephemeral tokens&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The numbers match the sections below. Each section zooms into one box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;Interactive version on my site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If a word is new to you, there is a small glossary at the end.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. How WebRTC connects you to an AI voice agent
&lt;/h2&gt;

&lt;p&gt;Before we talk about what changed, we need to know how a voice call reaches an AI agent in the first place.&lt;/p&gt;

&lt;p&gt;Most browser voice agents use &lt;strong&gt;WebRTC&lt;/strong&gt;. It is the same technology behind video calls in your browser. It is built for one job: move audio and video between two endpoints with very low delay. Instead of sending audio over a normal request and response channel, WebRTC sends it as small UDP packets, about 20 milliseconds of sound each.&lt;/p&gt;

&lt;p&gt;In a voice agent, one endpoint is the user. The other endpoint is not a person. It is a server that runs a speech model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How a call reaches an AI voice agent&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are two paths:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;From a browser or app:&lt;/strong&gt; User (browser or app) → WebRTC → AI voice agent (speech model on a server). The four steps are signaling, find a path, lock it, then talk. During signaling, the browser asks your server for a session. The two sides swap a short description of what they can do (called SDP). No audio yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;From a real phone call:&lt;/strong&gt; Caller (any phone) → phone network (the carrier) → SIP trunk (for example Twilio) → AI voice agent. The four steps are dial, invite, accept, then talk.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Names like ICE, SDP and SIP are only labels here. The &lt;a href="https://aqibnawab.vercel.app/blogs/webrtc" rel="noopener noreferrer"&gt;WebRTC post&lt;/a&gt; on this blog goes deeper.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;Interactive version on my site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the browser side, a call goes through four steps before you hear anything:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Signaling.&lt;/strong&gt; The two sides swap a short description of what they can do.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find a path.&lt;/strong&gt; They test network paths and pick one that works through routers and firewalls.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lock it.&lt;/strong&gt; They agree on encryption keys.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Talk.&lt;/strong&gt; Audio flows both ways at the same time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The names for these steps (SDP, ICE, DTLS) do not matter for this post. What matters is this: &lt;strong&gt;steps 1 to 3 happen before the agent can hear a single word.&lt;/strong&gt; Keep that in mind for the next section.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about real phone calls?
&lt;/h3&gt;

&lt;p&gt;Many voice agents are not in a browser at all. A customer dials a normal phone number, and an AI answers. The journey is different:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The caller dials. The &lt;strong&gt;phone network&lt;/strong&gt; routes the call to a &lt;strong&gt;SIP trunk provider&lt;/strong&gt; (for example Twilio). This company turns phone calls into internet calls.&lt;/li&gt;
&lt;li&gt;The trunk sends a &lt;strong&gt;SIP INVITE&lt;/strong&gt; to the voice API. SIP is the "someone is calling" language of phone systems. OpenAI's Realtime API accepts SIP calls at a project address, and it sends &lt;strong&gt;your server&lt;/strong&gt; a webhook called &lt;code&gt;realtime.call.incoming&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Your server &lt;strong&gt;accepts&lt;/strong&gt; the call through the API and sets the voice, the instructions and the tools. It can also open a WebSocket for that call to watch events and control it.&lt;/li&gt;
&lt;li&gt;Audio flows as &lt;strong&gt;RTP&lt;/strong&gt; packets, the same small packets as in the browser case. Phone lines are narrow, usually 8 kHz, so a phone call sounds thinner than a browser call.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The phone path is also in the &lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;interactive version on my site&lt;/a&gt; (the &lt;strong&gt;From a real phone call&lt;/strong&gt; tab).&lt;/p&gt;

&lt;p&gt;Why does this matter for this post? Because different changes live in different places:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Full-duplex, background reasoning and model handoffs&lt;/strong&gt; live inside the model and the session. They help every call, however it arrived. They matter &lt;strong&gt;most on the phone&lt;/strong&gt;, where callers say "uh-huh", talk over the agent and have no screen to look at.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WARP&lt;/strong&gt; is about the WebRTC handshake, so it matters for browser and app clients. A phone call uses SIP for its first leg instead.&lt;/li&gt;
&lt;li&gt;I could not find OpenAI saying exactly how phone calls use the relay and transceiver layer. Treat that part as unknown.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want the full story with NAT, STUN and TURN, I wrote about it in the &lt;a href="https://aqibnawab.vercel.app/blogs/webrtc" rel="noopener noreferrer"&gt;WebRTC post&lt;/a&gt;. The packet side is in the &lt;a href="https://aqibnawab.vercel.app/blogs/voice-jitter-buffer" rel="noopener noreferrer"&gt;voice jitter buffer post&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. WARP: why starting a voice conversation took time
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The problem.&lt;/strong&gt; When you press the mic button, nothing can be heard until the setup is done. Each step of the setup needs messages to go to the server and come back. Every one of those trips is called a &lt;strong&gt;round trip&lt;/strong&gt;. OpenAI counts &lt;strong&gt;six round trips&lt;/strong&gt; to start media and the data channel in a normal WebRTC call. If your network round trip is 100 ms, that is 600 ms before the agent can listen.&lt;/p&gt;

&lt;p&gt;Why so many? WebRTC was designed before &lt;strong&gt;QUIC&lt;/strong&gt;, a newer protocol that was built to keep setup short. WebRTC stacks several protocols on top of each other. Each one does its own checks. Some of that work is repeated. For example, both the path check and the encryption handshake have their own way to protect the server from fake clients.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix.&lt;/strong&gt; OpenAI made two related changes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;WARP&lt;/strong&gt; changes the transport handshake itself. Media and data channel startup goes from six round trips to &lt;strong&gt;one&lt;/strong&gt;. WARP is backward compatible. One write-up says it folds the encryption handshake into the path check and uses a newer version of DTLS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instant Connect&lt;/strong&gt; takes signaling off the critical path. The settings are agreed &lt;strong&gt;before&lt;/strong&gt; the call. The server can &lt;strong&gt;create the session when the first media packet arrives&lt;/strong&gt;, so the client can start with a single UDP packet. Normal signaling stays as a fallback.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;OpenAI is advancing WARP as open specifications through the IETF (the group that standardizes internet protocols), and support has already landed in &lt;strong&gt;libwebrtc&lt;/strong&gt; and &lt;strong&gt;Pion&lt;/strong&gt;, two widely used WebRTC libraries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Starting a voice call: before and after WARP&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Before: vanilla WebRTC.&lt;/strong&gt; Six round trips: ICE checks the network path, then DTLS hello, cookie check, key exchange and finish, then the data channel opens and the agent listens.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;After: WARP.&lt;/strong&gt; One round trip for all of it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;OpenAI counts six round trips for media and data channel startup. How they split up in this picture is illustrative. Each double arrow is one round trip (a message out and a reply back). Signaling is separate, and Instant Connect takes it off the critical path.&lt;/p&gt;

&lt;p&gt;If the network round trip is 100 ms, the wait before the agent listens is 600 ms in the before case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;Interactive version on my site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the &lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;interactive version&lt;/a&gt;, you can play both versions and move the round trip slider. The number of round trips drops from six to one. What each round trip costs depends on your network, so the gain is biggest on slow mobile networks, which is where many real users are.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Why it matters:&lt;/strong&gt; the first second of a voice call decides how it feels. If the agent is not listening yet, the user's first words are lost or the user waits in silence.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  3. Relay + Transceiver: scaling millions of voice sessions
&lt;/h2&gt;

&lt;p&gt;WARP makes &lt;strong&gt;one&lt;/strong&gt; call start faster. This section is about a different problem: running &lt;strong&gt;a very large number&lt;/strong&gt; of calls at once. They are related, but they solve different things.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem.&lt;/strong&gt; Picture a call center building. In the classic setup, every call has its own &lt;strong&gt;front door&lt;/strong&gt; facing the street. Every worker is on the public internet, with its own open UDP port, and the same worker also holds everything about the call. With more calls you need more workers, more open ports and more machines facing the internet.&lt;/p&gt;

&lt;p&gt;That hurts in practice. Large public port ranges are hard to run safely, especially in Kubernetes. Some workers get busy while others sit idle. Rolling out a new version is risky, because a worker with live calls cannot just be replaced.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix.&lt;/strong&gt; Add a &lt;strong&gt;reception desk&lt;/strong&gt; and keep the real work inside the building. OpenAI split the job into two layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Relay.&lt;/strong&gt; The reception desk. It sits on the public side with a small, fixed footprint. It receives packets and forwards them to the right room. It keeps almost no state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transceiver.&lt;/strong&gt; The room where the call actually happens. It owns the whole WebRTC session: path checks, encryption handshake, encrypted media and session lifetime. It lives inside the cluster, not on the public internet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How does the relay know the right room? The design reportedly puts a &lt;strong&gt;routing hint&lt;/strong&gt; in the first packets of a session. The relay reads the hint, forwards, and does not need to understand the whole protocol.&lt;/p&gt;

&lt;p&gt;OpenAI also looked at the usual alternatives and passed on them. A TURN server is a heavier middleman than needed, because most voice agent calls are one user and one model. An SFU is built for conference calls with many people. Here the model is just one endpoint, not a conference guest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where do the calls land? Workers on the internet vs relay + transceivers&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Before:&lt;/strong&gt; every worker faces the internet. Each caller talks straight to a worker. That worker needs its own open UDP port and also holds the session. Public UDP ports grow with the number of calls, and every worker holds WebRTC session state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;After:&lt;/strong&gt; a relay sits on the public side with a small, fixed footprint and forwards packets to a transceiver inside the cluster. The transceiver owns the whole WebRTC session. A routing hint in the first packets of a session is how OpenAI describes the relay finding the right transceiver without keeping session state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Illustrative.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;Interactive version on my site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the &lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;interactive version&lt;/a&gt;, you can click different callers and switch between the two tabs. Before, each caller needs its own open port, so the number grows with the calls. After, the public side stays small and fixed, and all the hard WebRTC state lives in one place, inside the cluster.&lt;/p&gt;

&lt;p&gt;OpenAI describes the idea in one sentence: the best place to add complexity is in a thin routing layer.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Do not mix these up.&lt;/strong&gt; WARP improves &lt;strong&gt;connection setup&lt;/strong&gt; for one call. Relay + Transceiver improves &lt;strong&gt;infrastructure scaling&lt;/strong&gt; for many calls.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  4. Full-duplex: AI that listens while speaking
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The problem.&lt;/strong&gt; Think of the difference between a &lt;strong&gt;walkie-talkie&lt;/strong&gt; and a &lt;strong&gt;phone call&lt;/strong&gt;. On a walkie-talkie you press a button, talk, say "over", and wait. On a phone call you can interrupt, laugh, say "uh-huh" and the other person adjusts.&lt;/p&gt;

&lt;p&gt;The classic voice agent is a walkie-talkie. It is a pipeline:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Two ways to build a voice agent&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Old way: walkie-talkie.&lt;/strong&gt; Speech to text (voice becomes words) → LLM (words in, words out) → Text to speech (words become voice). One step at a time. Each step waits for the one before it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New way: phone call.&lt;/strong&gt; You ⇄ one speech-to-speech model that listens and speaks at the same time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;Interactive version on my site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A small helper model, called a &lt;strong&gt;turn detector&lt;/strong&gt;, listens for silence to guess when you have finished. Only then does the LLM start. And while the AI is speaking, it is slow to notice that you started talking.&lt;/p&gt;

&lt;p&gt;That has a built-in trade-off. If the detector guesses too early, the agent cuts you off. If it guesses too late, you wait in silence. A quiet "mm-hm" looks like a new turn, so the agent stops and restarts. On a real phone call, with noise and people who talk over each other, this is where the robotic feeling comes from.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix.&lt;/strong&gt; OpenAI's GPT-Live (the model behind ChatGPT Voice) is a native &lt;strong&gt;speech-to-speech&lt;/strong&gt; model. Audio streams in and speech streams out &lt;strong&gt;at the same time&lt;/strong&gt;, with no hard line between "your turn" and "my turn". The turn detector is removed from the audio path. This is called &lt;strong&gt;full-duplex&lt;/strong&gt;: both sides can talk and listen at once, like a phone call between people.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A phone call with a pizza bot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The diagram compares a walkie-talkie agent and a phone call (full-duplex) agent in three moments: you ask a question, you interrupt, and you say "mm-hm". Timings are illustrative.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;Interactive version on my site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine you are calling a pizza shop run by an AI. On the &lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;interactive version&lt;/a&gt;, you can press play and try the three moments. Switch between the walkie-talkie and the phone call, and watch the &lt;strong&gt;AI ears&lt;/strong&gt; and when the AI answers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;You ask a question.&lt;/strong&gt; The old way leaves a pause. The new way answers right when you stop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You interrupt.&lt;/strong&gt; The old way talks over you for a moment and needs a new turn. The new way stops and answers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You say "mm-hm".&lt;/strong&gt; The old way stops and restarts. The new way understands it means "go on".&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Background reasoning: thinking without stopping the conversation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The problem.&lt;/strong&gt; A fast voice model is good at talking. It is not good at long reasoning or slow tool calls. If one model has to do everything, the user hears silence while it thinks. A real person would say "let me check" and keep talking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix.&lt;/strong&gt; Split the work across two models:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;fast voice model&lt;/strong&gt; stays in the call. It talks and listens all the time.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;reasoning model&lt;/strong&gt; (a frontier model, GPT-5.5 at the time of writing) works on a &lt;strong&gt;separate asynchronous path&lt;/strong&gt;. The voice model hands it the hard task. The result is merged back into the live conversation without pausing it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;OpenAI counts the whole delegation loop against the response time budget: routing, prompt processing, model run and tool calls. To keep it fast, the app server creates the reasoning session &lt;strong&gt;when the call starts&lt;/strong&gt; and fills it with the conversation so far, so the first delegation does not pay for a cold start. Session affinity (sending the same session to the same machine) and prompt caching keep later calls quick.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A fast voice model with a reasoning model in the background&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The voice model (fast, always in the call) stays on the line. The reasoning model (slow, deep, uses tools) works on a separate path. In the example, the user says "Move my flight to tomorrow and email me the receipt." The voice model never stops talking and listening. The slow work happens on a separate path.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;Interactive version on my site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the &lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;interactive version&lt;/a&gt;, you can step through the example. Notice that the voice model keeps talking and even asks a follow-up question while the work runs.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Python to Go: improving real-time audio performance
&lt;/h2&gt;

&lt;p&gt;This one is short on purpose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem.&lt;/strong&gt; Real-time audio is a stream of tiny frames that must arrive on schedule. The media frontend and the inference logic were running on Python &lt;code&gt;asyncio&lt;/code&gt;. At large scale, small delays and unlucky scheduling add up. They show up as the slow frames, the &lt;strong&gt;tail latency&lt;/strong&gt;, which users hear as cuts and glitches.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix.&lt;/strong&gt; OpenAI moved the media frontend and inference logic to &lt;strong&gt;Go&lt;/strong&gt;. A usual reason for this kind of move is that Python &lt;code&gt;asyncio&lt;/code&gt; runs many streams on one event loop, so one slow task can delay the others, while Go spreads streams across CPU cores. That is my explanation, not a quote. The reported result is that the new system's &lt;strong&gt;p95&lt;/strong&gt; frame delivery time matches the old system's &lt;strong&gt;p50&lt;/strong&gt;. In plain words: the slow frames of the new system are about as fast as the typical frames of the old one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Frame delivery time, before and after the Go rewrite&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The chart compares p50 and p95 frame delivery time for Python asyncio and for Go. Time grows to the right. p50 is the typical frame, p95 is the slow one in twenty. Illustrative scale. OpenAI reports a relative result only: the new p95 matches the old p50.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;Interactive version on my site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OpenAI shared this as a relative result, so the chart is only a picture of the idea, not real milliseconds.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Seamless model handoffs: switching without interrupting the user
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The problem.&lt;/strong&gt; Long calls fill up the model's context window. Sometimes you also want to change the model that serves a call. If you just stop one model and start another, the user hears a gap, and the new model has to read the whole history first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix.&lt;/strong&gt; Treat it like a hot swap:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Warm up&lt;/strong&gt; a new instance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prefill&lt;/strong&gt; it with the current conversation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run both in parallel&lt;/strong&gt; while the new one catches up.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cut over&lt;/strong&gt; when the new one is ready.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When the history gets too long, it is &lt;strong&gt;compacted&lt;/strong&gt; (shortened), and a replacement instance is prepared in the background. The live call never waits for the new instance to rebuild its cache.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Switching models in the middle of a call&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The timeline shows instance A serving the call, then a new instance B warming up and prefilling, then both running in parallel, then the cut over so A is retired and B is serving. The user hears continuous audio, with no gap. Illustrative timeline.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;Interactive version on my site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is one more finding worth keeping. For voice, the first limit is usually &lt;strong&gt;not the GPU&lt;/strong&gt;. Open sessions send audio frames all the time, so the CPU-side stream handlers, queues and network paths fill up first. OpenAI also tested the new system with &lt;strong&gt;shadow traffic&lt;/strong&gt;: a small share of real ChatGPT Voice sessions ran on it in read-only mode, without changing what users heard. That found problems short load tests missed, like memory pressure in long sessions, state recovery after reconnects and races during shutdown.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Grok: why ephemeral tokens matter
&lt;/h2&gt;

&lt;p&gt;The last topic is not about OpenAI's servers. It is about how &lt;strong&gt;your app&lt;/strong&gt; should connect to a voice API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem.&lt;/strong&gt; A browser app needs to talk to a voice model. The easy and unsafe way is to put the API key in the browser. Anyone can open dev tools, copy it and spend your money. The safe but heavy way is a &lt;strong&gt;backend relay&lt;/strong&gt;: the browser sends audio to your server, and your server forwards it to the provider. That keeps the key safe, but every audio packet now passes through your backend. It adds a hop of delay and your backend must scale with every call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix.&lt;/strong&gt; Use an &lt;strong&gt;ephemeral token&lt;/strong&gt;: a short-lived key for one session.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The browser asks &lt;strong&gt;your backend&lt;/strong&gt; for a token.&lt;/li&gt;
&lt;li&gt;Your backend calls the provider with the &lt;strong&gt;permanent key&lt;/strong&gt; and gets a short-lived token.&lt;/li&gt;
&lt;li&gt;The backend gives that token to the browser.&lt;/li&gt;
&lt;li&gt;The browser connects &lt;strong&gt;directly&lt;/strong&gt; to the provider with it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Backend relay vs ephemeral token&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Backend relays audio:&lt;/strong&gt; Every audio packet passes through your backend. It is one more hop of delay, and your backend must scale with every call.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ephemeral token:&lt;/strong&gt; The backend only hands out a token. Audio goes straight from the browser to the voice provider. The permanent API key never leaves your server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Grok Voice Agent is shown as the example provider. OpenAI Realtime supports the same pattern.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibnawab.vercel.app/blogs/openai-voice-ai-engineering" rel="noopener noreferrer"&gt;Interactive version on my site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;xAI's Grok Voice Agent API works this way. Your server calls &lt;code&gt;POST /v1/realtime/client_secrets&lt;/code&gt; with the API key to create the token. Browsers cannot set custom headers on a WebSocket, so the browser passes the token in the WebSocket subprotocol list with an &lt;code&gt;xai-client-secret&lt;/code&gt; prefix when it connects to &lt;code&gt;wss://api.x.ai/v1/realtime&lt;/code&gt;. The token expires after the time you set. xAI's docs say to always use ephemeral tokens for browser and mobile apps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenAI supports the same pattern.&lt;/strong&gt; Its Realtime API has an endpoint with the same name, &lt;code&gt;POST /v1/realtime/client_secrets&lt;/code&gt;. Your server creates a short-lived key (it starts with &lt;code&gt;ek_&lt;/code&gt;), and the browser uses it to open its WebRTC session. So the idea is not tied to one vendor. The benefit is the same: your key stays on your server, your backend is out of the audio path, and audio takes the shortest route.&lt;/p&gt;




&lt;h2&gt;
  
  
  The whole story in one picture
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Connect&lt;/td&gt;
&lt;td&gt;Six round trips before the agent can listen&lt;/td&gt;
&lt;td&gt;WARP (six to one) and Instant Connect (start with one packet)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scale&lt;/td&gt;
&lt;td&gt;Public ports and worker state grow with calls&lt;/td&gt;
&lt;td&gt;Relay + Transceiver&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Natural talk&lt;/td&gt;
&lt;td&gt;Walkie-talkie turns, pauses, bad interruptions&lt;/td&gt;
&lt;td&gt;Full-duplex speech-to-speech (a phone call, not a walkie-talkie)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hard tasks&lt;/td&gt;
&lt;td&gt;Thinking blocks the conversation&lt;/td&gt;
&lt;td&gt;Fast voice model + background reasoning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Performance&lt;/td&gt;
&lt;td&gt;Tail latency in audio frames&lt;/td&gt;
&lt;td&gt;Python asyncio to Go&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Long calls&lt;/td&gt;
&lt;td&gt;Context limits, gaps when switching models&lt;/td&gt;
&lt;td&gt;Parallel instances and compaction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Security&lt;/td&gt;
&lt;td&gt;Keys in the browser or audio via your backend&lt;/td&gt;
&lt;td&gt;Ephemeral tokens, direct connection&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you build voice agents, the lesson is that &lt;strong&gt;latency is not one number&lt;/strong&gt;. It is the sum of connection setup, packet routing, turn-taking, reasoning, scheduling and model changes. A better model helps. A better system around the model helps just as much.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What can you use today?&lt;/strong&gt; GPT-Live is described as rolling out in ChatGPT Voice first, with API access planned. Check OpenAI's current docs for what your API key can use. Parts of this story, like ephemeral tokens and SIP calling, are already available in the Realtime API.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  If you build voice agents: my takeaways
&lt;/h2&gt;

&lt;p&gt;These are my own conclusions, not OpenAI's.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Measure the whole gap.&lt;/strong&gt; Time from "user stops talking" to "first audio is heard". Not only the model time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Look at the slow end, not the average.&lt;/strong&gt; A call is ruined by its slowest moments, so watch p95 as well as the typical case.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the human moments.&lt;/strong&gt; Interrupt your agent. Say "mm-hm" while it talks. Stay silent for two seconds. These show the problems that a demo hides.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do not leave silence while work runs.&lt;/strong&gt; Let the agent say a short line while tools or a slower model work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never put a permanent API key in a browser.&lt;/strong&gt; Use short-lived tokens, or run the call through your server on purpose.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check phone and browser separately.&lt;/strong&gt; They connect in different ways and sound different (narrow phone audio vs wide browser audio).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Words you will see
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Word&lt;/th&gt;
&lt;th&gt;Plain meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Latency&lt;/td&gt;
&lt;td&gt;Delay. Here: the time before the agent answers.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Round trip&lt;/td&gt;
&lt;td&gt;A message goes to the server and the reply comes back.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Packet&lt;/td&gt;
&lt;td&gt;A tiny piece of data. Voice is sent as many packets, about 20 ms of sound each.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UDP&lt;/td&gt;
&lt;td&gt;A fast way to send packets that does not wait for receipts. Good for live audio.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;WebRTC&lt;/td&gt;
&lt;td&gt;The technology browsers use for live voice and video.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SIP&lt;/td&gt;
&lt;td&gt;The "someone is calling" language of internet phone systems.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SIP trunk&lt;/td&gt;
&lt;td&gt;A company that connects normal phone numbers to internet calls.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RTP&lt;/td&gt;
&lt;td&gt;The envelope format for live audio packets.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;STT / TTS&lt;/td&gt;
&lt;td&gt;Speech-to-text and text-to-speech.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LLM&lt;/td&gt;
&lt;td&gt;The language model that decides what to say.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Turn detector&lt;/td&gt;
&lt;td&gt;A helper that guesses when you have finished speaking.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Full-duplex&lt;/td&gt;
&lt;td&gt;Both sides can talk and listen at the same time.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;p50 / p95&lt;/td&gt;
&lt;td&gt;The typical case, and the slow case (1 in 20 is slower than this).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ephemeral token&lt;/td&gt;
&lt;td&gt;A short-lived key for one session.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;This post is based on the following. My tools could not open OpenAI's engineering pages directly, so some details come from technical write-ups and quotes of them, including the six-to-one round trip number. Check the originals for exact wording.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OpenAI, &lt;a href="https://openai.com/index/delivering-low-latency-voice-ai-at-scale/" rel="noopener noreferrer"&gt;How OpenAI delivers low-latency voice AI at scale&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;OpenAI, &lt;a href="https://openai.com/index/continuous-voice-interaction-with-gpt-live/" rel="noopener noreferrer"&gt;How we built a realtime system for responsive voice AI in six months&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;InfoQ, &lt;a href="https://www.infoq.com/news/2026/05/openai-voice-ai-scale/" rel="noopener noreferrer"&gt;OpenAI Outlines WebRTC Architecture for Low-Latency Voice AI at Scale&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Developers Digest, &lt;a href="https://www.developersdigest.tech/blog/openai-gpt-live-realtime-voice-architecture-2026" rel="noopener noreferrer"&gt;How OpenAI Built GPT-Live: Full-Duplex Voice, WARP, and the Death of the Turn Detector&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;OpenAI, &lt;a href="https://developers.openai.com/api/docs/guides/realtime-webrtc" rel="noopener noreferrer"&gt;Realtime API with WebRTC&lt;/a&gt; (ephemeral keys)&lt;/li&gt;
&lt;li&gt;OpenAI, &lt;a href="https://developers.openai.com/api/docs/guides/realtime-sip" rel="noopener noreferrer"&gt;Realtime API with SIP&lt;/a&gt; and the &lt;a href="https://www.twilio.com/en-us/blog/developers/tutorials/product/openai-realtime-api-elastic-sip-trunking" rel="noopener noreferrer"&gt;Twilio guide for the OpenAI SIP connector&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Stivers et al., &lt;a href="https://www.pnas.org/doi/10.1073/pnas.0903616106" rel="noopener noreferrer"&gt;Universals and cultural variation in turn-taking in conversation&lt;/a&gt; (PNAS, 2009): the roughly 200 ms gap between speakers&lt;/li&gt;
&lt;li&gt;xAI docs, &lt;a href="https://docs.x.ai/developers/model-capabilities/audio/ephemeral-tokens" rel="noopener noreferrer"&gt;Ephemeral tokens for the Voice Agent API&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The interactive diagrams use illustrative timings. They show the shape of each problem, not measured numbers.&lt;/p&gt;

</description>
      <category>voiceai</category>
      <category>webrtc</category>
      <category>ai</category>
      <category>openai</category>
    </item>
    <item>
      <title>WER Says 1%, but the Caller Got the Wrong Locker Code: Scoring the Words a Voice Agent Acts On</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Thu, 12 Nov 2026 16:28:42 +0000</pubDate>
      <link>https://dev.to/aqibnawab/wer-says-1-but-the-caller-got-the-wrong-locker-code-scoring-the-words-a-voice-agent-acts-on-4lkb</link>
      <guid>https://dev.to/aqibnawab/wer-says-1-but-the-caller-got-the-wrong-locker-code-scoring-the-words-a-voice-agent-acts-on-4lkb</guid>
      <description>&lt;p&gt;Most speech-to-text comparisons end with one number, the word error rate. It's a useful number, and I still check it first when I look at a new model. But when I'm building a voice agent that books appointments or takes order details over the phone, WER keeps telling me things are fine on calls that actually failed.&lt;/p&gt;

&lt;p&gt;This post is about why that happens and how I'd score the small set of words that decide whether a call succeeds: digits, names, dates and codes.&lt;/p&gt;

&lt;h2&gt;
  
  
  A small example from this week
&lt;/h2&gt;

&lt;p&gt;Voice Arena released its Dictation Bench this week. They played 761 recordings from 87 speakers into 10 dictation apps on a Mac, and then five LLM judges checked whether each draft could be sent as it was. One example in their results stuck with me. A speaker read a locker code as "twelve, thirty, eight". Only Gemini typed 12-30-8. The other apps wrote things like 12:38, 1238 or 12-38.&lt;/p&gt;

&lt;p&gt;You can see what happened. The model heard "thirty" followed by "eight" and merged them into "thirty-eight", which is how most people say that pair of words. The pause and the comma in the speaker's voice were the only signals that these were separate numbers, and most systems ignored them.&lt;/p&gt;

&lt;p&gt;In a dictation app this is a small edit. In a voice agent, the agent now has the wrong code, and the caller doesn't find out until the door doesn't open.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why WER hides this
&lt;/h2&gt;

&lt;p&gt;WER counts substitutions, deletions and insertions, and divides by the number of words in the reference transcript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WER = (S + D + I) / N
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every word has the same weight. Say the full utterance was "my locker code is twelve thirty eight". After normalization the reference becomes &lt;code&gt;my locker code is 12 30 8&lt;/code&gt; and the hypothesis becomes &lt;code&gt;my locker code is 12 38&lt;/code&gt;. That's one substitution (30 became 38) and one deletion (the 8 is gone), so two errors out of seven words.&lt;/p&gt;

&lt;p&gt;That sounds bad, but nobody scores one sentence. Over a full call of about 200 words, those same two errors are 1% WER, and a model at 1% looks excellent on a dashboard. The call still failed, because the only words the backend needed were the wrong ones.&lt;/p&gt;

&lt;p&gt;The opposite also happens. A model can get "um", "okay" and "so yeah" wrong all day and push WER up without hurting a single booking. So WER can be high on a call that worked and low on a call that didn't.&lt;/p&gt;

&lt;p&gt;Normalization makes this harder to see. Most WER scripts normalize both sides before comparing, so "twelve" and "12" count as the same word. That's the right choice for fair comparisons between models, but it also means the scoring step may quietly turn "12:38" and "12 38" into the same tokens. If you only look at the final number, you won't notice that formatting was where the real damage was.&lt;/p&gt;

&lt;h2&gt;
  
  
  Score the slots separately
&lt;/h2&gt;

&lt;p&gt;What I'd do instead is keep WER as a general health number, and add a second score for the values the agent actually acts on. I call these slots, because that's what they are in the conversation: the phone number, the order ID, the date, the customer's name.&lt;/p&gt;

&lt;p&gt;For each test call you need the expected slot values, written down by a person who listened to the audio. Then you run the call through your pipeline and compare what came out.&lt;/p&gt;

&lt;p&gt;For digits, I check two things. First, did we get the right digits in the right order? Second, did we keep the grouping the speaker used? The grouping matters for codes like 12-30-8, and it matters less for something like a phone number, where the backend usually only needs the digit string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;DigitScore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;sameDigits&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;sameGroups&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;digitGroups&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;+/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;scoreDigits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;heard&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DigitScore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;digitGroups&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;got&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;digitGroups&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sameDigits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sameGroups&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;sameDigits&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;group&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;group&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;sameDigits&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sameGroups&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;scoreDigits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"12-30-8"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"12:38"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// { sameDigits: false, sameGroups: false }&lt;/span&gt;
&lt;span class="nf"&gt;scoreDigits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"12-30-8"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"12 30 8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// { sameDigits: true,  sameGroups: true }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that "12:38" doesn't even pass the first check. The digit string is &lt;code&gt;1238&lt;/code&gt;, not &lt;code&gt;12308&lt;/code&gt;, because the zero from "thirty" disappeared when the two numbers were merged. A WER script that splits "12:38" into "12" and "38" would still give some credit for the "12". The slot check gives none, which matches what the caller experiences.&lt;/p&gt;

&lt;p&gt;Names and emails need a different check. I compare them after lowercasing and removing spaces and punctuation, and I log near misses separately. "Aqib" heard as "a keep" is a real failure, but it's useful to know whether the model was close, because that tells you whether a custom vocabulary list would fix it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Report it per slot type
&lt;/h2&gt;

&lt;p&gt;Once you have slot scores, report them as a small table instead of folding them into one number:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Slot type&lt;/th&gt;
&lt;th&gt;Calls&lt;/th&gt;
&lt;th&gt;Exact match&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Phone number&lt;/td&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Order ID&lt;/td&gt;
&lt;td&gt;80&lt;/td&gt;
&lt;td&gt;?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Date&lt;/td&gt;
&lt;td&gt;95&lt;/td&gt;
&lt;td&gt;?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;110&lt;/td&gt;
&lt;td&gt;?&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The question marks are on purpose. These numbers come from your own test set, and they'll be different for every product. What matters is that each row is something you can act on. If dates are weak, you look at how your prompt or parser handles "the third" versus "3rd". If order IDs are weak, you look at letter and digit confusion, like "B" and "D" or "eight" and "A".&lt;/p&gt;

&lt;p&gt;This is also where the test set itself matters. Read speech in a quiet room is easy. The cases that break are callers reading a code off a card while walking, saying a number twice because they corrected themselves, or pausing in the middle of a phone number. I'd rather have 50 calls like that than 500 clean ones.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixes that follow from the score
&lt;/h2&gt;

&lt;p&gt;When a slot type keeps failing, there are a few fixes I'd try, roughly in this order.&lt;/p&gt;

&lt;p&gt;The first is to tell the STT what to expect. Many providers let you pass a list of key terms or boost certain words. If the agent knows it's about to ask for an order ID, it can pass the expected format or known product names for that turn.&lt;/p&gt;

&lt;p&gt;The second is to stop trusting the formatted transcript for structured values. Ask for the raw words or the word-level output with timestamps if the provider gives it, and parse the digits yourself. The gap between "thirty" and "eight" is often visible in word timestamps even when the formatted text merged them.&lt;/p&gt;

&lt;p&gt;The third is to read the value back. "I have your code as one two, three zero, eight. Is that right?" costs a couple of seconds, and it turns a silent failure into one the caller can fix right there. For anything the backend can't undo, like a payment amount or a booking date, I think the read-back is worth it even when the model is usually right.&lt;/p&gt;

&lt;p&gt;The last one is to make the score part of every change. When you swap an STT model, change the endpointing settings or edit the prompt, rerun the slot checks. Turn detection changes are a common cause of slot failures, because cutting the caller off after "twelve thirty" produces a clean transcript with a missing digit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;WER tells you how well a model transcribes speech in general. A voice agent needs to know something narrower: did it get the handful of values it's about to act on? Scoring those slots separately, with checks for digits, grouping and names, gives you a number that moves when calls actually fail. It also points you at the fix, which a single WER figure rarely does.&lt;/p&gt;

</description>
      <category>voiceai</category>
      <category>ai</category>
      <category>testing</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Where Does the Clock Start? Measuring Voice Agent Latency Honestly</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Tue, 10 Nov 2026 16:26:58 +0000</pubDate>
      <link>https://dev.to/aqibnawab/where-does-the-clock-start-measuring-voice-agent-latency-honestly-5g2j</link>
      <guid>https://dev.to/aqibnawab/where-does-the-clock-start-measuring-voice-agent-latency-honestly-5g2j</guid>
      <description>&lt;p&gt;When someone says their voice agent responds in 500 milliseconds, my first question is always the same: 500 milliseconds from when to when? This week I saw three different benchmarks report three different reply times for the same voice platform. None of them was necessarily wrong. They just started and stopped the clock at different points, and on a voice call those points can be far apart.&lt;/p&gt;

&lt;p&gt;This post walks through where the time actually goes in a voice agent turn, why the number on your dashboard is usually smaller than what the caller feels, and how I'd set up measurement so the number means something.&lt;/p&gt;

&lt;h2&gt;
  
  
  The turn, step by step
&lt;/h2&gt;

&lt;p&gt;Take a simple cascaded agent: speech-to-text (STT), then an LLM, then text-to-speech (TTS), connected to a phone call. The caller says "I want to check my order status" and stops talking. Here is roughly what has to happen before they hear the agent start speaking:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The caller's last word leaves their phone and travels through the carrier network to your media server.&lt;/li&gt;
&lt;li&gt;Your system waits for some silence to decide the caller has finished. This is endpointing, or turn detection.&lt;/li&gt;
&lt;li&gt;The STT produces a final transcript for that turn.&lt;/li&gt;
&lt;li&gt;The LLM receives the transcript and produces its first tokens.&lt;/li&gt;
&lt;li&gt;TTS turns those first tokens into the first chunk of audio.&lt;/li&gt;
&lt;li&gt;That audio goes back through your media server, the carrier and the jitter buffer on the far end, and finally plays out of the caller's speaker.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every step has its own delay, and most dashboards only show some of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where people start the clock
&lt;/h2&gt;

&lt;p&gt;There are three common starting points, and they give very different numbers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From the endpoint event.&lt;/strong&gt; Many systems log a timestamp when turn detection fires and measure from there. This is easy, because it's an event in your own code. The problem is that it leaves out the silence you waited for before deciding the turn was over. If your endpointing waits for several hundred milliseconds of silence, the caller has already been waiting for that long before your clock even starts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From the final transcript.&lt;/strong&gt; This is even later. It's useful for measuring your LLM and TTS stages, but it says nothing about turn detection or STT.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From the caller's last word.&lt;/strong&gt; This is what the caller actually experiences. It's also the hardest to get, because no event in your system fires at the exact moment the caller stopped talking. You only learn it afterwards, by looking at the audio.&lt;/p&gt;

&lt;p&gt;The same goes for the end of the clock. "First TTS byte generated" is not the same as "first audio the caller hears". Between them sit your outbound media pipeline, the network, and the jitter buffer on the receiving side. I wrote about jitter buffers in an &lt;a href="https://dev.to/aqibnawab/voice-jitter-buffer-the-hardest-part-of-building-ai-voice-agents-l0n"&gt;earlier post&lt;/a&gt;, and the short version is that they exist to absorb uneven packet arrival, which means they add delay on purpose.&lt;/p&gt;

&lt;p&gt;So a reply time measured from endpoint to first TTS byte can look great while the caller still feels a long pause.&lt;/p&gt;

&lt;h2&gt;
  
  
  Log every stage with one clock
&lt;/h2&gt;

&lt;p&gt;The first fix is boring but it works. Give every turn an ID and record a timestamp at each stage, all from the same clock on the same machine. Then you can see which stage is slow instead of staring at one total.&lt;/p&gt;

&lt;p&gt;Here is a small TypeScript sketch of what I mean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Stage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"speechEnd"&lt;/span&gt;      &lt;span class="c1"&gt;// from VAD on inbound audio, best guess at the caller's last word&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"endpoint"&lt;/span&gt;       &lt;span class="c1"&gt;// turn detection decided the caller is done&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"sttFinal"&lt;/span&gt;       &lt;span class="c1"&gt;// final transcript ready&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"llmFirstToken"&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"ttsFirstAudio"&lt;/span&gt;  &lt;span class="c1"&gt;// first audio chunk back from TTS&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"firstPacketOut"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// first outbound media packet sent toward the caller&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TurnTimer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Stage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;turnId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Stage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;between&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Stage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Stage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;turnId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;turnId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;endpointWait&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;between&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"speechEnd"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"endpoint"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;stt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;between&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"endpoint"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sttFinal"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;between&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"sttFinal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"llmFirstToken"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;between&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"llmFirstToken"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ttsFirstAudio"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;sendOut&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;between&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ttsFirstAudio"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"firstPacketOut"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;serverSide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;between&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"speechEnd"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"firstPacketOut"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few details matter here. The &lt;code&gt;speechEnd&lt;/code&gt; mark should come from a voice activity detector running on the inbound audio, not from the endpoint decision, so the endpointing wait shows up as its own number. The &lt;code&gt;mark&lt;/code&gt; method ignores repeats, because streaming systems love to fire the same event more than once. And &lt;code&gt;performance.now()&lt;/code&gt; is a monotonic clock, so it won't jump if the server's wall clock gets adjusted in the middle of a call.&lt;/p&gt;

&lt;p&gt;Even with all of this, &lt;code&gt;serverSide&lt;/code&gt; is still only what happens inside your system. It doesn't include the carrier legs or the caller's jitter buffer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measuring what the caller hears
&lt;/h2&gt;

&lt;p&gt;To get the real number, you have to measure from outside your system. The most reliable way I know is to record test calls with the caller and the agent on separate channels, so you have a stereo file with the caller's audio on one side and the agent's on the other. Then you run a voice activity detector over both channels and measure the gap from the end of caller speech to the start of agent speech.&lt;/p&gt;

&lt;p&gt;If you can record on the caller's side, for example with a test phone or a SIP client that places the call from outside your network, that recording includes the network and jitter buffer delay on the way back. A recording made on your own media server does not, so it's worth knowing which one you have.&lt;/p&gt;

&lt;p&gt;You won't do this on every production call. But running a fixed set of test calls through the real phone path each time you change a model, a region or your endpointing settings tells you how far your server-side number is from what the caller feels. Once you know that gap for your setup, your internal dashboard becomes much more useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use percentiles, not averages
&lt;/h2&gt;

&lt;p&gt;Voice latency has a long tail. Most turns might be fine while a few take two or three times as long, usually because an LLM call was slow, a tool call ran long, or STT took a while to finalize after a noisy stretch. An average hides those turns, but callers remember them, because a long silence in the middle of a call is when people start saying "hello?" and talk over the agent.&lt;/p&gt;

&lt;p&gt;I'd track the median and the 95th percentile for each stage, and look at the slowest individual turns by hand. Very often the slow ones share a cause you can actually fix, like one tool that's slow, or turns where the caller trailed off and turn detection waited too long.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparing numbers from other people
&lt;/h2&gt;

&lt;p&gt;When you read a latency claim from a vendor, a benchmark or a demo, these are the questions I'd ask before trusting it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does the clock start at the caller's last word, at the endpoint event, or at the final transcript?&lt;/li&gt;
&lt;li&gt;Does it stop at the first audio generated or the first audio heard?&lt;/li&gt;
&lt;li&gt;Is it a phone call, a WebRTC call in a browser, or a local test with no network at all?&lt;/li&gt;
&lt;li&gt;Is it the median, the mean, or a single best run?&lt;/li&gt;
&lt;li&gt;Was there a tool call in the turn, or just a short reply?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two numbers that answer these questions differently can't be compared, even if they're both printed in milliseconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  The takeaway
&lt;/h2&gt;

&lt;p&gt;Response time is the thing callers notice most on a voice agent, so it's worth measuring carefully. Log each stage with one clock so you know where the time goes, keep the endpointing wait visible as its own number, check your server-side numbers against real recorded calls from time to time, and look at the slow turns instead of the average. Then when someone asks how fast your agent is, you can tell them exactly what your number covers.&lt;/p&gt;

</description>
      <category>voiceai</category>
      <category>ai</category>
      <category>webrtc</category>
      <category>typescript</category>
    </item>
    <item>
      <title>How I Use Cursor and AI Coding Agents to Ship Features That Actually Last in Production</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Sat, 07 Nov 2026 16:32:14 +0000</pubDate>
      <link>https://dev.to/aqibnawab/how-i-use-cursor-and-ai-coding-agents-to-ship-features-that-actually-last-in-production-3fk5</link>
      <guid>https://dev.to/aqibnawab/how-i-use-cursor-and-ai-coding-agents-to-ship-features-that-actually-last-in-production-3fk5</guid>
      <description>&lt;p&gt;I use Cursor almost every day.&lt;/p&gt;

&lt;p&gt;The surprising part is not how fast the code appears. The surprising part is how often that code runs, and still does not belong in the system.&lt;/p&gt;

&lt;p&gt;I have fallen into the same loop enough times to recognize it. I describe a feature. The agent writes it. I run it. Something is missing, so I explain the missing piece. It fixes that piece and changes something else. I fix the new thing. Another gap shows up. A few rounds later the feature works on my machine. It still does not fit the service that owns the data, the cache in front of it, or the way production actually deploys.&lt;/p&gt;

&lt;p&gt;That is the loop I have been trying to leave.&lt;/p&gt;

&lt;p&gt;The hard part of an AI coding agent is not generating code. The hard part is giving it enough &lt;strong&gt;context, constraints, and direction&lt;/strong&gt; to make a decision that still makes sense after the code reaches production.&lt;/p&gt;

&lt;p&gt;A lot of AI-assisted development still starts like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Build this feature.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The agent writes some code. You run it. Something is missing. You explain it again. It changes something else. You fix another issue. Then another one appears.&lt;/p&gt;

&lt;p&gt;You end with technically working code. The feature does not really fit the system.&lt;/p&gt;

&lt;p&gt;I have found a different workflow works much better for me. I do not start with the code. I start with &lt;strong&gt;context and planning&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The order is roughly:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Context → Questions → Plan → Review → Implementation → Verification&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is not a perfect production process, and I am not saying everyone should follow it exactly. It is simply the process I have been using to reduce unnecessary rework, and to ship features with fewer surprises.&lt;/p&gt;




&lt;h2&gt;
  
  
  The prompt is the small part
&lt;/h2&gt;

&lt;p&gt;When people talk about AI coding, they usually focus on the prompt. Which words to use. How to make the model “think harder.”&lt;/p&gt;

&lt;p&gt;I think the bigger factor is &lt;strong&gt;context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;An agent does not just need to know what to build. It needs to know what system it is building inside.&lt;/p&gt;

&lt;p&gt;Suppose I ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Add an API endpoint to update workspace settings.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That sounds simple. The implementation may only be about 100 lines. The decisions around those 100 lines are the real work.&lt;/p&gt;

&lt;p&gt;Who is allowed to update the workspace? Is this a multi-tenant system? How is authentication handled? Are permissions checked at the gateway or inside the service? Does the workspace service own this data? Are there caches, and can stale cache data cause an authorization problem? Is there optimistic concurrency? What happens if two admins update it at the same time? Does this change trigger an event? Does production use a different database or a different deployment flow? Is the endpoint backwards compatible? Do we need an audit record? What happens when the downstream service is unavailable?&lt;/p&gt;

&lt;p&gt;If the agent does not know those answers, it will still write the endpoint. It will just pick an answer quietly. That quiet pick is where the rework starts. Two days later I am not debugging syntax. I am debugging a decision nobody made on purpose.&lt;/p&gt;

&lt;p&gt;So I try to give the model the whole picture before I ask it to implement anything.&lt;/p&gt;




&lt;h2&gt;
  
  
  A living picture of the project
&lt;/h2&gt;

&lt;p&gt;Before a larger feature, I want the AI to understand the project itself. Not only the source code.&lt;/p&gt;

&lt;p&gt;Code tells you what the system does today. It does not always tell you why. It does not tell you what the team decided not to do. It does not tell you which temporary workaround became a rule after an incident.&lt;/p&gt;

&lt;p&gt;I want it to understand things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What the product does&lt;/li&gt;
&lt;li&gt;Architecture and services&lt;/li&gt;
&lt;li&gt;Data ownership&lt;/li&gt;
&lt;li&gt;Authentication and authorization&lt;/li&gt;
&lt;li&gt;Deployment, environments, and production constraints&lt;/li&gt;
&lt;li&gt;Team structure and current priorities&lt;/li&gt;
&lt;li&gt;Known technical debt&lt;/li&gt;
&lt;li&gt;Important design decisions&lt;/li&gt;
&lt;li&gt;Things we intentionally do not do&lt;/li&gt;
&lt;li&gt;Current work in progress&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I usually ask a model to help me write this, from the repo, the notes I already have, and what we are working on now. One rule is strict. Do not invent. If something is unclear, mark it unknown.&lt;/p&gt;

&lt;p&gt;This is the prompt I use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I want you to understand this project as an engineer before we implement anything.

I will provide you with the project structure, important code, architecture notes, and current development context.

Your job is NOT to suggest changes yet.

First, build a structured understanding of the system.

Document:

1. What the product does
2. Main user flows
3. System architecture
4. Services and responsibilities
5. Important data models and ownership
6. Authentication and authorization model
7. API boundaries
8. External dependencies
9. Database and caching strategy
10. Background jobs and event-driven flows
11. Deployment and infrastructure model
12. Development, staging, and production differences
13. Reliability and failure-sensitive areas
14. Performance-sensitive areas
15. Security-sensitive areas
16. Current technical debt
17. Important engineering conventions
18. Existing patterns that should be reused
19. Things that should NOT be changed casually
20. Current project priorities

Do not invent information.

Whenever something is unclear, explicitly mark it as unknown.

At the end, produce:

- System overview
- Architecture summary
- Important invariants
- Engineering constraints
- Open questions
- Context that future coding agents should know

Think like a senior engineer taking ownership of an existing production system.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That document becomes the &lt;strong&gt;base context&lt;/strong&gt;. I keep it, and I keep updating it.&lt;/p&gt;

&lt;p&gt;Six months ago a component can be an experiment. Today the same component can sit on the path of a live call. If the document still describes the experiment, the agent will treat a production rule like a suggestion.&lt;/p&gt;

&lt;p&gt;I do not treat this file as a wiki that nobody opens. It is closer to a &lt;strong&gt;living engineering memory&lt;/strong&gt;. When the project changes, the memory has to change with it. Otherwise the next conversation starts from an old truth, and the agent will sound confident about something that is no longer true.&lt;/p&gt;




&lt;h2&gt;
  
  
  One feature, one conversation
&lt;/h2&gt;

&lt;p&gt;This sounds minor. It has made a bigger difference for me than a clever prompt.&lt;/p&gt;

&lt;p&gt;I used to keep one long chat. Authentication, Redis, billing, a database migration, a bug from two weeks ago, some experimental architecture, and then a new notification feature. All of it in the same thread. It felt useful. The model already “knew” the project.&lt;/p&gt;

&lt;p&gt;Sometimes that knowledge works against you. Old assumptions start affecting new decisions. A caching comment from the billing thread shows up in a feature that should not share it. A worry from last week’s migration makes the model careful about a table this feature does not touch.&lt;/p&gt;

&lt;p&gt;I call that &lt;strong&gt;conversation bias&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For larger work I prefer one feature in one focused conversation. The chat starts with the relevant project context and the feature. Nothing else sitting underneath it.&lt;/p&gt;

&lt;p&gt;On QuickStart AI that has looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Project: QuickStart AI

Conversation 1: Agent publishing
Conversation 2: Knowledge ingestion
Conversation 3: Workspace membership caching
Conversation 4: Conversation idempotency
Conversation 5: Voice call state management
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each one starts clean. The model is less likely to carry an old decision into a problem that does not share it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rules that sound like a teammate
&lt;/h2&gt;

&lt;p&gt;Project context tells the agent what the system is. It does not, by itself, tell the agent how I expect changes to be made in this repository.&lt;/p&gt;

&lt;p&gt;That is where Cursor Rules help. I do not want the rules to say “write clean code.” The agent already believes it writes clean code. The sentence is too vague to change anything.&lt;/p&gt;

&lt;p&gt;I want the rules to explain how this particular project should be approached. Follow the service boundaries that already exist. Do not move business logic between services unless I explicitly ask. Respect who owns the data. Do not invent a new abstraction when an existing pattern already solves the problem. Validate at the API boundary. Use the error model we already have. Never trust a workspace id that came from the client. Derive authorization from the authenticated identity. Do not log tokens or secrets. Assume a network call can fail. Keep the change inside the feature.&lt;/p&gt;

&lt;p&gt;The version I keep is closer to a note for a new engineer than a style guide.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Project Engineering Rules

## Architecture

- Follow existing service boundaries.
- Do not move business logic between services unless explicitly requested.
- Respect data ownership.
- Do not introduce a new abstraction when an existing pattern already solves the problem.

## API

- Validate requests at the API boundary.
- Use the existing error model.
- Preserve backward compatibility unless the feature explicitly allows a breaking change.

## Database

- Prefer existing repository patterns.
- Migrations must be backwards compatible where required.
- Never silently change production data semantics.

## Security

- Never trust client-supplied workspace identity.
- Always derive authorization context from authenticated identity.
- Do not log tokens, secrets, or sensitive user data.

## Reliability

- Assume network calls can fail.
- Consider retries, timeouts, idempotency and partial failure where relevant.

## Code Changes

- Reuse existing conventions.
- Avoid unrelated refactoring.
- Keep the change scoped to the feature.
- Before implementing, inspect existing patterns.

## Decision Making

When multiple approaches are possible:
1. Prefer consistency with the existing architecture.
2. Prefer the simplest design that satisfies production requirements.
3. Explain trade-offs before introducing a new pattern.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The goal is simple. &lt;strong&gt;Teach the agent how I expect an engineer on this project to think.&lt;/strong&gt; That is much more useful than explaining the same constraints again in every prompt.&lt;/p&gt;




&lt;h2&gt;
  
  
  I ask it to find the problems first
&lt;/h2&gt;

&lt;p&gt;This is the part I consider the most important, and the part I still see people skip.&lt;/p&gt;

&lt;p&gt;They ask the agent to code too early. Coding is often the easy part. Choosing the correct implementation is the harder part.&lt;/p&gt;

&lt;p&gt;For a feature I want the model to think through architecture, data flow, API changes, database changes, permissions, concurrency, failure, retries, observability, deployment, backwards compatibility, migration, testing, and rollback. I do not want the first plan accepted immediately. I read it. I challenge it. I send it back.&lt;/p&gt;

&lt;p&gt;Before any of that, I ask it to question the feature. The prompt is long on purpose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I want to implement the following feature:

[FEATURE DESCRIPTION]

Do NOT write code yet.

First, analyze the feature as a production system.

Ask me questions before producing the implementation plan.

Review the feature from these angles:

1. Product behavior
2. Existing architecture
3. Service boundaries
4. Data ownership
5. Database changes
6. API changes
7. Authentication
8. Authorization
9. Multi-tenancy
10. Concurrency
11. Idempotency
12. Caching
13. Background jobs
14. Queues/events
15. External service failures
16. Retries and timeouts
17. Rate limiting
18. Performance
19. Observability
20. Logging
21. Metrics
22. Tracing
23. Security
24. Privacy / sensitive data
25. Backwards compatibility
26. Deployment
27. Rollout strategy
28. Migration safety
29. Rollback strategy
30. Testing
31. Operational risks
32. Future scaling concerns

Ask questions wherever the current context is insufficient.

Do not assume missing requirements.

Separate:
- known facts
- assumptions
- decisions we still need to make

Your goal is to help me discover problems before implementation starts.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This changes the conversation.&lt;/p&gt;

&lt;p&gt;Instead of “here is my feature, build it,” it becomes “here is my feature, try to break the idea before we build it.”&lt;/p&gt;

&lt;p&gt;That second conversation is much more valuable. The model comes back with gaps I would otherwise find after the code is already half written. Who can publish. Whether a published agent version is immutable. What a retry should do if the client never saw the response. Those questions are cheaper before the files change.&lt;/p&gt;




&lt;h2&gt;
  
  
  Then I ask it to disagree with me
&lt;/h2&gt;

&lt;p&gt;I do not want the model to agree just because I proposed the design. A plan that only nods along is not very useful. I have watched a weak design get written because it sounded sure of itself.&lt;/p&gt;

&lt;p&gt;So after the first pass I send something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Now challenge this design.

What could go wrong in production?

Which assumptions are dangerous?

Where can race conditions occur?

Where can duplicate operations occur?

What happens if a downstream service is unavailable?

What happens if the request succeeds but the response is lost?

What happens if two users perform this action at the same time?

What happens if a deployment happens halfway through the migration?

What happens if old and new versions of the application run simultaneously?

Where could authorization be bypassed?

What could become a bottleneck at 10x the current traffic?

Which parts of this design will be painful to change later?

Give me better alternatives where they exist.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This often produces a better architecture discussion than asking for code. Asking for code at this stage mostly gives me speed. Asking it to challenge the design gives me judgment, or at least a list of places where my judgment is thin.&lt;/p&gt;

&lt;p&gt;I still decide. The model does not own the system. But I would rather argue with a plan for twenty minutes than discover the race condition after two people save the same agent.&lt;/p&gt;




&lt;h2&gt;
  
  
  I show the same feature to more than one model
&lt;/h2&gt;

&lt;p&gt;Another thing I have started doing is asking different models to look at the same feature before anyone edits the repository.&lt;/p&gt;

&lt;p&gt;The reason is not that one model is wrong. Different models notice different things.&lt;/p&gt;

&lt;p&gt;One might suggest a synchronous request that writes straight to the database. Another might notice the write should emit an event, and a worker should finish the rest. Another might point out the request can be retried, so the operation needs idempotency. Another might say optimistic concurrency is enough, and a distributed lock would be unnecessary complexity.&lt;/p&gt;

&lt;p&gt;That comparison is useful. I am using several models as &lt;strong&gt;independent engineering reviewers&lt;/strong&gt;. I read them side by side. I keep the point that survives the argument.&lt;/p&gt;

&lt;p&gt;The tools are also not doing the same job for me. I prefer to separate them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ChatGPT&lt;/strong&gt; is where I sit with the problem. I use it to understand the feature, ask questions, explore the architecture, challenge assumptions, compare approaches, and produce the first plan. I can keep the broader project context there and bring it into the feature discussion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Codex&lt;/strong&gt; is where I go deeper once the plan is already reviewed. I ask it to turn that plan into an implementation plan, inspect the repository, find the affected files, and match the existing patterns. I do not start it from zero. I give it the engineering plan I already argued with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cursor&lt;/strong&gt; is where I spend a lot of the implementation time. By then the agent has the repository, the Cursor Rules, the feature plan, and the patterns already in the code. It is no longer deciding the design from a one-line ticket. It is implementing a decision that already exists.&lt;/p&gt;

&lt;p&gt;There is one more tool I keep in this loop, because coding agents sometimes answer from memory of a library. That memory is often a version behind, or mixed with an older API that looks similar.&lt;/p&gt;

&lt;p&gt;When the feature depends on a framework, I want the docs for the version we actually installed. Context7 is how I get that. The instruction I add is short:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Before implementing this:

1. Inspect the existing code.
2. Check the project's dependency versions.
3. Use Context7 to verify the current API and recommended usage for the exact library version.
4. Do not rely on memory when the library behavior matters.
5. Follow the existing project patterns where possible.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I would rather pause and confirm how this version works than ship a call that was correct last year.&lt;/p&gt;




&lt;h2&gt;
  
  
  The context has to move when the project moves
&lt;/h2&gt;

&lt;p&gt;Creating the project context once, and never touching it again, is a mistake I try not to repeat.&lt;/p&gt;

&lt;p&gt;Architecture changes. Priorities change. A new service shows up. A temporary workaround becomes a permanent constraint. A table gets a new ownership rule. A production incident teaches something the document does not know yet.&lt;/p&gt;

&lt;p&gt;So I update the context when those things happen. A recent update looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Update project context:

Current focus:
- Knowledge ingestion
- Agent publishing

New architecture decision:
- Production agent versions are immutable.
- Runtime reads the production pointer.
- Draft edits must never modify an already published version.

New reliability constraint:
- All externally triggered conversation actions must support idempotency.

Do not treat previous assumptions about these areas as current truth.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks simple. It matters because the next agent will believe whatever you leave in that file. &lt;strong&gt;The quality of the agent depends heavily on the quality of the context you give it.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The plan stays as a document
&lt;/h2&gt;

&lt;p&gt;Once I am happy with the plan, I do not immediately say “go build it” and close the tab.&lt;/p&gt;

&lt;p&gt;I keep the plan. A good one, for something like agent publishing, explains the current behavior, the behavior we want, the architecture changes, the database and API changes, the authorization rules, the state transitions, the failure behavior, concurrency, idempotency, observability, testing, deployment, rollback, the files we expect to change, and the things that are explicitly out of scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Feature: Agent Publishing

1. Current behavior
2. Desired behavior
3. Architecture changes
4. Database changes
5. API changes
6. Authorization rules
7. State transitions
8. Failure behavior
9. Concurrency behavior
10. Idempotency behavior
11. Observability
12. Testing
13. Deployment
14. Rollback
15. Files expected to change
16. Things explicitly out of scope
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, implementation is much more mechanical. The agent has fewer large decisions left to invent while it is typing.&lt;/p&gt;

&lt;p&gt;I also want the implementation to stay inside that plan. If the agent suddenly wants to refactor five unrelated modules while building one feature, I stop it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stay within the approved feature scope.

Do not perform unrelated refactoring.

If you discover a broader architectural issue:
1. Stop.
2. Explain the issue.
3. Explain why the current feature cannot safely proceed without addressing it.
4. Propose the smallest change required.

Do not silently expand the scope.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have seen this many times. A small feature turns into a repository-wide refactor. The diff looks busy. The risk is larger than the feature. If there is a real blocker, I want it named. I do not want it quietly absorbed into the same pull request.&lt;/p&gt;




&lt;h2&gt;
  
  
  Failure paths, before the happy path
&lt;/h2&gt;

&lt;p&gt;A feature that works when everything succeeds is not production-ready. I have shipped enough “it worked when I clicked once” paths to be suspicious of them.&lt;/p&gt;

&lt;p&gt;When the plan is taking shape, I explicitly ask for the failure paths.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Now describe the failure paths.

For each external dependency:
- timeout
- network failure
- invalid response
- partial success
- duplicate request
- retry
- unavailable dependency

For each database operation:
- transaction failure
- concurrent update
- constraint violation
- migration mismatch

For each async operation:
- duplicate message
- delayed message
- out-of-order message
- worker crash
- poison message
- retry exhaustion

For each user action:
- repeated request
- stale client
- unauthorized request
- concurrent request

Explain expected system behavior for each case.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I want the expected behavior, not a slogan. What does the system do if the write succeeded and the response was lost? What does it do if the worker dies after the side effect and before the acknowledgement?&lt;/p&gt;

&lt;p&gt;This is where an AI is actually useful as a second engineering brain. It will list failures that are easy to miss when you are focused on the feature you want to exist.&lt;/p&gt;

&lt;p&gt;I also ask what changes when the feature gets larger. A path can feel fine at 100 requests a minute and behave very differently at 10,000.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assume this feature becomes 10x larger.

What breaks first?

Analyze:
- database load
- indexes
- connection pools
- cache behavior
- queue depth
- worker concurrency
- API latency
- memory usage
- external API limits
- hot keys
- locks
- event throughput

Which parts of the current design are safe to scale?

Which parts would need redesign?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am not trying to over-engineer every feature for some imaginary future. I just want to know where the boundary is. Which parts can grow, and which parts we would have to redraw. Knowing that is enough.&lt;/p&gt;




&lt;h2&gt;
  
  
  Only then I let it write the code
&lt;/h2&gt;

&lt;p&gt;After the important questions are answered, I let the coding agent do the part it is actually good at.&lt;/p&gt;

&lt;p&gt;The message is boring on purpose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The architecture and implementation plan have been approved.

Implement the feature according to the plan.

Before changing files:

1. Inspect the existing implementation patterns.
2. Identify the exact files that need changes.
3. Reuse existing abstractions where possible.
4. Do not introduce new abstractions without a reason.

During implementation:

- follow project rules
- preserve existing contracts
- handle expected failure paths
- add required validation
- add required logging/metrics/tracing
- keep the change scoped

After implementation:

1. Review the diff.
2. Check for unintended behavior changes.
3. Run the relevant tests.
4. Add missing tests.
5. Check error paths.
6. Check backwards compatibility.
7. Summarize what changed and why.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point Cursor is mostly executing a decision that has already been thought through. I still read the diff. I still run the tests. I still check the failure paths, the logs, and whether an old client still works. The agent does not get the last word. It gets a narrower job, which is the job it does well.&lt;/p&gt;




&lt;h2&gt;
  
  
  What my week looks like now
&lt;/h2&gt;

&lt;p&gt;Without this, AI coding often looks like a repair loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prompt
↓
Code
↓
Run
↓
Bug
↓
New prompt
↓
Patch
↓
Another bug
↓
Another prompt
↓
Another patch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The workflow I prefer is slower at the start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Project context
↓
One feature
↓
Questions
↓
Architecture
↓
Failure analysis
↓
Plan
↓
Review
↓
Implementation
↓
Verification
↓
Update the context
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second process spends more time thinking before coding. It often saves much more time afterwards, because I am not spending the afternoon explaining what I meant after the files have already changed.&lt;/p&gt;

&lt;p&gt;The loop I personally use today is close to this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I maintain a living description of the project, the architecture, the constraints, the deployment model, and the current priorities.&lt;/li&gt;
&lt;li&gt;I keep one feature in one conversation.&lt;/li&gt;
&lt;li&gt;I ask the model to find missing requirements and production risks before it writes code.&lt;/li&gt;
&lt;li&gt;I ask for a real implementation plan, including failure, deploy, and rollback.&lt;/li&gt;
&lt;li&gt;I challenge that plan, sometimes with another model.&lt;/li&gt;
&lt;li&gt;I feed the approved plan into Codex or Cursor, and I ask them to inspect the repository before they edit.&lt;/li&gt;
&lt;li&gt;I review the diff, the tests, the failure paths, observability, security, and backwards compatibility.&lt;/li&gt;
&lt;li&gt;Any important new decision goes back into the project context.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  The line I keep coming back to
&lt;/h2&gt;

&lt;p&gt;The biggest lesson for me is that AI coding is not mainly about asking for better coding prompts.&lt;/p&gt;

&lt;p&gt;It is about giving the model the information required to make a good engineering decision.&lt;/p&gt;

&lt;p&gt;The model needs to know what we are building, and why. What already exists. What must not change. What the constraints are. What happens in production. What happens when things fail. What happens when the system becomes larger.&lt;/p&gt;

&lt;p&gt;Only after that does “write the code” become a useful instruction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AI is very good at writing code.&lt;/strong&gt; Production engineering is the decisions around that code. What should be built. Where it should live. What it should depend on. What can fail. What happens when it fails. How it behaves under concurrency. How it behaves during a deployment, when old and new versions are both alive. How it behaves when the traffic grows. How it fits the rest of the system.&lt;/p&gt;

&lt;p&gt;So I try not to use Cursor as “give me code.”&lt;/p&gt;

&lt;p&gt;I try to use it as: here is the system, here is the feature, here are the constraints. Help me think through the engineering problem. Then help me implement the decision.&lt;/p&gt;

&lt;p&gt;I still make mistakes. The process is not perfect. I just spend much less time telling the agent what I meant after the code has already been written.&lt;/p&gt;

&lt;p&gt;For me, that is the real value of AI-assisted development. Not writing more code. Reaching the right implementation, with less unnecessary rework.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>cursor</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>We Stopped Installing LLM SDKs in Every Microservice</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Thu, 05 Nov 2026 16:34:08 +0000</pubDate>
      <link>https://dev.to/aqibnawab/we-stopped-installing-llm-sdks-in-every-microservice-nm3</link>
      <guid>https://dev.to/aqibnawab/we-stopped-installing-llm-sdks-in-every-microservice-nm3</guid>
      <description>&lt;p&gt;We almost put the OpenAI SDK inside the agent runtime.&lt;/p&gt;

&lt;p&gt;It looked like the short path. The runtime already runs a turn: load the published agent, load the chat, maybe search knowledge, maybe load memory, call a model, save the assistant message. Installing &lt;code&gt;@anthropic-ai/sdk&lt;/code&gt; or the OpenAI client right there would have made the first demo work in an afternoon.&lt;/p&gt;

&lt;p&gt;We did not do that. This post is about why, what we built instead, and how that choice helps a multi-tenant agent platform when the traffic is no longer a laptop.&lt;/p&gt;

&lt;p&gt;The audience I have in mind is a senior backend or platform engineer who has already shipped one LLM feature and is now asking where the keys, the retries, and the bill should live.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we were building
&lt;/h2&gt;

&lt;p&gt;QuickStart AI is a production-oriented platform. Organizations run &lt;strong&gt;workspaces&lt;/strong&gt;. A workspace is the tenant. Every customer row is scoped by &lt;code&gt;workspace_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Inside a workspace we already had the product pieces a chat agent needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Agents.&lt;/strong&gt; Instructions and model settings. Publish is immutable. Rollback points at an older published version.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conversations.&lt;/strong&gt; Ordered messages in Postgres. Writes use an idempotency key so a client retry does not append the same turn twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Knowledge.&lt;/strong&gt; Ingest plus retrieval. RAG goes through OpenSearch.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory.&lt;/strong&gt; Facts about an end user, stored in Postgres.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Runtime.&lt;/strong&gt; The service that executes one turn.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The public door is an &lt;strong&gt;API gateway&lt;/strong&gt; on port &lt;strong&gt;3000&lt;/strong&gt;. It checks the JWT, checks workspace membership, and applies a Redis rate limit. It then proxies to internal services. A browser and a dashboard never call an LLM vendor or a scrape vendor themselves.&lt;/p&gt;

&lt;p&gt;The stack around this is a Node.js and TypeScript monorepo, Postgres, Redis (Valkey in some environments), OpenSearch, and SQS-shaped queues (ElasticMQ locally). Internal services listen on their own ports.&lt;/p&gt;

&lt;p&gt;The numbers we wrote down, so the resilience work had a target, are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;10,000+ organizations, 50,000+ users, 100,000+ agents&lt;/li&gt;
&lt;li&gt;About 10,000 HTTP requests per second sustained, and 20,000 at peak, on the gateway and API. That is &lt;strong&gt;not&lt;/strong&gt; 10,000 LLM calls per second.&lt;/li&gt;
&lt;li&gt;Millions of messages per day, and 10,000+ concurrent chats&lt;/li&gt;
&lt;li&gt;Later, a separate voice service for about 5,000 concurrent inbound calls and 5,000 outbound calls. Voice is out of scope for this post.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Identity, workspace RBAC, the gateway, agents, conversations, a Redis membership cache, knowledge, memory, and the runtime turn path were already shipped. The open question was outbound: how every service talks to paid vendors without copying keys and failure policy into each deployable.&lt;/p&gt;

&lt;p&gt;We wrote the decision down in ADR 0010 for the model gateway and ADR 0011 for web fetch.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we needed the design to do
&lt;/h2&gt;

&lt;p&gt;A turn is not only "call the model." Some paths also fetch a page or search the web, so knowledge ingest and future tools can acquire content. Those are different vendors, different bills, and different failure shapes. The requirement was the same for both:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Vendor API keys live in as few deployables as possible.&lt;/li&gt;
&lt;li&gt;Timeout, retry, and "do not double-charge" rules are written once.&lt;/li&gt;
&lt;li&gt;A slow vendor cannot pin every replica until the process is useless.&lt;/li&gt;
&lt;li&gt;We can prove, later, which workspace spent tokens or pages.&lt;/li&gt;
&lt;li&gt;Product services keep a stable internal contract when we swap a provider.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If any one of those is missing, the platform still demos. It does not stay cheap or operable at the scale above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three ways to call a model
&lt;/h2&gt;

&lt;p&gt;We treated this as a real fork, not a style preference. The same fork applies to paid web APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option A — install the vendor SDK in every service
&lt;/h3&gt;

&lt;p&gt;Runtime imports the SDK. A tools service imports it. An eval worker imports it. Each process holds a key and picks its own timeout.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Why it hurts at our scale&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Secrets surface area&lt;/td&gt;
&lt;td&gt;The API key is in N deployables. Rotation and audit are N operations.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Duplicated policy&lt;/td&gt;
&lt;td&gt;Each service chooses its own timeout, retry count, and logs. Those choices drift.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Streaming and retry&lt;/td&gt;
&lt;td&gt;After SSE tokens are already on the wire, a blind retry can duplicate what the user sees, or charge twice.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thundering herd&lt;/td&gt;
&lt;td&gt;Many replicas times many retries becomes vendor 429s and a runaway bill.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Compliance story&lt;/td&gt;
&lt;td&gt;It is hard to say "all model traffic is logged and capped in one place."&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This option is fine for one script. It is a poor fit once more than one service can spend vendor money.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option B — a shared npm package
&lt;/h3&gt;

&lt;p&gt;A package such as &lt;code&gt;@quickstart/llm-client&lt;/code&gt; puts the HTTP or SDK call in one module. Callers stay DRY. We still rejected it as the final shape.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Why a package was not enough&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Policy still runs in every process&lt;/td&gt;
&lt;td&gt;A global in-flight limit needs shared state, which for us means Redis. You end up rebuilding half a service inside a library.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Circuit state per replica&lt;/td&gt;
&lt;td&gt;If the package does not talk to Redis, each pod has its own breaker. Protection is uneven.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deploy coupling&lt;/td&gt;
&lt;td&gt;A new retry rule or a new provider means redeploying every consumer.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Operational ownership&lt;/td&gt;
&lt;td&gt;On-call still asks which service version ran the bad retry loop.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Billing hooks&lt;/td&gt;
&lt;td&gt;One caller forgets to pass &lt;code&gt;workspace_id&lt;/code&gt; or to log tokens. The ledger is already wrong.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A shared package fixes copy-paste. It does not give you one process that owns the vendor.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option C — dedicated internal services
&lt;/h3&gt;

&lt;p&gt;This is what we shipped.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;model-gateway&lt;/strong&gt;, port &lt;strong&gt;3005&lt;/strong&gt;. Every chat completion and every stream to an LLM vendor goes here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;web-fetch-service&lt;/strong&gt;, port &lt;strong&gt;3007&lt;/strong&gt;. Scrape, search, and async crawl go here. Direct HTTP and Olostep sit behind it as providers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Product services speak internal HTTP. They do not import vendor SDKs.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Criterion&lt;/th&gt;
&lt;th&gt;SDK in every service&lt;/th&gt;
&lt;th&gt;Shared npm package&lt;/th&gt;
&lt;th&gt;Dedicated model-gateway + web-fetch&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Secret sprawl&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retry and stream policy stays the same&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Global in-flight cap and budget&lt;/td&gt;
&lt;td&gt;Hard&lt;/td&gt;
&lt;td&gt;Hard unless every caller talks to Redis&lt;/td&gt;
&lt;td&gt;Redis inside the service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deploy when vendor policy changes&lt;/td&gt;
&lt;td&gt;Many services&lt;/td&gt;
&lt;td&gt;Many services&lt;/td&gt;
&lt;td&gt;One service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scale chat and LLM concurrency apart&lt;/td&gt;
&lt;td&gt;Coupled&lt;/td&gt;
&lt;td&gt;Coupled&lt;/td&gt;
&lt;td&gt;Decoupled&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Test vendor failures&lt;/td&gt;
&lt;td&gt;Per service&lt;/td&gt;
&lt;td&gt;Package tests&lt;/td&gt;
&lt;td&gt;Fake adapters plus orchestrator tests&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Why dedicated services fit this product
&lt;/h2&gt;

&lt;p&gt;We picked option C for five reasons that map onto the platform we already had.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One place for credentials and policy.&lt;/strong&gt; Timeouts, retries, circuits, caps, and usage logs sit next to the adapter that speaks to the vendor. Key rotation is one secret on one deployable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A stable contract for product services.&lt;/strong&gt; Runtime, future tools, and ingest workers call internal HTTP. Swapping a model vendor is &lt;code&gt;PROVIDER_PROFILES_JSON&lt;/code&gt; on the gateway, not a runtime release.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Separate scale knobs.&lt;/strong&gt; More runtime replicas means more concurrent chats. More model-gateway replicas means more LLM concurrency. More web-fetch replicas and workers means more page acquisition. Keys and policy stay in the two outbound services.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cost checked where the unit is known.&lt;/strong&gt; Web fetch denies a workspace that is over its daily unit budget in Redis, before the vendor call. Model usage is logged in one shape: tokens, latency, &lt;code&gt;workspace_id&lt;/code&gt;. There is no billing ledger on the request path yet. The facts the ledger will need are already there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Vendor keys stay off the public gateway.&lt;/strong&gt; Staff and end users present a JWT to port 3000. model-gateway and web-fetch trust a separate internal bearer token. In production those ports sit on a private network, with Kubernetes network policy planned as roadmap item P8.&lt;/p&gt;

&lt;p&gt;Knowledge and memory stay separate services. They have their own failure rules. This post is about the &lt;strong&gt;vendor outbound&lt;/strong&gt; pattern: model calls and web acquisition. RAG and memory are neighbors, not the same control plane.&lt;/p&gt;

&lt;h2&gt;
  
  
  How a request moves
&lt;/h2&gt;

&lt;p&gt;A user message hits the gateway. The gateway checks the JWT, checks that the person belongs to the workspace, and applies the public rate limit. It does not call OpenAI.&lt;/p&gt;

&lt;p&gt;The runtime on port &lt;strong&gt;3006&lt;/strong&gt; runs the turn. When it needs a model, it sends &lt;code&gt;POST /internal/complete&lt;/code&gt; (or &lt;code&gt;/internal/stream&lt;/code&gt;) to model-gateway with &lt;code&gt;Authorization: Bearer &amp;lt;MODEL_GATEWAY_INTERNAL_TOKEN&amp;gt;&lt;/code&gt;. The body carries &lt;code&gt;workspace_id&lt;/code&gt;, &lt;code&gt;request_id&lt;/code&gt;, a model profile, and the messages. The token is an internal secret, at least 32 characters. It is not the end user's JWT.&lt;/p&gt;

&lt;p&gt;model-gateway picks an adapter, runs policy, and only then calls OpenAI, Anthropic, OpenRouter, or another configured provider.&lt;/p&gt;

&lt;p&gt;Web acquisition is the parallel path. Knowledge ingest and future tools call &lt;code&gt;POST /internal/scrape&lt;/code&gt;, &lt;code&gt;/internal/search&lt;/code&gt;, or &lt;code&gt;/internal/crawl&lt;/code&gt; on web-fetch. That service calls direct HTTP or Olostep.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    [ Browser / API client ]
                              |
                              v
                    +---------------------+
                    |  API Gateway :3000  |
                    |  JWT, membership,   |
                    |  Redis rate limit   |
                    +----------+----------+
                               |
         +---------------------+---------------------+
         |                     |                     |
         v                     v                     v
 +---------------+    +----------------+    +------------------+
 | runtime :3006 |    | knowledge :3009|    | memory :3010     |
 | (turn loop)   |    | (RAG API)      |    | (facts API)      |
 +-------+-------+    +----------------+    +------------------+
         |
         |  POST /internal/complete
         v
 +---------------------+
 | model-gateway :3005 |
 | adapters + policy   |
 +----------+----------+
            |
            v
     [ OpenAI / Anthropic / OpenRouter / ... ]

 knowledge ingest, tools --&amp;gt; POST /internal/scrape|search|crawl
         |
         v
 +---------------------+
 | web-fetch :3007     |
 | adapters + policy   |
 +----------+----------+
            |
            v
     [ direct HTTP / Olostep / ... ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Faqibnawab.vercel.app%2Fstatic%2Fmedia%2Farchitecture.63eadd8a6e010ba517ad6a7bc3cb5614.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Faqibnawab.vercel.app%2Fstatic%2Fmedia%2Farchitecture.63eadd8a6e010ba517ad6a7bc3cb5614.svg" alt="Outbound path: gateway, product services, model-gateway, and web-fetch" width="1100" height="920"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Public clients stop at port 3000. Product services do not hold vendor keys. Only model-gateway and web-fetch talk to paid APIs.&lt;/p&gt;

&lt;p&gt;Inside model-gateway the split is simple, and it is the split we repeat in web-fetch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Runtime  --&amp;gt;  Orchestrator (timeout, retry, breaker, cap, logs)
                    |
                    v
              Adapter (one vendor's HTTP or SSE shape)
                    |
                    v
                 Vendor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adapters talk to vendors. Orchestrators talk to failure. That sentence is the whole design.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model gateway: one door for models
&lt;/h2&gt;

&lt;p&gt;model-gateway is the control plane for LLM calls. Runtime does not know whether the profile is OpenRouter or Anthropic.&lt;/p&gt;

&lt;p&gt;The internal routes we use are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;POST /internal/complete&lt;/code&gt; for a full response&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST /internal/stream&lt;/code&gt; for SSE: &lt;code&gt;token&lt;/code&gt; events, then &lt;code&gt;done&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST /internal/decide&lt;/code&gt; for an optional routing decision&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Auth on all of them is the internal bearer token.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adapters&lt;/strong&gt; implement a narrow interface, &lt;code&gt;ChatProvider&lt;/code&gt;, with &lt;code&gt;complete()&lt;/code&gt; and &lt;code&gt;stream()&lt;/code&gt;. The drivers we have are &lt;code&gt;fake&lt;/code&gt; (for CI), &lt;code&gt;openai_compatible&lt;/code&gt;, &lt;code&gt;openrouter&lt;/code&gt;, and &lt;code&gt;anthropic&lt;/code&gt;. A registry, built by &lt;code&gt;createProviderRegistry&lt;/code&gt;, maps a profile id from config to a driver instance. A profile is a named provider config, for example &lt;code&gt;openrouter-prod&lt;/code&gt;. The driver is the implementation type.&lt;/p&gt;

&lt;p&gt;Adapters throw &lt;code&gt;ProviderError&lt;/code&gt; with &lt;code&gt;retryable: true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;, and an optional HTTP status. They map the vendor's HTTP and SSE. They do not decide "retry three times." That split matters. The adapter knows that 429 and 400 are different. The orchestrator knows we only allow one retry, and that a stream which already sent tokens must not be retried.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The orchestrator&lt;/strong&gt; (&lt;code&gt;CompletionOrchestrator&lt;/code&gt;) wraps every call. Timeouts, the retry loop, the circuit breaker, the Redis in-flight cap, fallback, and &lt;code&gt;model.usage&lt;/code&gt; logs live here.&lt;/p&gt;

&lt;p&gt;What this gives the product:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We change vendor by config, without a runtime deploy.&lt;/li&gt;
&lt;li&gt;A request may name &lt;code&gt;fallback_model&lt;/code&gt;. If the primary profile fails and the stream has not started, we try that fallback once.&lt;/li&gt;
&lt;li&gt;Every successful or attempted call can emit a structured &lt;code&gt;model.usage&lt;/code&gt; log: tokens, latency, &lt;code&gt;workspace_id&lt;/code&gt;. That is the input for a later billing feature (Feature 26). The ledger itself is not on this path.&lt;/li&gt;
&lt;li&gt;Workspace bring-your-own-key is reserved for the secrets work (Feature 16). If a request says the credential source is the workspace, the gateway returns &lt;strong&gt;400&lt;/strong&gt; today. We would rather fail closed than store customer keys halfway.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Web fetch: the same shape, a different bill
&lt;/h2&gt;

&lt;p&gt;We did not put scrape inside model-gateway. The vendors differ. The billing unit differs: pages and search queries, not tokens. The policy differs: try cheap direct HTTP first, and use Olostep when we need JavaScript rendering or when direct fetch is not enough. Crawl is also asynchronous. It needs a Postgres job row and a worker. Those are bad reasons to share one orchestrator with chat completions.&lt;/p&gt;

&lt;p&gt;web-fetch is the control plane for acquisition.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;POST /internal/scrape&lt;/code&gt; is synchronous&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST /internal/search&lt;/code&gt; is synchronous, with a cap on &lt;code&gt;max_results&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST /internal/crawl&lt;/code&gt; returns a job id (HTTP 202 style)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GET /internal/jobs/:jobId&lt;/code&gt; returns status&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Auth is &lt;code&gt;WEB_FETCH_INTERNAL_TOKEN&lt;/code&gt;. A legacy alias is still accepted.&lt;/p&gt;

&lt;p&gt;The provider registry has &lt;code&gt;direct_http&lt;/code&gt;, &lt;code&gt;olostep&lt;/code&gt;, and &lt;code&gt;fake&lt;/code&gt;. Before the orchestrator runs, &lt;code&gt;decideAcquisitionProviders&lt;/code&gt; picks an ordered list of profile ids. For scrape, direct HTTP profiles come first, then Olostep. Search uses Olostep or the fake driver. &lt;code&gt;render_js&lt;/code&gt; goes down the Olostep path.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;AcquisitionOrchestrator&lt;/code&gt; then applies caps, the workspace budget, the breaker, the profile chain, and retries.&lt;/p&gt;

&lt;p&gt;What this gives the product:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Knowledge ingest (Feature 11) and future tools call one service. We do not put the Olostep SDK in five places.&lt;/li&gt;
&lt;li&gt;Each workspace has a &lt;strong&gt;daily unit budget&lt;/strong&gt; in Redis. Over budget means 429, and the vendor is not called.&lt;/li&gt;
&lt;li&gt;Usage facts record &lt;code&gt;action&lt;/code&gt;, &lt;code&gt;provider&lt;/code&gt;, &lt;code&gt;units&lt;/code&gt;, and &lt;code&gt;estimated_cost_micros&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Crawl inserts a row as &lt;code&gt;queued&lt;/code&gt;. A worker claims it with &lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt;, so two replicas can poll without taking the same job.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;URL ingest from the knowledge service into web-fetch is still a follow-up. The service exists. That wire is not the whole story yet. I say that again in the limitations, because it is easy to over-claim.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the resilience works, and why we did not take the simpler version
&lt;/h2&gt;

&lt;p&gt;Each pattern below is something we run inside these two services. I include the simpler idea we rejected, because that is the part that is easy to skip when you are only trying to make the call succeed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adapters plus a registry
&lt;/h3&gt;

&lt;p&gt;Vendor code implements a small interface. Config names a profile. The registry returns the driver for that profile id.&lt;/p&gt;

&lt;p&gt;New vendor means a new adapter file and a config row. Runtime does not fork. In CI, the &lt;code&gt;fake&lt;/code&gt; driver can simulate a 429, a hang, a failure before any token, and a failure after tokens, with no network.&lt;/p&gt;

&lt;p&gt;The SDK already speaks the vendor's shape. Our product speaks one shape: a completion body, or a scrape body. The adapter is the anti-corruption layer between those two shapes. If the adapter also owns retry policy, every new vendor copies a slightly different loop. We kept that loop out.&lt;/p&gt;

&lt;h3&gt;
  
  
  One orchestrator per domain
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;CompletionOrchestrator&lt;/code&gt; and &lt;code&gt;AcquisitionOrchestrator&lt;/code&gt; wrap the outbound call. Callers stay thin. Runtime does not parse OpenRouter error bodies.&lt;/p&gt;

&lt;p&gt;LLM streaming rules are not the same as a scrape profile chain, so we did not force one giant policy class. The primitives match. The policy does not.&lt;/p&gt;

&lt;p&gt;Without this layer, &lt;code&gt;if (retry)&lt;/code&gt; grows inside each adapter. Six months later, two providers retry differently and nobody remembers which one double-charged a stream. On-call for "the model vendor is down" is one service, one log shape, not a search across every consumer's version.&lt;/p&gt;

&lt;h3&gt;
  
  
  A deadline on every vendor call
&lt;/h3&gt;

&lt;p&gt;Vendor TCP can hang. At 10,000 concurrent chats, one hung connection holds work in the Node process until the event loop lags and other chats stall.&lt;/p&gt;

&lt;p&gt;model-gateway races the provider call against a timer (&lt;code&gt;UPSTREAM_TIMEOUT&lt;/code&gt;, about 60 seconds for a non-streaming complete). Web fetch uses a per-action timeout around 30 seconds, as we wrote in ADR 0011. On the model path a timeout is retryable, inside the one-retry rule below. Vendor SDK defaults differ, and some are long enough to exhaust the process before they fire.&lt;/p&gt;

&lt;h3&gt;
  
  
  At most one extra attempt, and only when the error says so
&lt;/h3&gt;

&lt;p&gt;model-gateway allows a &lt;strong&gt;maximum of two attempts&lt;/strong&gt; when the error is retryable: &lt;code&gt;ProviderError.retryable&lt;/code&gt;, a timeout, or an abort. Web fetch allows up to two attempts &lt;strong&gt;per profile&lt;/strong&gt;, and only when the error is retryable &lt;strong&gt;and&lt;/strong&gt; no billable unit has been recorded yet.&lt;/p&gt;

&lt;p&gt;We do not retry until success. Each retry costs money and adds to turn latency. A 429 plus a retry storm turns a vendor rate limit into an outage we caused.&lt;/p&gt;

&lt;p&gt;The retryable flag stays on the adapter, because HTTP meaning belongs next to the vendor. The attempt budget stays on the orchestrator, because "only once, and never after tokens" is a product rule.&lt;/p&gt;

&lt;h3&gt;
  
  
  If the stream already started, stop
&lt;/h3&gt;

&lt;p&gt;This rule is model-gateway only, and it is the one I care about most.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;stream&lt;/code&gt;, if any token was already sent to the client, we do not retry and we do not switch to the fallback model. The attempt loop breaks.&lt;/p&gt;

&lt;p&gt;A retry after partial SSE duplicates content. The user sees two answers stuck together, and the vendor may bill both attempts. Every caller that owns an SDK has to remember this rule. In model-gateway it exists once.&lt;/p&gt;

&lt;h3&gt;
  
  
  One fallback model, not a chain of five
&lt;/h3&gt;

&lt;p&gt;If the primary profile fails policy and the stream has not started, we try &lt;code&gt;fallback_model&lt;/code&gt; once. The point is degradation: a cheaper or more available model instead of a hard error to the user. The bound is deliberate. A chain of five models looks resilient and spends like an incident.&lt;/p&gt;

&lt;p&gt;Web fetch is different on purpose. &lt;code&gt;decideAcquisitionProviders&lt;/code&gt; returns an &lt;strong&gt;ordered list&lt;/strong&gt; of profile ids, for example direct HTTP then Olostep. The orchestrator walks that list in &lt;code&gt;executeScrape&lt;/code&gt; until one succeeds or the list is done. Direct fetch is cheap. Olostep is for JavaScript rendering or for when the cheap path failed. That is multi-provider routing for acquisition, not a primary and secondary model id. We did not pretend those are the same feature.&lt;/p&gt;

&lt;h3&gt;
  
  
  A circuit breaker for a sustained outage
&lt;/h3&gt;

&lt;p&gt;Retry helps a blip. A breaker helps a vendor that is down for minutes.&lt;/p&gt;

&lt;p&gt;We share a &lt;code&gt;CircuitBreaker&lt;/code&gt; implementation. After N failures the state is &lt;strong&gt;open&lt;/strong&gt;. New calls fail fast with &lt;code&gt;CIRCUIT_OPEN&lt;/code&gt; instead of waiting on another timeout. After a cooldown the breaker goes &lt;strong&gt;half-open&lt;/strong&gt;. A success closes it. Each orchestrator keeps a breaker per profile id.&lt;/p&gt;

&lt;p&gt;When the vendor is down, a fast 503 means replicas are not stacked on timeouts, and the vendor is not hit by a synchronized retry from every pod.&lt;/p&gt;

&lt;p&gt;That state is &lt;strong&gt;inside the process&lt;/strong&gt; today, not in Redis. Two pods can disagree for a while. Per-replica breakers still help. They are not a cluster-wide view.&lt;/p&gt;

&lt;h3&gt;
  
  
  A fleet cap in Redis before the vendor call
&lt;/h3&gt;

&lt;p&gt;A library-level semaphore counts in-flight calls inside one process. The moment we add a second replica, the vendor sees twice the concurrency. That is the opposite of a cap.&lt;/p&gt;

&lt;p&gt;Before model-gateway or web-fetch calls a vendor, we increment a Redis key (&lt;code&gt;model-gateway:inflight&lt;/code&gt; or &lt;code&gt;web-fetch:inflight&lt;/code&gt;). If the count is over the max, we return &lt;strong&gt;429&lt;/strong&gt; with &lt;code&gt;RATE_LIMITED&lt;/code&gt; and we do not call the vendor. The increment is released when the call finishes.&lt;/p&gt;

&lt;p&gt;This is a bulkhead. Model slowness cannot occupy unbounded work. It is also the start of stopping one tenant from filling every vendor connection. The global cap exists. &lt;strong&gt;Per-workspace dollar or concurrency caps on the model path do not.&lt;/strong&gt; Those are later runtime and Redis work.&lt;/p&gt;

&lt;p&gt;This is also why a shared package was not enough. A fleet cap has to live in one service plus Redis.&lt;/p&gt;

&lt;h3&gt;
  
  
  Budget where the unit is the bill
&lt;/h3&gt;

&lt;p&gt;The public gateway already rate-limits HTTP with Redis. That protects the front door. It does not know what a scrape costs.&lt;/p&gt;

&lt;p&gt;web-fetch keeps a per-workspace daily unit budget in Redis and rejects with 429 &lt;strong&gt;before&lt;/strong&gt; the vendor call. Acquisition is billed per unit. We cannot depend on every future caller remembering to check. One workspace in a crawl loop should stop when the budget is gone, not when the invoice arrives.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reject work that cannot succeed
&lt;/h3&gt;

&lt;p&gt;Some requests are wrong before any vendor should see them. Examples: bring-your-own-key with &lt;code&gt;credential_source: workspace&lt;/code&gt; while secrets are not shipped (400), a bad URL, a bad workspace id, a missing &lt;code&gt;request_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The rule is simple. Do not pay a vendor for a request we already know will fail. Callers also get a clear 4xx instead of a 5xx that looks like an outage.&lt;/p&gt;

&lt;h3&gt;
  
  
  One error language
&lt;/h3&gt;

&lt;p&gt;Vendors return a mess of bodies and status codes. We map that to platform codes: &lt;code&gt;UPSTREAM_UNAVAILABLE&lt;/code&gt;, &lt;code&gt;UPSTREAM_TIMEOUT&lt;/code&gt;, &lt;code&gt;RATE_LIMITED&lt;/code&gt;, &lt;code&gt;VALIDATION_FAILED&lt;/code&gt;, &lt;code&gt;UNAUTHORIZED&lt;/code&gt;. Runtime and the gateway branch on those codes. Logs do not store raw vendor bodies. ADR 0010 is the failure-policy contract for the model path.&lt;/p&gt;

&lt;p&gt;This is what lets product services stay ignorant of vendor semantics. They handle a small set of outcomes. They do not special-case one provider's JSON.&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage logs now, ledger later
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;model.usage&lt;/code&gt; and the acquisition usage facts carry &lt;code&gt;workspace_id&lt;/code&gt;, &lt;code&gt;request_id&lt;/code&gt;, tokens or units, &lt;code&gt;latency_ms&lt;/code&gt;, and the provider profile. Billing must not run inside the hot path of a chat turn. If the fields arrive only when the ledger exists, the months before that cannot be audited. One service keeps the field names the same.&lt;/p&gt;

&lt;h3&gt;
  
  
  Long work does not hold the HTTP request
&lt;/h3&gt;

&lt;p&gt;Crawl returns a job id and finishes in a worker. Knowledge ingest uses a queue and a worker in its own service. Same idea, different process: the request thread must not wait for N pages or for embeddings. Clients disconnect. Timeouts fire. The door stays blocked.&lt;/p&gt;

&lt;p&gt;Workers scale by claiming jobs. For crawl, &lt;code&gt;SKIP LOCKED&lt;/code&gt; is what makes two replicas safe. For knowledge, more consumers read the SQS-shaped queue. model-gateway's philosophy is "do not block the door with an unbounded vendor wait." Crawl applies that to a job that is long by nature.&lt;/p&gt;

&lt;h3&gt;
  
  
  Idempotency sits on the turn
&lt;/h3&gt;

&lt;p&gt;model-gateway is not the idempotency store for chat. Conversations already take an idempotency key on messages and turns. A client retry must not double-append and must not double-call the model for the same turn key.&lt;/p&gt;

&lt;p&gt;I mention it because vendor retry policy cannot fix a duplicate turn that our own API accepted twice. Resilience is end to end. The vendor layer is one cut. The conversation write is another. We documented them as related, not as the same mechanism.&lt;/p&gt;

&lt;h2&gt;
  
  
  How internal calls stay different from public calls
&lt;/h2&gt;

&lt;p&gt;We did not want "internal" to mean "no identity, just a private port." The boundary is explicit.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concern&lt;/th&gt;
&lt;th&gt;Public path&lt;/th&gt;
&lt;th&gt;Internal path&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Identity&lt;/td&gt;
&lt;td&gt;JWT &lt;code&gt;sub&lt;/code&gt; becomes the staff &lt;code&gt;actorId&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Service bearer token&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tenant&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;workspace_id&lt;/code&gt; in the URL, plus a membership check&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;workspace_id&lt;/code&gt; on the body or route, from a trusted caller&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exposure&lt;/td&gt;
&lt;td&gt;Internet, through the gateway&lt;/td&gt;
&lt;td&gt;Private network, or localhost ports in development&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stability&lt;/td&gt;
&lt;td&gt;Versioned public API&lt;/td&gt;
&lt;td&gt;Can change with the contracts package&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Runtime's &lt;code&gt;ModelGatewayClient&lt;/code&gt; posts to &lt;code&gt;{MODEL_GATEWAY_URL}/internal/complete&lt;/code&gt; with the internal token and a JSON body from &lt;code&gt;@quickstart/contracts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We chose internal HTTP, not "only gRPC" and not a shared SDK, for three practical reasons. A network hop is something we can later lock with Kubernetes network policy. Another language can call the same contract without importing our TypeScript package. And a hop forces a timeout and an error envelope at the boundary. An in-process function call makes it too easy to forget both.&lt;/p&gt;

&lt;p&gt;The public gateway still does not replace per-service checks. Membership and permissions are enforced again downstream. A valid JWT at port 3000 is not a blank permission inside every service.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the pieces scale
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;What we add&lt;/th&gt;
&lt;th&gt;Shared state&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;API gateway&lt;/td&gt;
&lt;td&gt;More replicas&lt;/td&gt;
&lt;td&gt;Redis rate limit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Runtime&lt;/td&gt;
&lt;td&gt;More replicas&lt;/td&gt;
&lt;td&gt;None in the process. Conversations live in Postgres&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;model-gateway&lt;/td&gt;
&lt;td&gt;More replicas&lt;/td&gt;
&lt;td&gt;Redis in-flight cap. Breakers are per pod&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;web-fetch&lt;/td&gt;
&lt;td&gt;More replicas and more workers&lt;/td&gt;
&lt;td&gt;Redis budget and in-flight cap. Crawl jobs in Postgres&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Knowledge worker&lt;/td&gt;
&lt;td&gt;More consumers&lt;/td&gt;
&lt;td&gt;SQS-shaped queue&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Chat capacity and LLM concurrency are separate dials. Adding runtime pods does not, by itself, multiply vendor connections, because the cap lives in Redis behind model-gateway. Adding model-gateway pods does not copy the API key into the runtime.&lt;/p&gt;

&lt;p&gt;Breakers are the exception. They are per pod until we share that state. Doubling model-gateway replicas does not double the in-flight cap. During an outage, each pod can still let a few calls through until its own breaker opens.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cost, keys, and the on-call page
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Cost.&lt;/strong&gt; One extra model attempt, and none after the stream starts, is the spend limit on retries. Web fetch spends the cheap direct fetch first and stops the workspace at the Redis budget. Token and unit logs are what we will use for chargeback. They are not the invoice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keys and compliance.&lt;/strong&gt; Vendor credentials are deploy secrets on model-gateway and web-fetch only. Service-to-service calls use internal tokens. Staff JWTs stay on public routes. Internal ports are meant to sit on a private network in production.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Operations.&lt;/strong&gt; Fake providers let CI exercise 429s, hangs, and partial streams without a vendor account. web-fetch has circuit tests in its suite. When OpenRouter is down, there is one service to open, not a list of every repo that imported a client.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we would do next
&lt;/h2&gt;

&lt;p&gt;Three items are already implied by the limits above. They are not shipped.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Put circuit state in a shared store so every pod agrees that a profile is open.&lt;/li&gt;
&lt;li&gt;Add per-workspace caps on LLM calls. Today the model path only has a global in-flight cap.&lt;/li&gt;
&lt;li&gt;Store workspace keys through the secrets work, then let model-gateway use them. Until that exists, those requests stay a 400.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What we still don't do
&lt;/h2&gt;

&lt;p&gt;I want this list in the post so the architecture diagram is not read as a finished platform.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Circuit breaker state is &lt;strong&gt;per process&lt;/strong&gt;, not cluster-wide.&lt;/li&gt;
&lt;li&gt;model-gateway does &lt;strong&gt;not&lt;/strong&gt; enforce per-workspace dollar caps. It enforces a global in-flight cap. Per-tenant caps are planned elsewhere.&lt;/li&gt;
&lt;li&gt;There is &lt;strong&gt;no billing ledger&lt;/strong&gt; on the request path. We write usage logs only.&lt;/li&gt;
&lt;li&gt;Knowledge &lt;strong&gt;embeddings&lt;/strong&gt; use a lighter pattern. They do not go through the full provider registry and orchestrator described here.&lt;/li&gt;
&lt;li&gt;Knowledge calling web-fetch for URL ingest is a roadmap follow-up.&lt;/li&gt;
&lt;li&gt;The public gateway does &lt;strong&gt;not&lt;/strong&gt; replace per-service RBAC. Membership and permissions are still enforced downstream.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Memory reads can degrade when memory is down. A knowledge-base turn can fail when retrieval was required and RAG is not healthy. Those are different features with different failure choices. They are not the outbound policy in this post. I include them only so "resilience" is not mistaken for "every dependency fails open."&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;The hard part was not the first successful LLM call. It was keeping keys, retries, streams, and spend in one place while chat traffic and vendor traffic grow on different curves.&lt;/p&gt;

&lt;p&gt;An SDK in every service spreads the key and the mistakes. A shared package shares the code and still leaves policy inside every process. model-gateway and web-fetch are where vendor credentials and vendor failure live.&lt;/p&gt;

&lt;p&gt;Adapters translate one vendor. Orchestrators decide what failure is allowed to cost. The rest of the platform calls a contract.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>systemdesign</category>
      <category>backend</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Voice Jitter Buffer: The Hardest Part of Building AI Voice Agents</title>
      <dc:creator>Aqib</dc:creator>
      <pubDate>Tue, 03 Nov 2026 16:39:25 +0000</pubDate>
      <link>https://dev.to/aqibnawab/voice-jitter-buffer-the-hardest-part-of-building-ai-voice-agents-l0n</link>
      <guid>https://dev.to/aqibnawab/voice-jitter-buffer-the-hardest-part-of-building-ai-voice-agents-l0n</guid>
      <description>&lt;p&gt;When people talk about AI voice agents, they usually talk about the model. They talk about prompts, tools, and how smart the reply sounds. That is only half the story.&lt;/p&gt;

&lt;p&gt;The other half is much quieter, and much harder. It lives under the audio pipeline. It is called a &lt;strong&gt;jitter buffer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you get the jitter buffer wrong, your agent does not sound slow in a polite way. It sounds broken. It cuts people off mid-sentence. It answers into silence. It waits too long and kills the feeling of a real conversation.&lt;/p&gt;

&lt;p&gt;This post is a deep dive into that piece. We will start from how voice travels on the internet, open an &lt;strong&gt;RTP packet&lt;/strong&gt; and look at fields like sequence number and timestamp, then look at why fixed buffers fail for AI, and finally look at how adaptive jitter buffers try to solve the trade-off.&lt;/p&gt;




&lt;h2&gt;
  
  
  The internet does not deliver audio in a straight line
&lt;/h2&gt;

&lt;p&gt;When you speak into a microphone, the sound starts as a continuous wave. Computers cannot ship that wave as one long stream across the public internet. So the system cuts the audio into tiny pieces called &lt;strong&gt;packets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In voice systems, one packet often carries about &lt;strong&gt;20 milliseconds&lt;/strong&gt; of audio. That is a very small slice of speech. Your “hello” may travel as many packets, one after another.&lt;/p&gt;

&lt;p&gt;Those packets usually travel over &lt;strong&gt;UDP&lt;/strong&gt;. UDP is fast because it does not wait for a receipt. The sender does not sit there asking, “Did you get packet 17?” It just keeps sending.&lt;/p&gt;

&lt;p&gt;That speed is useful for voice. The cost is reliability. Packets can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;arrive late&lt;/li&gt;
&lt;li&gt;take different routes&lt;/li&gt;
&lt;li&gt;land out of order&lt;/li&gt;
&lt;li&gt;disappear completely&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The variance in arrival time is called &lt;strong&gt;jitter&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If the receiving side plays each packet the moment it arrives, the audio does not sound like a human voice. It sounds like a skipping CD. You hear gaps, robotic jumps, and broken words.&lt;/p&gt;

&lt;p&gt;So the problem is not only “send audio.” The problem is “rebuild smooth speech from messy packet arrival.”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mic wave
  -&amp;gt; cut into 20ms packets
  -&amp;gt; wrap each slice in an RTP header
  -&amp;gt; send over UDP
  -&amp;gt; packets arrive late / early / out of order
  -&amp;gt; without a buffer: glitchy audio
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is why voice systems do not send raw audio blobs with no labels. They send &lt;strong&gt;RTP&lt;/strong&gt; packets. RTP is the envelope that tells the receiver how to put speech back together.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is inside an RTP packet?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;RTP&lt;/strong&gt; means Real-time Transport Protocol. In WebRTC and most VoIP systems, your voice does not travel as “just UDP bytes.” It travels as RTP on top of UDP.&lt;/p&gt;

&lt;p&gt;Think of each RTP packet as a small box with two parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;strong&gt;header&lt;/strong&gt; with labels the receiver needs&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;payload&lt;/strong&gt; with the actual compressed audio
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+--------------------------------------------------+
| RTP header                                       |
|  seq | timestamp | SSRC | payload type | ...     |
+--------------------------------------------------+
| Payload: encoded audio (Opus, PCMU, etc.)        |
+--------------------------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The jitter buffer mostly cares about the header labels. Without those labels, late packets are just noise. With those labels, the receiver can reorder, detect loss, and decide when to play each slice of audio.&lt;/p&gt;

&lt;p&gt;Here are the fields that matter most when you are building or debugging AI voice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sequence number
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;sequence number&lt;/strong&gt; is a simple counter. The sender increases it by one for every RTP packet it sends.&lt;/p&gt;

&lt;p&gt;If the sender sends packets like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;seq 101 -&amp;gt; seq 102 -&amp;gt; seq 103 -&amp;gt; seq 104
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the receiver gets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;seq 101 -&amp;gt; seq 103 -&amp;gt; seq 102 -&amp;gt; seq 104
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the sequence number tells the buffer the true order. Packet 102 is not “wrong audio.” It is just late. The buffer can hold 103 for a moment, wait for 102, then play them in the right order.&lt;/p&gt;

&lt;p&gt;Sequence numbers also help detect loss. If you see 101, then suddenly 104, you know 102 and 103 are missing for now. Maybe they will still arrive. Maybe they are gone. The buffer has to decide how long to wait before giving up.&lt;/p&gt;

&lt;p&gt;In plain words:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sequence number answers:&lt;/strong&gt; “Which packet comes next?”&lt;/li&gt;
&lt;li&gt;It is the main tool for &lt;strong&gt;ordering&lt;/strong&gt; and &lt;strong&gt;loss detection&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Timestamp
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;timestamp&lt;/strong&gt; is different from the sequence number, and this difference confuses many people at first.&lt;/p&gt;

&lt;p&gt;The sequence number counts packets. The timestamp marks &lt;strong&gt;media time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It tells the receiver when the first sample in this packet should sit on the audio timeline. It is not a wall-clock time like “3:41:02 PM on your laptop.” It is a media clock based on the audio sample rate.&lt;/p&gt;

&lt;p&gt;A simple example helps.&lt;/p&gt;

&lt;p&gt;Suppose you send 20ms of audio in each packet, and the clock runs at 8000 samples per second. Then each packet often advances the timestamp by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8000 samples/sec * 0.020 sec = 160
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So timestamps may look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;packet A: timestamp 1600
packet B: timestamp 1760
packet C: timestamp 1920
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if packet B arrives after packet C on the network, the timestamps still say B should play before C.&lt;/p&gt;

&lt;p&gt;Why do we need both sequence number and timestamp?&lt;/p&gt;

&lt;p&gt;Because one packet is not always one equal slice of time in every codec setup, and because silence, packetization, and frame packing can make timing more subtle. Sequence number keeps packet order. Timestamp keeps playout timing.&lt;/p&gt;

&lt;p&gt;In plain words:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Timestamp answers:&lt;/strong&gt; “When should this audio be played on the media timeline?”&lt;/li&gt;
&lt;li&gt;It is the main tool for &lt;strong&gt;smooth playout&lt;/strong&gt; and &lt;strong&gt;jitter measurement&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Payload type
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;payload type&lt;/strong&gt; tells the receiver which codec is inside the packet.&lt;/p&gt;

&lt;p&gt;Examples people see in real systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PCMU / PCMA for classic telephony audio&lt;/li&gt;
&lt;li&gt;Opus for modern WebRTC voice&lt;/li&gt;
&lt;li&gt;other dynamic payload types negotiated during the call setup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the receiver thinks the packet is Opus but the bytes are actually something else, decode fails. So this field is small, but it is important. It says, “Here is how you should unpack the payload.”&lt;/p&gt;

&lt;h3&gt;
  
  
  SSRC
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;SSRC&lt;/strong&gt; means Synchronization Source. It is an ID for one media stream from one sender.&lt;/p&gt;

&lt;p&gt;If your call has one microphone stream, that stream has one SSRC. If another person joins, or if a mixer creates a new stream, you get another SSRC.&lt;/p&gt;

&lt;p&gt;The receiver uses SSRC to keep streams separate. Your jitter buffer for speaker A should not mix packets from speaker B just because both arrived on the same socket.&lt;/p&gt;

&lt;p&gt;In plain words:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SSRC answers:&lt;/strong&gt; “Which talker or which stream does this packet belong to?”&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Marker bit
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;marker bit&lt;/strong&gt; is a one-bit flag. In audio, it is often used to mark the start of a new talkspurt, meaning the moment someone starts speaking again after silence.&lt;/p&gt;

&lt;p&gt;This matters for voice agents because silence and speech boundaries are exactly where VAD and turn-taking get sensitive. The marker bit is not the full VAD system by itself, but it is one more signal in the media layer about where a speech burst begins.&lt;/p&gt;

&lt;h3&gt;
  
  
  Payload
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;payload&lt;/strong&gt; is the actual compressed audio. This is the Opus frame, or PCMU samples, or whatever codec you negotiated.&lt;/p&gt;

&lt;p&gt;Everything above the payload exists to protect this part. The header is the map. The payload is the voice.&lt;/p&gt;

&lt;h3&gt;
  
  
  A quick map of the important RTP fields
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;What it is&lt;/th&gt;
&lt;th&gt;Why the jitter buffer cares&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Sequence number&lt;/td&gt;
&lt;td&gt;Packet counter&lt;/td&gt;
&lt;td&gt;Reorder packets, detect gaps and loss&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Timestamp&lt;/td&gt;
&lt;td&gt;Media-clock time&lt;/td&gt;
&lt;td&gt;Decide playout timing, measure jitter&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Payload type&lt;/td&gt;
&lt;td&gt;Codec ID&lt;/td&gt;
&lt;td&gt;Decode the bytes correctly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SSRC&lt;/td&gt;
&lt;td&gt;Stream ID&lt;/td&gt;
&lt;td&gt;Keep one talker’s packets together&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Marker bit&lt;/td&gt;
&lt;td&gt;Talkspurt / frame hint&lt;/td&gt;
&lt;td&gt;Help spot speech burst boundaries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Payload&lt;/td&gt;
&lt;td&gt;Encoded audio&lt;/td&gt;
&lt;td&gt;The actual sound the AI must hear&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  What RTP does not do
&lt;/h3&gt;

&lt;p&gt;RTP itself does not guarantee delivery. It does not retransmit lost packets by default. It does not fix jitter by itself.&lt;/p&gt;

&lt;p&gt;RTP only labels the media carefully. The hard work still sits on the receiving side: hold packets, sort them, wait a little, and decide when silence is real silence versus a late packet.&lt;/p&gt;

&lt;p&gt;That receiving-side logic is the jitter buffer.&lt;/p&gt;

&lt;p&gt;There is also a partner protocol called &lt;strong&gt;RTCP&lt;/strong&gt;. RTCP carries reports about quality: loss rate, jitter estimates, and timing stats between sender and receiver. Adaptive systems often use those reports, together with local arrival measurements, to decide whether the buffer should grow or shrink.&lt;/p&gt;




&lt;h2&gt;
  
  
  The jitter buffer is a waiting room for packets
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;jitter buffer&lt;/strong&gt; is the waiting room.&lt;/p&gt;

&lt;p&gt;Instead of playing a packet the millisecond it arrives, the receiver holds packets for a short time. That short wait gives delayed packets a chance to show up. Then the system uses the &lt;strong&gt;sequence number&lt;/strong&gt; to line them up, uses the &lt;strong&gt;timestamp&lt;/strong&gt; to place them on the media timeline, and feeds clean audio forward at a steady rate.&lt;/p&gt;

&lt;p&gt;Think of it like people entering a metro station during rush hour. If you open the gate for every person the second they arrive, the platform becomes chaotic. If you hold people for a few seconds and then let them board in order, the flow feels smooth.&lt;/p&gt;

&lt;p&gt;In audio terms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;RTP packets enter the buffer.&lt;/li&gt;
&lt;li&gt;The buffer reads sequence numbers and timestamps.&lt;/li&gt;
&lt;li&gt;The buffer waits a little for missing sequence numbers.&lt;/li&gt;
&lt;li&gt;Late packets get a chance to catch up.&lt;/li&gt;
&lt;li&gt;Packets leave the buffer in media order, at a constant pace.&lt;/li&gt;
&lt;li&gt;Speech-to-text (STT) hears cleaner audio.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A common starting point is something like &lt;strong&gt;40 milliseconds&lt;/strong&gt; of buffering. That number is not magic. It is a compromise. Enough wait to hide small network bumps. Not so much wait that the whole call feels delayed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Arriving packets (messy timing)
        |
        v
  [  jitter buffer  ]  &amp;lt;-- short waiting room
        |
        v
Smooth audio stream into STT / VAD / the AI pipeline
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This idea is old in VoIP. Zoom-style calls have lived with it for years. The hard part starts when the listener on the other side is not a human. The listener is an AI agent with Voice Activity Detection, an LLM, and text-to-speech.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why this is easy for Zoom and hard for AI voice agents
&lt;/h2&gt;

&lt;p&gt;In a normal phone or video call, both sides are humans. Humans are flexible. If one packet is slightly late, your brain fills the gap. If the other person pauses for half a second, you wait. You do not instantly decide, “They finished talking, so I should start speaking now.”&lt;/p&gt;

&lt;p&gt;An AI voice agent does not have that patience by default.&lt;/p&gt;

&lt;p&gt;The agent pipeline usually looks like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Capture caller audio&lt;/li&gt;
&lt;li&gt;Smooth it through a jitter buffer&lt;/li&gt;
&lt;li&gt;Run Voice Activity Detection (VAD)&lt;/li&gt;
&lt;li&gt;Transcribe speech to text&lt;/li&gt;
&lt;li&gt;Send text to an LLM&lt;/li&gt;
&lt;li&gt;Generate a reply&lt;/li&gt;
&lt;li&gt;Turn the reply into speech with TTS&lt;/li&gt;
&lt;li&gt;Stream that speech back to the caller&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every stage adds time. The jitter buffer is only one part of the total delay. But it sits at the front door, so bad choices here poison everything after it.&lt;/p&gt;

&lt;p&gt;This creates a painful trade-off.&lt;/p&gt;

&lt;h3&gt;
  
  
  If the buffer is too large
&lt;/h3&gt;

&lt;p&gt;Imagine a fixed buffer of &lt;strong&gt;150 milliseconds&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Audio quality can look great. Delayed packets have more time to arrive. The STT engine hears cleaner speech. On paper, this feels safe.&lt;/p&gt;

&lt;p&gt;In a live AI conversation, that “safe” wait is expensive.&lt;/p&gt;

&lt;p&gt;You add 150ms before the AI even starts understanding you. Then you still pay for transcription time, model thinking time, and TTS time. By the time the agent answers, the caller already feels an awkward pause. A two-second gap destroys the illusion of natural talk. People stop trusting the product. They start speaking over the bot, or they hang up.&lt;/p&gt;

&lt;p&gt;Large buffers protect quality. They also make the agent feel slow and unnatural.&lt;/p&gt;

&lt;h3&gt;
  
  
  If the buffer is too small
&lt;/h3&gt;

&lt;p&gt;Now imagine a tiny buffer of &lt;strong&gt;10 milliseconds&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Response time looks fast. The pipeline feels “real-time.” On a perfect network, this can work.&lt;/p&gt;

&lt;p&gt;On a normal mobile network, it fails in a nasty way.&lt;/p&gt;

&lt;p&gt;When a packet arrives late, a small buffer cannot wait for it. The packet gets dropped. In RTP terms, you may see a hole in the sequence numbers. The AI then hears a tiny hole of silence inside your sentence. Voice Activity Detection sees that silence and thinks you stopped talking. The agent decides it is its turn. It starts answering while you are still mid-thought.&lt;/p&gt;

&lt;p&gt;That is the fatal AI flaw with small buffers.&lt;/p&gt;

&lt;p&gt;The failure is not only bad audio. The failure is wrong turn-taking. The agent interrupts you because Wi-Fi had a hiccup, not because you finished your sentence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Too large buffer:
  better audio quality
  higher latency
  awkward pauses
  conversation feels slow

Too small buffer:
  lower latency
  more packet drops
  fake silence holes
  VAD thinks you stopped
  agent interrupts mid-sentence
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why jitter buffering is one of the hardest parts of building AI voice agents. You are not only optimizing sound. You are protecting the social rhythm of a conversation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fixed buffers are too dumb for real networks
&lt;/h2&gt;

&lt;p&gt;A fixed-size buffer assumes the network stays roughly the same. Real networks do not stay the same.&lt;/p&gt;

&lt;p&gt;A caller can start on good office Wi-Fi, then walk into a lift. A mobile user can move from strong LTE into a crowded street. Packet delay can jump within seconds. A buffer that was perfect one minute ago can become too small or too large in the next minute.&lt;/p&gt;

&lt;p&gt;That is why modern voice systems, including WebRTC stacks, move toward &lt;strong&gt;Adaptive Jitter Buffers (AJB)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;An adaptive buffer behaves like a smart shock absorber on a car. On a smooth road, it stays tight and responsive. On a broken road, it softens and absorbs the bumps.&lt;/p&gt;




&lt;h2&gt;
  
  
  How an adaptive jitter buffer thinks
&lt;/h2&gt;

&lt;p&gt;An adaptive jitter buffer keeps watching the network and changing its own size.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Network monitoring
&lt;/h3&gt;

&lt;p&gt;It constantly measures variance using RTP metadata and arrival time.&lt;/p&gt;

&lt;p&gt;A practical way to think about it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compare &lt;strong&gt;arrival time on the wire&lt;/strong&gt; with the packet’s &lt;strong&gt;RTP timestamp&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Watch gaps in the &lt;strong&gt;sequence number&lt;/strong&gt; to estimate loss&lt;/li&gt;
&lt;li&gt;Track how often packets arrive out of order&lt;/li&gt;
&lt;li&gt;Use local stats, and sometimes &lt;strong&gt;RTCP&lt;/strong&gt; reports, to see if the path is getting messy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is not a one-time setup. It is continuous measurement during the call.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Dynamic resizing
&lt;/h3&gt;

&lt;p&gt;If the connection looks clean, the buffer can shrink toward something like &lt;strong&gt;10–20 milliseconds&lt;/strong&gt;. That keeps the AI snappy. The agent can start thinking sooner, which helps the reply feel natural.&lt;/p&gt;

&lt;p&gt;If the caller moves into weak cell coverage, the buffer can grow toward something like &lt;strong&gt;60–80 milliseconds&lt;/strong&gt;. That extra room catches delayed packets before they become silence holes. The goal is to stop VAD from hallucinating an end-of-turn.&lt;/p&gt;

&lt;p&gt;So the buffer is not “set once and forget.” It breathes with the network.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Time stretching
&lt;/h3&gt;

&lt;p&gt;Advanced systems go one step further. WebRTC’s &lt;strong&gt;NetEQ&lt;/strong&gt; is a famous example in this family of ideas.&lt;/p&gt;

&lt;p&gt;If the buffer is running empty, the system can slightly slow down playback for a few milliseconds. That buys time for the next packet to arrive. If the buffer is getting too full, it can slightly speed playback up.&lt;/p&gt;

&lt;p&gt;The important part is that good algorithms try to do this without making your voice sound like a cartoon. Pitch stays natural. Timing flexes a little. The listener hears continuity instead of a hard glitch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Good network:
  shrink buffer -&amp;gt; lower delay -&amp;gt; faster AI turn start

Bad network:
  grow buffer -&amp;gt; hide jitter -&amp;gt; protect VAD from fake silence

Buffer almost empty:
  stretch time a little -&amp;gt; wait for next packet

Buffer too full:
  compress time a little -&amp;gt; catch up without a hard jump
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tug-of-war never fully ends. Every live call is a negotiation between latency and stability.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why VAD makes the stakes higher
&lt;/h2&gt;

&lt;p&gt;In text chat, a late token is annoying. In voice, a late packet can change who is allowed to speak.&lt;/p&gt;

&lt;p&gt;Voice Activity Detection is the component that guesses whether the human is still talking. Many agent systems use VAD to decide when to stop listening and start answering.&lt;/p&gt;

&lt;p&gt;That decision is brittle when audio is broken.&lt;/p&gt;

&lt;p&gt;A dropped packet can look like silence. A burst of jitter can look like the person paused. If your buffer is too aggressive, VAD fires early. The LLM starts generating. TTS starts speaking. Now the human and the bot are talking over each other.&lt;/p&gt;

&lt;p&gt;Once that happens, recovery is hard. The caller feels interrupted. The transcript may be incomplete. The model answers a half-finished question. The product feels “dumb,” even if the model itself is strong.&lt;/p&gt;

&lt;p&gt;So when people say “our voice agent interrupts users,” the root cause is not always the prompt. Often the root cause is audio timing at the edge of the pipeline.&lt;/p&gt;




&lt;h2&gt;
  
  
  The full latency budget, not just the buffer
&lt;/h2&gt;

&lt;p&gt;It helps to look at the whole chain, because the jitter buffer never works alone.&lt;/p&gt;

&lt;p&gt;A rough mental model:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stage&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;th&gt;Why it hurts if it grows&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Jitter buffer&lt;/td&gt;
&lt;td&gt;Smooths packet arrival&lt;/td&gt;
&lt;td&gt;Makes the agent start late&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VAD&lt;/td&gt;
&lt;td&gt;Detects end of speech&lt;/td&gt;
&lt;td&gt;Wrong guess causes interruptions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;STT&lt;/td&gt;
&lt;td&gt;Turns audio into text&lt;/td&gt;
&lt;td&gt;Slow STT adds dead air&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LLM&lt;/td&gt;
&lt;td&gt;Plans the reply&lt;/td&gt;
&lt;td&gt;Long reasoning feels like silence&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TTS&lt;/td&gt;
&lt;td&gt;Turns text into voice&lt;/td&gt;
&lt;td&gt;Slow first audio makes replies feel heavy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Playback&lt;/td&gt;
&lt;td&gt;Streams speech back&lt;/td&gt;
&lt;td&gt;Extra buffering on the way out also adds delay&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If each stage “only” adds a little delay, the total still becomes a conversation killer. That is why production voice teams obsess over the first audible response, not only the final answer quality.&lt;/p&gt;

&lt;p&gt;A beautiful reply that arrives after two awkward seconds still feels broken.&lt;/p&gt;




&lt;h2&gt;
  
  
  What “good” looks like in production
&lt;/h2&gt;

&lt;p&gt;A strong voice pipeline does not chase zero jitter buffer. Zero is a fantasy on the public internet.&lt;/p&gt;

&lt;p&gt;Good systems chase a moving target:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep buffer small when the network is stable.&lt;/li&gt;
&lt;li&gt;Grow only when measurement says growth is needed.&lt;/li&gt;
&lt;li&gt;Use sequence gaps and timestamp drift as early warning signs, not only “audio sounds bad.”&lt;/li&gt;
&lt;li&gt;Protect VAD from fake end-of-speech caused by drops.&lt;/li&gt;
&lt;li&gt;Start STT and model work as early as safely possible.&lt;/li&gt;
&lt;li&gt;Stream TTS output instead of waiting for the full sentence.&lt;/li&gt;
&lt;li&gt;Measure interruption rate, silence gaps, and time-to-first-audio as product metrics, not only as network metrics.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, the buffer is not a pure networking detail. It is part of conversation design.&lt;/p&gt;

&lt;p&gt;If you are building an AI calling product, these questions matter as much as model choice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How often does the agent cut the user off?&lt;/li&gt;
&lt;li&gt;How often does the user talk over the agent?&lt;/li&gt;
&lt;li&gt;How long is the quiet gap before the agent starts speaking?&lt;/li&gt;
&lt;li&gt;How does that gap change on mobile networks versus office Wi-Fi?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those numbers tell you whether your jitter strategy is working.&lt;/p&gt;




&lt;h2&gt;
  
  
  A simple way to remember the whole idea
&lt;/h2&gt;

&lt;p&gt;Here is the whole story in one short chain.&lt;/p&gt;

&lt;p&gt;The internet ships voice as tiny UDP packets wrapped in RTP. Each RTP packet carries labels like sequence number, timestamp, payload type, and SSRC. Those packets do not arrive smoothly. Jitter is that uneven arrival. A jitter buffer uses sequence numbers to reorder packets and timestamps to place them on the media timeline. A buffer that is too large makes the AI slow. A buffer that is too small makes the AI interrupt people. Adaptive buffers watch the network and resize themselves. Advanced buffers also stretch or squeeze time a little to keep speech continuous. The hard part is not making audio “kind of work.” The hard part is protecting natural turn-taking while staying fast enough that the conversation still feels human.&lt;/p&gt;

&lt;p&gt;That tug-of-war between fighting latency and preventing VAD misfires is the core engineering challenge of real-time conversational AI.&lt;/p&gt;




&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;Models get the headlines. Jitter buffers do not.&lt;/p&gt;

&lt;p&gt;But if you are building voice agents seriously, this is one of the places where product quality is won or lost. A clever prompt cannot fix a pipeline that keeps handing the model broken silence. A bigger model cannot hide a two-second pause that makes every reply feel late.&lt;/p&gt;

&lt;p&gt;If you want AI voice to feel natural, you have to respect the physics of the network, the psychology of conversation, and the engineering trade-offs in between.&lt;/p&gt;

&lt;p&gt;The waiting room has to be smart.&lt;/p&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://webrtc.googlesource.com/src/+/refs/heads/main/modules/audio_coding/neteq" rel="noopener noreferrer"&gt;WebRTC NetEQ overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API" rel="noopener noreferrer"&gt;MDN WebRTC API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://datatracker.ietf.org/doc/html/rfc3550" rel="noopener noreferrer"&gt;RFC 3550 — RTP: A Transport Protocol for Real-Time Applications&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>webrtc</category>
      <category>networking</category>
      <category>voiceai</category>
    </item>
  </channel>
</rss>
