How to switch TTS during agent runtime

This question originally came up in our Slack community and the thread has been consolidated here for long-term reference.

What is the recommended way to switch TTS of an agent during runtime?

I’m building a telephony AI agent and need to make an API call with the agent’s phone number to get the TTS to use. I can’t get the agent phone number from room.local_participant before connecting to the room, and I need to instantiate the Agent to connect using session.start().

It’s a chicken-and-egg problem. How can I change the TTS plugin during runtime?

Use session.update_agent() to switch to a different agent with the correct TTS configuration.

Solution:

  1. Start the session with an empty/placeholder Agent
  2. Get the agent phone number after connecting
  3. Determine which TTS to use based on the phone number
  4. Use update_agent() to set the correct agent with the proper TTS

See the documentation on setting the active agent:

Updating the agent does not affect the agent participant identity unless you explicitly change it.