Need request/response schemas? Pair this overview with the REST API, WebSocket, and SIP configuration references.
System landscape
Traffic entry points
WebSocket (/ws) – The main control plane. Clients connect once, send a config payload (providers + LiveKit settings), and then stream audio, emit speak commands, or relay LiveKit data with sub-second latency.
REST endpoints – Provide complementary one-off operations:
/voiceslists provider catalogs for UI pickers./speakperforms one-shot synthesis when a persistent socket is overkill./livekit/tokenmints participant tokens so browser/mobile clients can join the same LiveKit room that Sayna already inhabits.
sip block is populated, Sayna exposes a SIP domain/IP. Carriers such as Twilio point their Origination URI at that address (e.g., sip:sip.sayna.ai;transport=tcp). Sayna enforces room_prefix constraints, filters source IPs, and forwards LiveKit webhook payloads to the hosts defined in SIP_HOOKS_JSON.
Voice orchestration pipeline
- Configuration – Sayna validates the
configmessage, injects server-side API keys (or uses client-supplied credentials when provided via theauthfield), initializes STT/TTS providers, loads DSP assets (turn detection, noise filtering), and emitsreadyonce everything is live. - Streaming – Binary frames flow through the STT connector;
stt_resultevents stream back withis_finalandis_speech_finalhints so you know when to respond. - Synthesis –
speakcommands enqueue TTS jobs. Audio streams back as binary frames and, when LiveKit is enabled, mirrors into the room for human listeners. - Caching – TTS outputs are hashed by text + config and stored under
CACHE_PATH, so repeated prompts replay instantly. - Error handling – Most faults surface as JSON
errorevents while keeping the socket open, giving clients room to retry.
LiveKit & SIP interplay
- Sayna runs its own LiveKit participant (identity defaults to
sayna-ai). Other clients call/livekit/tokento get their own access tokens; Sayna never shares its agent keys. - When
enable_recording=true, Sayna asks LiveKit to start composite recordings to the S3 target you configured. - SIP mode auto-provisions the LiveKit SIP trunk + dispatch rule (
sayna-{room_prefix}-trunk/dispatch). You only configure the carrier; Sayna takes care of LiveKit. - The SIP dispatcher reads
sip.h.toheaders from LiveKit webhook events and forwards the raw JSON payload to whichever hook hostname matches—perfect for per-domain routing or downstream analytics.
Component responsibilities
Common deployment patterns
Operational checklist
- Dependencies – Outbound HTTPS to providers + LiveKit; inbound TCP from carrier IPs when SIP is enabled.
- Scaling –
/wssessions are stateful. Run multiple Sayna instances behind a load balancer (with sticky sessions if the proxy terminates WebSockets). - Observability – Monitor
readyvserrorrates, STT latency, cache hit ratios, SIP provisioning logs, and webhook forwarding success. - Security – Use
AUTH_REQUIRED=trueplus API secrets or delegated JWT to protect REST + WebSocket traffic, and keep SIP hooks HTTPS-only.