Skip to content
mentoor

Topic 1 of 5 · 4 min read

Connecting voice agents to phone networks

Goal: Wire a voice agent to an inbound phone number and identify the configuration errors that break audio quality or call transfer.

A phone agent is your normal voice agent with a phone network bolted on the front, and most failures come from the seam: the audio format, the transfer method and the webhook.

Check yourself · 1 of 3

A Twilio Media Streams WebSocket sends your server audio from a phone call. In which format does it arrive?

Choose one answer

Why it matters

Phone calls are not browser audio. A caller on a mobile network sends narrow-band audio, a carrier hands your provider the call, and your provider hands it to your code. Each step has its own format and its own failure. A model tuned on clean 16 kHz microphone audio will mishear a phone line unless you plan for it.

The path of a call

  1. A phone number is rented from a provider such as Twilio or Telnyx, or from LiveKit. The number points at a webhook or a dispatch rule.
  2. A SIP trunk is the carrier link that carries calls over the internet using SIP, the Session Initiation Protocol. Inbound trunks accept calls; outbound trunks place them. If you have your own phone system, you point its trunk at the provider and allow only the provider's addresses.
  3. Your agent receives the audio. With Twilio Media Streams your server opens a WebSocket and receives base64 audio chunks in 8 kHz mu-law, the G.711 format phones use. With ConversationRelay, Twilio runs speech recognition and speech synthesis and your server exchanges plain text. With LiveKit telephony, the call joins a room and your agent joins the same room like any other participant.
  4. Keypad input arrives as DTMF tones. Treat them as a separate event, not as speech. Use them for account numbers, PINs and "press 1 for a person".
  5. Transfer moves the caller elsewhere. A cold transfer hands the call over and leaves. A warm transfer keeps the agent on the line until a person answers.

Before and after

A team streamed Twilio audio into a model that expects 16-bit PCM at 16 kHz. The agent heard static and answered nonsense.

# Before: mu-law bytes fed straight to the recogniser
await stt.send(base64.b64decode(msg["media"]["payload"]))
# After: decode mu-law, then resample 8 kHz to the 16 kHz the model expects
# audioop left the standard library in Python 3.13; install audioop-lts there
pcm8k = audioop.ulaw2lin(base64.b64decode(msg["media"]["payload"]), 2)
pcm16k, _ = audioop.ratecv(pcm8k, 2, 1, 8000, 16000, None)
await stt.send(pcm16k)

Resampling to 16 kHz does not add detail the phone line never carried. Pick a speech model that handles telephone audio, and test with real calls.

What goes wrong

  • Wrong audio format. Mu-law treated as PCM, or the wrong sample rate on the way out, gives static or chipmunk speech.
  • A webhook that is not public or not verified. The provider cannot reach localhost, and an unverified webhook lets anyone post fake calls. Validate the provider's signature header.
  • Blind transfers. Transferring to a number that is closed or busy leaves the caller in silence. Check the transfer result and have a fallback.
  • No barge-in handling. Callers interrupt. If the agent keeps talking, clear the outgoing audio buffer when speech starts.
  • Ignoring latency. Every hop adds delay, and a pause of a second on a phone call feels like a dropped line.

Examples

Inbound number to a LiveKit agent

{
  "rule": {
    "dispatchRuleIndividual": { "roomPrefix": "call-" }
  },
  "trunk_ids": ["ST_support_in"],
  "name": "support-line"
}

Each caller gets a private room named call-.... The agent worker joins that room, and the trunk only accepts calls to the support number. If the dispatch rule is missing, the call connects and nobody answers.

Twilio stream that sounds like static

Symptom: the agent replies to every call with "sorry, I did not catch that". Log check: the first media frame has encoding: audio/x-mulaw and the recogniser is configured for linear16 at 16000. Fix: decode the mu-law, resample, and set the output encoding back to mu-law 8000 before sending audio to the caller.

Key points

  • A call reaches your agent through a number, a SIP trunk and a provider bridge, and each hop has its own format and failure mode.
  • Twilio Media Streams delivers 8 kHz mu-law audio, so it must be decoded and resampled before a model that expects PCM can use it.
  • ConversationRelay and LiveKit telephony remove most audio plumbing, at the cost of depending on the platform's speech and routing choices.
  • Keypad tones (DTMF) are events, not speech, and are the reliable way to collect digits.
  • A warm transfer needs a checked result and a fallback, otherwise the caller is dropped into silence.

Common mistakes

  • Feeding mu-law bytes to a speech model that expects linear PCM at a higher rate.
  • Skipping webhook signature validation because it works in testing.
  • Transferring a call without checking whether the target answered.
  • Treating keypad digits as spoken words and letting the recogniser guess them.

Check yourself · 2 of 3

A caller types their 10-digit account number on the keypad. How should the agent read it?

Choose one answer

Check yourself · 3 of 3

Callers hear silence for several seconds after a warm transfer to the sales line outside opening hours. What is the best fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic

Where to go in your country