Host the FastAPI bridge
run_realtime starts a FastAPI app that proxies between your Agency Swarm agents and a supported realtime provider (OpenAI or xAI). The helper already converts your agency to the realtime runtime, exposes a /realtime websocket, and streams events back to callers.
voice= to pick the voice for the whole call, or leave it out to use the entry agent’s voice—see one voice per call. Supply cors_origins when you deploy behind a browser client that runs on a different domain.
Serve voice endpoints from run_fastapi
Keep your existing REST endpoints and add realtime voice routes with one flag:
enable_realtime=True mounts /support/realtime alongside the normal JSON endpoints. Pass realtime_options when you want to override model settings (they map directly to run_realtime keyword arguments). Authentication and logging apply to the new websocket route automatically.
For xAI sessions:
?model=<model> because xAI selects voice models from the connection query string.
Serve the packaged browser client
A ready-made WebAudio frontend ships with the library. Launch it with your own agency in one call:examples/interactive/realtime/demo.py.
Twilio phone calls
Pass a Twilio number torun_realtime to expose a media-stream bridge. The helper exposes /incoming-call (returns TwiML) and /twilio/media-stream for bidirectional audio.
- Start the server and expose it publicly, e.g.
ngrok http 8000. - In the Twilio Console, set your phone number’s voice webhook to
https://<public-host>/incoming-call. - Call the number—the helper streams audio in both directions and reuses your existing tools and handoffs.
Store your Twilio account SID and auth token in a local
.env, export them before launching the demo, and keep OPENAI_API_KEY alongside them. The packaged server reads standard environment variables; no credentials live in source control.