can i have a single agent with 2 llm models; and 2...
# ask-ai
a
can i have a single agent with 2 llm models; and 2 tts models; it would start by generating text with a fast tts/llm then with a slower one such that latency isn't felt?
t
Thanks for reaching out, Ali. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can set up a single agent to use two LLM models and two TTS models by dynamically switching between them as it runs. This method helps keep things speedy by starting with fast, low-latency models for initial responses, then moving to higher-quality, slightly slower models when needed. Here’s a simple way to think about it: • Begin with fast models (like Cartesia for TTS and GPT-4.1 mini for LLM) to get quick responses going. • Then, switch to higher-quality models (such as ElevenLabs for TTS and GPT-4o for LLM) during the conversation using function tools and the
llm.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...
a
doesn't llm/handoff() or else hands it off to another agent?! give me an example of just one agent; 2 llm models; 2 tts models
t
I'm glad you asked! A single agent can actually use two LLM models and two TTS models dynamically without needing to hand off to another agent. This is done by updating its configuration during runtime, using handy methods like
update_options()
for TTS or
update_agent()
for LLMs. Let me share a simple example for TTS:
Copy code
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.yaml
a
but i want this agent to start speaking with one llm then with another llm/tts before handing back to user
t
Hi there! To create an agent that begins by speaking with one LLM and TTS, then smoothly switches to another LLM and TTS before handing things back to the user, you can use a circular handoff mechanism. This approach lets you dynamically switch between configurations while keeping the context intact. Here’s a simple example to illustrate:
Copy code
class 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.py
a
walk me through it further
t
I'd love to guide you through a detailed walkthrough on how to configure an agent that starts with one LLM and TTS, switches to another, and then hands back to the user. Let's take it step by step: 1. Define the Primary Agent: This agent uses the initial LLM and TTS models. It includes a function tool to switch to the secondary agent.
Copy code
class 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.
Copy code
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.
Copy code
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.
Copy code
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.
Copy code
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!
a
but now you're switching between 2 agents! i thought you were using just one agent!
t
Great question! You can absolutely have a single agent switch between two TTS models on the fly by updating the TTS configuration using the
update_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