Skip to main content
Once you’ve created an agent, you connect to it by opening a realtime WebSocket and referencing its 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:
  1. Open the WebSocket (with your API key, or a browser token).
  2. Bind to your agent by sending one session.update with its agent_id. The stored prompt, voice, and tools load automatically, so you don’t resend them.
  3. Wait for session.ready — the signal the agent is live.
  4. Stream microphone audio as input.audio frames and play the agent’s reply.audio frames.
  5. End cleanly with session.end when the conversation is over.
Always send session.end before closing the WebSocket. If the client just closes the socket, the server holds the session open for a 30-second session.resume grace window, and that window is billable. session.end short-circuits the grace window, emits session.ended, and stops billing immediately.
Send session.end on every intentional disconnect: “End call” button, page unload (beforeunload / pagehide), user hangup, and Ctrl+C in server clients. See Ending the session cleanly.
Step 2 is the whole binding — send the 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 the Authorization 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.
This passes your raw API key over the connection, which is fine for servers and trusted native clients. Never ship your API key in browser or mobile client code. For client-side apps, use the browser integration token flow.

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.