<?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: Bilal Ahmed</title>
    <description>The latest articles on DEV Community by Bilal Ahmed (@bilal_ahmed_3840).</description>
    <link>https://dev.to/bilal_ahmed_3840</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%2F4223580%2Fbbda8542-04f9-43b5-b5eb-7a964a8c2431.png</url>
      <title>DEV Community: Bilal Ahmed</title>
      <link>https://dev.to/bilal_ahmed_3840</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bilal_ahmed_3840"/>
    <language>en</language>
    <item>
      <title>Your LiveKit agent works great on WebRTC. The phone network will humble it</title>
      <dc:creator>Bilal Ahmed</dc:creator>
      <pubDate>Wed, 04 Nov 2026 11:16:04 +0000</pubDate>
      <link>https://dev.to/bilal_ahmed_3840/your-livekit-agent-works-great-on-webrtc-the-phone-network-will-humble-it-kd0</link>
      <guid>https://dev.to/bilal_ahmed_3840/your-livekit-agent-works-great-on-webrtc-the-phone-network-will-humble-it-kd0</guid>
      <description>&lt;p&gt;A voice agent that works perfectly in your browser can fall apart the moment someone calls it from a phone. Same model, same prompt, same code. Different network.&lt;/p&gt;

&lt;p&gt;I run a real-time voice platform that handles around 40,000 calls a day, almost all of them over the PSTN. This is the stuff I wish someone had written down before I started.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, the path a call actually takes
&lt;/h2&gt;

&lt;p&gt;Most people picture one hop. There are five.&lt;/p&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%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft9h9fwrmzijnymn2qvax.png" 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%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft9h9fwrmzijnymn2qvax.png" alt="Inbound SIP call flow into a LiveKit agent" width="800" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Caller dials your DID. It lands at your SIP provider.&lt;/li&gt;
&lt;li&gt;The provider forwards the INVITE to the LiveKit SIP service.&lt;/li&gt;
&lt;li&gt;SIP checks it against your &lt;strong&gt;inbound trunk&lt;/strong&gt;: IP allowlist, allowed numbers, auth.&lt;/li&gt;
&lt;li&gt;It matches the call to a &lt;strong&gt;dispatch rule&lt;/strong&gt;, which decides which room this caller belongs in.&lt;/li&gt;
&lt;li&gt;LiveKit server creates the room and a SIP participant. Your agent is already there, or gets dispatched now.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Setting that up is two API calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;livekit&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;

&lt;span class="n"&gt;lkapi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LiveKitAPI&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# 1. who is allowed to send us calls
&lt;/span&gt;&lt;span class="n"&gt;trunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;lkapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_sip_inbound_trunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CreateSIPInboundTrunkRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;trunk&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SIPInboundTrunkInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;carrier-primary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;+14155550123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="n"&gt;allowed_addresses&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;203.0.113.10/32&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;   &lt;span class="c1"&gt;# your carrier's signalling IP
&lt;/span&gt;            &lt;span class="n"&gt;krisp_enabled&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&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="c1"&gt;# 2. where those calls go
&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;lkapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_sip_dispatch_rule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CreateSIPDispatchRuleRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;trunk_ids&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;trunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sip_trunk_id&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="n"&gt;rule&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SIPDispatchRule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;dispatch_rule_individual&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SIPDispatchRuleIndividual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;room_prefix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;call&lt;/span&gt;&lt;span class="sh"&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;room_config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RoomConfiguration&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;agents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RoomAgentDispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;agent_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;support-agent&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tenant&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;acme&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;}&lt;/span&gt;&lt;span class="sh"&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;That works. Now here is what broke.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Your audio is 8 kHz, no matter what your STT can do
&lt;/h2&gt;

&lt;p&gt;This one costs people the most and gets noticed the least.&lt;/p&gt;

&lt;p&gt;Inside a LiveKit room, audio is Opus at 48 kHz. The PSTN is not. Your carrier almost certainly hands you &lt;strong&gt;G.711&lt;/strong&gt; (PCMU in North America, PCMA most other places): 8 kHz sampling, roughly 300 Hz to 3400 Hz of usable bandwidth, unchanged since the 1970s. The SIP bridge transcodes between the two, which is why its build needs libopus and libsoxr.&lt;/p&gt;

&lt;p&gt;So your STT never sees the audio you tested it on. It sees a resampled narrowband signal with the top of every fricative shaved off. "S" and "F" start blurring. Digits get worse. Accented speech gets much worse.&lt;/p&gt;

&lt;p&gt;Two things worth doing:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benchmark on real call audio.&lt;/strong&gt; Not clean 16 kHz samples from your laptop mic. Record actual calls, run your candidate STT models against those, and compare. The gap between your clean-audio WER and your narrowband WER is the number that matters, and it is usually wide enough to change which model you pick. Vendor benchmarks will not tell you this, because nobody benchmarks on 8 kHz.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ask your carrier for G.722.&lt;/strong&gt; It is 16 kHz and costs nothing extra on most trunks. LiveKit will negotiate it if both sides offer it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;media&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SIPMediaConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;codecs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SIPCodec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;G722&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SIPCodec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PCMU&lt;/span&gt;&lt;span class="sh"&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;Supported names are &lt;code&gt;PCMU&lt;/code&gt;, &lt;code&gt;PCMA&lt;/code&gt;, &lt;code&gt;G722&lt;/code&gt; and &lt;code&gt;AMR-WB&lt;/code&gt;. There is also &lt;code&gt;only_listed_codecs&lt;/code&gt;, which forces your list instead of adding to the defaults. Be careful with it. If the carrier does not support anything in your list, the call just fails.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Your dispatch rule is a concurrency decision in disguise
&lt;/h2&gt;

&lt;p&gt;There are three rule types and the names undersell how different they are.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Rule&lt;/th&gt;
&lt;th&gt;Room behaviour&lt;/th&gt;
&lt;th&gt;Use it for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;dispatch_rule_individual&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;New room per caller, named from their number plus a random suffix&lt;/td&gt;
&lt;td&gt;Almost every agent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;dispatch_rule_direct&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Every caller joins the same room&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Conferences, not agents&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;dispatch_rule_callee&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Room named after the number that was dialed&lt;/td&gt;
&lt;td&gt;Multi-tenant, one DID per client&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;dispatch_rule_direct&lt;/code&gt; is the first example in a lot of tutorials and it will silently put caller two into caller one's conversation. If you take one thing from this post, take that.&lt;/p&gt;

&lt;p&gt;The matching trap is &lt;code&gt;no_randomness&lt;/code&gt; on the individual rule. Set it to &lt;code&gt;true&lt;/code&gt; and callers with the same number share a room. That sounds like a nice "resume my call" feature until two people from the same office switchboard land in the same session.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The latency you measured in your browser is not your latency
&lt;/h2&gt;

&lt;p&gt;Your browser demo has one hop to LiveKit. A phone call has the carrier network, possibly an SBC, the SIP service, and a transcode step before a single sample reaches your agent.&lt;/p&gt;

&lt;p&gt;Measure the phone path separately. Treat your browser number as a floor you will never hit in production, not as a baseline.&lt;/p&gt;

&lt;p&gt;Whatever that SIP leg costs you, it is spent before your agent pipeline starts, and no amount of prompt tuning or model swapping gets it back. Measure it once on your own trunk and carry it as fixed overhead in your budget. Discovering it later, when a client asks why the agent feels slow, is a worse way to find out.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. DTMF is not a keypress event
&lt;/h2&gt;

&lt;p&gt;People press 1 for sales. They will do it to your agent too, usually while it is mid-sentence.&lt;/p&gt;

&lt;p&gt;Two things to know. The &lt;code&gt;dtmf&lt;/code&gt; field on &lt;code&gt;CreateSIPParticipant&lt;/code&gt; is for &lt;strong&gt;dialing&lt;/strong&gt; digits on an outbound call, for navigating someone else's IVR. The &lt;code&gt;w&lt;/code&gt; character inserts a half-second pause. It is not for receiving digits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;lkapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_sip_participant&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CreateSIPParticipantRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;sip_trunk_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;trunk_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;sip_call_to&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;+14155550199&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;room_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;outbound-42&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;dtmf&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ww3001#&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="c1"&gt;# wait, wait, extension 3001
&lt;/span&gt;        &lt;span class="n"&gt;wait_until_answered&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&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;For inbound digits you subscribe to DTMF events on the SIP participant. And whichever way you handle it, remember carriers send DTMF as RFC 2833 telephone-events, inband tones, or SIP INFO, depending on the carrier and the day. Test yours specifically. Do not assume.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Transfer is a one-way door
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;TransferSIPParticipant&lt;/code&gt; moves a caller to a number or SIP URI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;lkapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transfer_sip_participant&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TransferSIPParticipantRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;room_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;room&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;participant_identity&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;caller_identity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;transfer_to&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tel:+14155550177&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;play_dialtone&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;ringing_timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;30&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 details that matter in production. &lt;code&gt;play_dialtone&lt;/code&gt; defaults to off, which means the caller hears your room's audio during the transfer, and if nobody is speaking that is pure silence. People hang up on silence. Turn it on or play something.&lt;/p&gt;

&lt;p&gt;And if &lt;code&gt;ringing_timeout&lt;/code&gt; expires, the transfer errors and the caller stays in the room with your agent. Your agent has to notice and say something. If it does not, the caller is sitting there with a bot that thinks the conversation is over.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. The context you need is in the SIP headers
&lt;/h2&gt;

&lt;p&gt;Carriers put real information in custom headers. Account IDs from your IVR, campaign tags, the original dialed number before forwarding. By default none of it reaches your agent.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;include_headers&lt;/code&gt; on the trunk pulls them into participant attributes. Separately, dispatch rules carry &lt;code&gt;attributes&lt;/code&gt; and &lt;code&gt;metadata&lt;/code&gt; down to every SIP participant they create, and &lt;code&gt;room_config.agents[].metadata&lt;/code&gt; lands in your agent's job metadata.&lt;/p&gt;

&lt;p&gt;That is how you do multi-tenant routing without a database lookup on the critical path. Tenant comes in on the dispatch rule, your agent reads it at startup, done.&lt;/p&gt;

&lt;h2&gt;
  
  
  A short pre-launch checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Test with a real phone on a real carrier, not a softphone on your LAN&lt;/li&gt;
&lt;li&gt;Confirm which codec you actually negotiated, do not assume&lt;/li&gt;
&lt;li&gt;Benchmark STT on recorded call audio&lt;/li&gt;
&lt;li&gt;Verify your dispatch rule does not share rooms&lt;/li&gt;
&lt;li&gt;Press DTMF digits mid-sentence and see what happens&lt;/li&gt;
&lt;li&gt;Let a transfer time out on purpose and listen to what the caller hears&lt;/li&gt;
&lt;li&gt;Call from a mobile on a weak signal, the thing your demo never does&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of this is exotic. It is just the gap between WebRTC, where you control both ends, and the phone network, where you control one.&lt;/p&gt;

&lt;p&gt;If you are running agents on the PSTN, I would like to hear which of these bit you. My guess is the codec one.&lt;/p&gt;

</description>
      <category>livekit</category>
      <category>webrtc</category>
      <category>voice</category>
      <category>ai</category>
    </item>
    <item>
      <title>My cleanup code was cancelling itself (an asyncio gotcha)</title>
      <dc:creator>Bilal Ahmed</dc:creator>
      <pubDate>Tue, 03 Nov 2026 06:40:42 +0000</pubDate>
      <link>https://dev.to/bilal_ahmed_3840/my-cleanup-code-was-cancelling-itself-an-asyncio-gotcha-273d</link>
      <guid>https://dev.to/bilal_ahmed_3840/my-cleanup-code-was-cancelling-itself-an-asyncio-gotcha-273d</guid>
      <description>&lt;p&gt;Every call that ended in silence was vanishing.&lt;/p&gt;

&lt;p&gt;No transcript. No recording. And the session sat in our dashboard marked &lt;strong&gt;Active&lt;/strong&gt; forever, like a guest who missed every hint that the party was over.&lt;/p&gt;

&lt;p&gt;It was picky, too. Agent says goodbye? Saved. User hangs up? Saved. Caller goes quiet and the agent ends the call? Gone. 9 out of 9 in a day, while the other 24 calls were perfect.&lt;/p&gt;

&lt;p&gt;That's not flaky. That's a code path.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;I build real-time voice AI agents. When a caller goes quiet, the agent checks in ("Are you still there?") a few times, then ends the call. That runs as a background task:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_inactivity_loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&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;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_away_timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;say_check_in&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;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;check_interval&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;end_call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;presence_timeout&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CancelledError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;inactivity task cancelled&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every way of ending a call goes through one shared cleanup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;end_call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_perform_cleanup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_perform_cleanup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cleanup started&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_cancel_inactivity_task&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   &lt;span class="c1"&gt;# stop the silence timer
&lt;/span&gt;    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aclose&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save_call_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cleanup completed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_cancel_inactivity_task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_inactivity_task&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_inactivity_task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks responsible. Call's ending, stop the timer.&lt;/p&gt;

&lt;p&gt;Now ask one question: &lt;strong&gt;which task is running the cleanup when the call ends from silence?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What &lt;code&gt;cancel()&lt;/code&gt; actually does
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Task.cancel()&lt;/code&gt; doesn't stop a task on the spot. It sets a flag that says "next time you hand control back to the event loop, throw &lt;code&gt;CancelledError&lt;/code&gt; in instead."&lt;/p&gt;

&lt;p&gt;Normally you cancel a &lt;em&gt;different&lt;/em&gt; task, parked on some &lt;code&gt;await&lt;/code&gt;. But a task can cancel itself too. No error, no warning. It just keeps running until its next &lt;code&gt;await&lt;/code&gt;, and then the error lands.&lt;/p&gt;

&lt;p&gt;A delayed self-destruct, basically.&lt;/p&gt;

&lt;p&gt;So on the silence path:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The inactivity task calls &lt;code&gt;end_call()&lt;/code&gt;. Awaiting a coroutine doesn't create a new task, so we're still inside the inactivity task.&lt;/li&gt;
&lt;li&gt;Cleanup calls &lt;code&gt;.cancel()&lt;/code&gt; on the inactivity task. On itself.&lt;/li&gt;
&lt;li&gt;Next line, &lt;code&gt;await self.session.aclose()&lt;/code&gt;, suspends. &lt;code&gt;CancelledError&lt;/code&gt; arrives.&lt;/li&gt;
&lt;li&gt;It unwinds back to &lt;code&gt;_inactivity_loop&lt;/code&gt;, gets caught, and is logged at &lt;strong&gt;debug&lt;/strong&gt;. Which production doesn't print.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Call data never saved. The logs showed &lt;code&gt;Cleanup started&lt;/code&gt; and then nothing at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  30-line repro
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;guard&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;watchdog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cleanup_started&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;current_task&lt;/span&gt;&lt;span class="p"&gt;()):&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&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;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&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="c1"&gt;# any real await
&lt;/span&gt;        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;call_data_saved&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;watchdog&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="k"&gt;try&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;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CancelledError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cancelled&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;guard=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;guard=False:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;'cleanup_started'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'cancelled'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;guard=True:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;'cleanup_started'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'call_data_saved'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even &lt;code&gt;await asyncio.sleep(0)&lt;/code&gt; is enough. One yield and the save is gone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why goodbye and hang-up worked
&lt;/h2&gt;

&lt;p&gt;Their cleanup ran in a &lt;em&gt;different&lt;/em&gt; task. Goodbye runs in the agent's tool-call task, hang-up spawns its own. So the cancel hit the sleeping inactivity task, not themselves.&lt;/p&gt;

&lt;p&gt;Same method, same &lt;code&gt;.cancel()&lt;/code&gt;. Correct for two callers out of three. That's why reviewing the method alone would never catch it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_cancel_inactivity_task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_inactivity_task&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;current_task&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If cleanup runs inside that task, there's nothing to cancel. It finishes on its own.&lt;/p&gt;

&lt;p&gt;The cleaner long-term design: a watchdog shouldn't tear down the thing that owns it. Let it &lt;em&gt;signal&lt;/em&gt; (an &lt;code&gt;asyncio.Event&lt;/code&gt;, a queue), and have one owner end the call. And no, &lt;code&gt;asyncio.shield()&lt;/code&gt; doesn't help here. Your own &lt;code&gt;await&lt;/code&gt; still raises.&lt;/p&gt;

&lt;h2&gt;
  
  
  The real villain: swallowing &lt;code&gt;CancelledError&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The self-cancel caused it. The debug log hid it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CancelledError&lt;/code&gt; inherits from &lt;code&gt;BaseException&lt;/code&gt; on purpose, so &lt;code&gt;except Exception&lt;/code&gt; won't eat it. It's a control signal, not an error. Swallow it and callers think you stopped when you didn't, &lt;code&gt;asyncio.timeout()&lt;/code&gt; and &lt;code&gt;TaskGroup&lt;/code&gt; get confused, and real failures go silent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CancelledError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;inactivity task cancelled&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A cleanup that stops halfway is never a debug-level event.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why sessions stayed stuck forever
&lt;/h2&gt;

&lt;p&gt;Losing data is one bug. A session stuck &lt;em&gt;forever&lt;/em&gt; means every backstop failed too. Ours:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A fallback "session ended" report existed, but sat after the cancelled &lt;code&gt;await&lt;/code&gt;, and its API route wasn't deployed on dev anyway.&lt;/li&gt;
&lt;li&gt;LiveKit's &lt;code&gt;room_finished&lt;/code&gt; webhook would have closed it, but dev had no webhook configured.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;expires_at&lt;/code&gt; only filtered lists. It never changed the status.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your expiry doesn't change state, it isn't expiry. It's a rug.&lt;/p&gt;

&lt;h2&gt;
  
  
  One testing lesson
&lt;/h2&gt;

&lt;p&gt;My first test reproduced the &lt;em&gt;pattern&lt;/em&gt; and passed. But the bug lived in how the real class wired things together. The test that matters drives the real silence path with tiny timeouts and asserts on outcomes ("data saved, status ended"), not on "cleanup() was called". Cleanup &lt;em&gt;was&lt;/em&gt; called. It just never finished.&lt;/p&gt;

&lt;h2&gt;
  
  
  Grep your code for this today
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Every &lt;code&gt;.cancel()&lt;/code&gt; in shared cleanup: can the target be the caller?&lt;/li&gt;
&lt;li&gt;Every &lt;code&gt;except asyncio.CancelledError&lt;/code&gt;: does it re-raise?&lt;/li&gt;
&lt;li&gt;Background timers that call teardown: should they signal instead?&lt;/li&gt;
&lt;li&gt;Success logs: do they come &lt;em&gt;after&lt;/em&gt; the work?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fix was one &lt;code&gt;if&lt;/code&gt;. Finding it took a day, thanks to one debug log line.&lt;/p&gt;

</description>
      <category>python</category>
      <category>asyncio</category>
      <category>voiceai</category>
      <category>debugging</category>
    </item>
  </channel>
</rss>
