agent_id. The same agent runs unchanged whether you connect from your own server, a browser, or a phone call.
The connection lifecycle
Every transport talks to the same realtime endpoint and follows the same five steps:- Open the WebSocket (with your API key, or a browser token).
- Bind to your agent by sending one
session.updatewith itsagent_id. The stored prompt, voice, and tools load automatically, so you don’t resend them. - Wait for
session.ready— the signal the agent is live. - Stream microphone audio as
input.audioframes and play the agent’sreply.audioframes. - End cleanly with
session.endwhen the conversation is over.
agent_id and nothing else:
agent_id is mutually exclusive with inline session fields. When you bind to a stored agent, don’t also send system_prompt, greeting, tools, input, or output; those are rejected. To override config per session instead, send those fields inline and omit agent_id. See Inline configuration.Connect from a server or native app
For server-side apps, backends, and native desktop clients, connect directly with your API key in theAuthorization header (raw key; a Bearer prefix is also accepted). Set ASSEMBLYAI_API_KEY and AGENT_ID in your environment first.
Both clients below are complete: they capture your microphone, stream it to the agent bound by agent_id, play the agent’s replies, flush playback on barge-in, and end the session cleanly on Ctrl+C.
Deploy on another channel
The lifecycle above is identical everywhere — only how you authenticate and move audio changes:- From a browser — mint a short-lived token server-side so no API key is exposed; the browser handles mic capture and echo cancellation.
- Over the phone (Twilio) — bridge a Twilio call to your agent over G.711 μ-law with zero transcoding.
Next steps
Stream & play audio
Encodings, sending input, and playing output with clean interruptions.
WebSocket events
Every event and payload, with the session flow diagram.
Best practices
Tune turn-taking, latency, and reliability once it works.
Troubleshooting
Symptom-to-fix table for the common failures.