Skip to main content

Twilio + LiveKit SIP Telephony

ADR: ADR-0050 (kept in the project's private repository) Date: 2026-05-02 Status as last verified (2026-05-27): Phase A operational on the development stack; later phases pending. Network addresses, credentials, certificates and runbooks are deliberately not published on this page.

Architecture​

The self-hosted telephony stack connects Belgian phone callers to the voice agent without a managed audio gateway. Twilio's role is narrow: own the phone number, forward the call over SIP, and handle emergency routing per the requirements of BIPT (the Belgian Institute for Postal services and Telecommunications). (RFC 3261) (ITU-T E.164) Everything else runs on infrastructure operated by the project.

ComponentRole
Media server (LiveKit)WebRTC media relay; rooms and participants (@livekit_agents_docs)
SIP gateway (LiveKit SIP)Translates SIP and the phone network to and from a media-server participant
Voice agentDeepgram STT (@deepgram_nova3), a connection to the backend, ElevenLabs TTS (@elevenlabs_multilingual_v2)
RedisShared state between the media server and the SIP gateway (required), plus per-caller rate-limit counters

Architectural trade-offs​

Three deployment-shape decisions define the telephony stack; each was considered against alternatives at ADR-0050 time.

DecisionChosenAlternatives consideredRejected because
SIP gateway hostingSelf-hosted gateway on the pilot serverLiveKit Cloud managed SIP; Twilio ConversationRelay; Twilio voice with no LiveKitThe managed gateway was priced per participant-hour, which at the brief's projected query volume would have been a recurring cost (vendor pricing at decision time, not re-checked). ConversationRelay ties cognition to Twilio's call-flow control. Self-hosting has no marginal gateway fee and keeps caller audio on the project's server, apart from the outbound calls to STT and TTS vendors already in ADR-0048 scope.
Phone-network providerTwilio SIP trunkBICS, Voxbone, a Belgian incumbent; direct termination to Belgian carriersTwilio's Belgian coverage, emergency routing and address allowlisting are mature; alternative providers would have required hand-built emergency-routing compliance.
TLS terminationPilot server with a publicly trusted certificate (Phase B)LiveKit Cloud edge TLS; CDN passthroughEdge TLS implies the managed gateway (rejected above); a CDN does not terminate SIP. Terminating on the pilot server keeps the trust boundary on infrastructure the project operates.

Per-call sequence​

Operator handoff​

When the agent's helpdesk-transfer tool fires, the voice agent asks the telephony layer to transfer the caller to the hospital helpdesk (+32 89 80 80 80, a public number). A caller saying "ik wil met een mens praten" (I want to speak to a person) is routed to the helpdesk without dropping the call. Two paths lead there: the rule-based pre-filter recognises a handoff request, or the GPT-4.1 agent picks the transfer tool; either way the orchestrator returns the escalate intent and the voice agent issues the transfer.

Configuration​

The SIP gateway is configured at two layers: a media-server configuration (shared by development and pilot) and a gateway configuration, with one variant for development (unencrypted, local only) and one for the pilot (TLS with a publicly trusted certificate). Both processes must point to the same Redis instance, because SIP state (inbound trunks, dispatch rules) lives in Redis and is read by both.

The inbound trunk and dispatch rule are runtime state, not configuration files. They are created with the LiveKit command-line tool and stored in Redis, so wiping the Redis volume requires recreating them.

Phased rollout​

PhaseScopeStatus (as of 2026-05-27)
ASIP gateway in the development stack, unencrypted; a local softphone call reaches the voice agentDone (local)
BPilot server: public DNS, publicly trusted SIP certificate, firewall rules for signalling and mediaPending
CTwilio SIP trunk pointed at the pilot gateway, with an address allowlistPending
DThe pilot phone number forwarded to the trunk; a call from any mobile reaches the voice agentPending
EOperator handoff to the helpdesk when the intent is escalatePending
FPer-caller rate limit plus monitoring and a voicemail fallback; one week of sustainable trafficPending

Phase A lessons​

Three things surfaced during local testing:

  • Version mismatch between the media server and its command-line tool produced a "missing rule" error when creating dispatch rules; fixed by upgrading the media server.
  • The media-server configuration must include a Redis section. Without it, creating an inbound trunk fails with "sip not connected (redis required)" even when the gateway is healthy.
  • An inbound trunk requires at least one security gate (credentials, an address allowlist or a number filter). The tool refuses to create an unrestricted trunk, which is a useful guardrail rather than a bug.

Phase B — what remains for the pilot deploy​

Public DNS for the SIP endpoint, a publicly trusted certificate, firewall rules for signalling and media, a Twilio trunk with an address allowlist, and forwarding of the pilot number to that trunk. The detailed runbook is internal and not published.

Per-caller rate limit and concurrency​

Two limiters cap inbound traffic before any LLM cost is incurred:

LimiterScopeEnforcement
ConcurrencyTotal simultaneous callsIn-process counter; trips on a configured number of concurrent admissions
RatePer-caller calls per hourRedis counter keyed on a hashed caller ID

The limit is 10 calls per hour per caller number, using the same pattern as the public web-socket rate limiter. Caller IDs are pseudonymised before logging, and the hash (not the raw number) is used for rate limiting. HIPAA Safe Harbor

Belgian regulatory notes​

  • 112 emergency routing: must honour call transfer per BIPT requirements. Twilio handles the phone-network side; the SIP server honours transfers for emergency calls.
  • Call recording consent: the voice channel is designed record-free. No audio is stored. STT transcripts are stored (conversation history) and subject to the GDPR retention policy.
  • PII redaction: caller-ID numbers are pseudonymised before any logging or rate-limit writes, in line with the "minimum necessary data" GDPR principle.

References​