agreeable-dream-41746
10/23/2025, 2:26 AMtall-airline-16756
10/23/2025, 2:27 AMtall-airline-16756
10/23/2025, 2:27 AMllm.handoff() method.
Also, turning on speculative execution with preemptive_generation can really help by overlapping processing time with user input, making things feel even snappier.
If you’d like, I’m here to walk you through some detailed examples or help out with any part of the setup—just let me know!
Sources: examples/src/comprehensive_test.ts | basics/say_in_voice.py | How can I reduce latency in voice agents using STT, TTS and LLM? | livekit-agents/livekit/agents/voice/agent_session.py | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik...agreeable-dream-41746
10/23/2025, 2:28 AMtall-airline-16756
10/23/2025, 2:29 AMupdate_options() for TTS or update_agent() for LLMs.
Let me share a simple example for TTS:
class SayPhraseInVoiceAgent(Agent):
def __init__(self):
super().__init__(
tts=inworld.TTS(voice="Ashley"), # Initial TTS voice
llm=openai.LLM(model="gpt-4.1-mini")
)
async def say_phrase_in_voice(self, phrase, voice="Hades"):
self.tts.update_options(voice=voice) # Switch TTS voice
await self.session.say(phrase)
self.tts.update_options(voice="Ashley") # Revert to default
When it comes to LLMs, you can also use update_agent() to switch models on the fly. For instance, you might start with OpenAI’s GPT-4.1 and later switch to Google’s Gemini depending on what fits your context or performance needs best.
If you'd like, I’d be happy to walk you through a detailed example of how to switch between two LLMs within a single agent. Just let me know!
Sources: README.md | Tool definition and use | LiveKit Docs | docs/index.yamlagreeable-dream-41746
10/23/2025, 2:30 AMtall-airline-16756
10/23/2025, 2:31 AMclass PrimaryAgent(Agent):
def __init__(self):
super().__init__(
llm=openai.LLM(model="gpt-4o"),
tts=elevenlabs.TTS(model="eleven_multilingual_v2")
)
@function_tool
async def switch_to_secondary(self):
return SecondaryAgent()
class SecondaryAgent(Agent):
def __init__(self):
super().__init__(
llm=google.LLM(model="gemini-1.5-pro"),
tts=cartesia.TTS(model="sonic-2")
)
@function_tool
async def hand_back_to_primary(self):
return self.session.userdata.agents["primary"]
This setup lets your agent start with one LLM and TTS, switch over to another, and then come back to the original configuration smoothly. If you’d like, I’m more than happy to walk you through it further or provide more examples—just let me know!
Sources: Workflows | LiveKit Docs | Introducing LiveKit Inference: A unified model interface for voice AI | complex-agents/personal_shopper/personal_shopper.pyagreeable-dream-41746
10/23/2025, 2:35 AMtall-airline-16756
10/23/2025, 2:35 AMclass PrimaryAgent(Agent):
def __init__(self):
super().__init__(
llm=openai.LLM(model="gpt-4o"),
tts=elevenlabs.TTS(model="eleven_multilingual_v2")
)
@function_tool
async def switch_to_secondary(self):
return SecondaryAgent()
2. Define the Secondary Agent: This agent uses a different LLM and TTS. It includes a function tool to hand back control to the primary agent.
class SecondaryAgent(Agent):
def __init__(self):
super().__init__(
llm=google.LLM(model="gemini-1.5-pro"),
tts=cartesia.TTS(model="sonic-2")
)
@function_tool
async def hand_back_to_primary(self):
return self.session.userdata.agents["primary"]
3. Store Agent References: To make the handoffs smooth and seamless, we use shared session state (like userdata) to keep references to both agents.
class UserData:
def __init__(self):
self.agents = {}
userdata = UserData()
userdata.agents["primary"] = PrimaryAgent()
4. Preserve Context: It’s important to maintain conversation flow when switching agents. You can do this by using the on_enter() method.
class BaseAgent(Agent):
async def on_enter(self):
if hasattr(self.session, "userdata"):
prev_agent = self.session.userdata.agents.get("previous")
if prev_agent:
self.session.copy_context_from(prev_agent)
5. Start the Session: Finally, initialize the session with the primary agent and allow it to switch dynamically.
async def entrypoint(ctx):
session = AgentSession(userdata=userdata)
primary = userdata.agents["primary"]
await session.start(agent=primary, room=ctx.room)
This setup ensures your agent can start with one LLM and TTS, switch to another, and then return to the original configuration, all while keeping the conversation context intact. If you’d like me to clarify anything further or provide additional examples, just let me know—I’m here to help!agreeable-dream-41746
10/23/2025, 2:36 AMtall-airline-16756
10/23/2025, 2:37 AMupdate_options method. This means you can change the TTS voice or model right in the middle of a conversation without needing to create or pass control to another agent.
On the other hand, switching between two LLM models dynamically within the same agent instance isn’t supported just yet. Instead, the framework asks you to create a new agent instance with the LLM configuration you want and then hand off control to it. This handoff is done using the llm.handoff() method, and you’ll find some helpful examples demonstrating this.
If you’d like, I’m more than happy to walk you through more details or share some examples on either switching TTS models or handling the LLM handoff. Just let me know!
Sources: Realtime model plugins | LiveKit Docs | avatars/hedra/dynamically_created_avatar/agent.py | examples/src/comprehensive_test.ts | tests/test_evals.py