G.711 or Opus: choosing a codec for a voice agent SIP trunk
When to prefer G.711 or Opus on a voice agent's SIP trunk, how to set the codec order on your PBX, and a short test to run before going live.
For most voice agents on a SIP trunk, offer both codecs and let the call settle on one without transcoding: put G.711 first when the agent platform sits on a stable, well-provisioned link and the call ends on an ordinary phone, and prefer Opus when the leg between your platform or PBX and the trunk runs over a link with limited bandwidth or occasional packet loss. SIP.cr trunks support G.711 and Opus, so the decision is about your side of the call: what your platform offers, in which order, and whether anything in the middle converts audio from one codec to another.
This guide explains the trade-offs in plain terms, shows how to set the codec order on Asterisk and FreeSWITCH, and gives a short test you can run before going live.
What each codec does
| G.711 | Opus |
|---|---|
| Standard | ITU-T G.711 |
| Audio band | Narrowband (8 kHz sampling) |
| Bit rate | Fixed 64 kbit/s of audio |
| CPU load | Very low |
| Behaviour under packet loss | Relies on the receiver's concealment |
| Names in SDP | PCMU/8000 (μ-law), PCMA/8000 (A-law) |
Two facts matter more than the table for a voice agent:
- The public phone network is narrowband. When your agent calls a mobile or a landline, or a customer calls your Costa Rica number, the audio on the phone network side is telephone-quality. Opus cannot add bandwidth that the far end never had. Its advantage is on the IP leg between your agent platform or PBX and the trunk.
- Every conversion loses something. Each time audio is decoded and re-encoded into another codec, you add a little delay and lose a little quality. For a voice agent, delay hurts turn-taking, and lost detail hurts speech recognition. A call that stays in one codec end to end is usually the cleanest.
When G.711 is the better first choice
- Your platform or PBX runs in a data center or cloud region with plenty of bandwidth and little packet loss.
- Most of your calls go to or come from ordinary phones, so the audio is narrowband anyway.
- Your PBX handles many simultaneous calls and you want to keep CPU low.
- You want the simplest possible setup: G.711 is supported almost everywhere, and there are few settings to get wrong.
Plan bandwidth with headers included. G.711 at the common 20 ms packet size uses roughly 80 kbit/s per direction at the IP layer, a bit more on Ethernet. Multiply by your channel count to size the link.
When Opus is the better first choice
- The leg to the trunk runs over a congested office connection, a branch link or a path with occasional packet loss.
- You need to keep bandwidth per call low because many calls share the same link.
- Your agent platform already works natively in Opus, so offering it first avoids a conversion on your side.
Opus can recover from some lost packets with forward error correction, but only when the sender enables it and the receiver uses it; check the Opus documentation and your platform's settings. If jitter and loss are high every day, fix the network as well; a codec choice will not hide a bad link for long.
How codec negotiation works
SIP uses an offer and an answer, described in RFC 3264. The side that starts the call lists the codecs it accepts in order of preference; the other side answers with the ones it accepts from that list. In practice:
- Order matters. Put the codec you want first. If both ends accept it, it usually wins.
- Keep the list short. Offer G.711 and Opus, and remove codecs neither side will use. A long list makes failures harder to read.
- Match both directions. Inbound and outbound calls can be negotiated by different sides, so set your preference on both your inbound and outbound profiles.
- Keep DTMF working. If your agent or IVR reads keypresses, keep the
telephone-eventpayload (RFC 4733) in your offer and confirm in your platform's documentation how it handles keypresses.
For hosted agent platforms, the codec list may be managed on the vendor's side. Read their SIP documentation before changing anything: ElevenLabs, Vapi, Retell AI and Synthflow.
Setting the codec order on a PBX
On Asterisk with PJSIP, the order comes from the allow line of the endpoint that faces the trunk. Opus needs the Opus codec module to be loaded; check the Asterisk documentation for your version.
[sipcr-endpoint]
type=endpoint
disallow=all
allow=ulaw,alaw,opus
To prefer Opus instead, change the line to allow=opus,ulaw,alaw.
On FreeSWITCH, the preference lists usually live in vars.xml and are used by the SIP profiles. See the FreeSWITCH documentation for where your profiles read them.
<X-PRE-PROCESS cmd="set" data="global_codec_prefs=PCMU,PCMA,OPUS"/>
<X-PRE-PROCESS cmd="set" data="outbound_codec_prefs=PCMU,PCMA,OPUS"/>
Reload the configuration and make a test call after each change. The trunk values themselves (address, username, password) are shown on the SIP.cr trunk page under the names each platform uses.
A ten-minute test before going live
- Set G.711 first and make five inbound and five outbound test calls of at least one minute each, from the same network the agent will use in production.
- Switch to Opus first and repeat the same calls.
- Open each call in the SIP.cr portal and compare MOS, jitter and packet loss on the call record.
- Listen to the agent's transcripts for the same calls. Count misheard words, especially numbers, names and ID digits.
- Check the PBX or platform logs to confirm which codec was actually negotiated; a preference that never takes effect is a common surprise.
- Keep the order that gives the better transcripts with stable quality figures, and write it down with the date of the test.
As a rule of thumb, MOS around 4 or higher, jitter under about 30 ms and packet loss under 1% are generally considered good. These are industry references, not limits set by SIP.cr. If one codec scores clearly better only at certain hours, the cause is more likely the network than the codec.
Common mistakes
- Offering only Opus. If something along the path does not accept it, the call fails at setup. Keep G.711 in the list as a fallback.
- Transcoding twice. A PBX that converts Opus from the agent platform to G.711, while the platform could send G.711 directly, adds delay for no gain.
- Changing the codec to fix a NAT problem. One-way or missing audio is almost never a codec issue. For a PBX behind NAT, use registration, which SIP.cr trunks support, and review the firewall rules for the RTP port range.
- Judging by ear on one call. Use the quality figures from several calls and the transcripts, not a single impression.
For the trunk settings that go with your PBX, see the guide at /sip-trunk/pbx.