Is it possibile to start a session with a realtime...
# ask-ai
f
Is it possibile to start a session with a realtime agent (possible nova sonic), then switch to a voice pipeline agent once it enters a specific node?
t
Thanks for reaching out, Muthana. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It is possible to start a session with a realtime agent (such as Nova Sonic) and then switch to a voice pipeline agent when the session enters a specific node. LiveKit supports agent handoff within a session using several mechanisms: • Function tool-based handoff: You can define function tools that, when invoked, return a new agent instance, automatically transferring control. This lets the logic of your workflow (such as reaching a certain node or condition) trigger the agent switch. • Direct agent switching: You can mix realtime agents (like Nova Sonic) and traditional voice pipeline agents in the same session. Each agent can be customized with its own models and instructions, and you can swap between agents depending on your workflow. • Node-based customization: The framework allows you to define transitions at specific nodes. Nodes represent points where different processing (STT-LLM-TTS pipeline or realtime) can be swapped in or out. You can use lifecycle hooks (such as
on_enter
or
on_exit
) to control behavior before and after a switch. An example pattern is to have your realtime agent (Nova Sonic) invoke a function (e.g.,
switch_to_pipeline
) when the target node is reached, returning the new pipeline agent and effectively handing off control. User context and chat state can be preserved between agents, supporting a seamless experience. Sources: Building voice agents | LiveKit Docs | Agents 0.x migration guide | LiveKit Docs | Voice pipeline nodes | LiveKit Docs | Workflows | LiveKit Docs | examples/voice_agents/restaurant_agent.py | complex-agents/shopify-voice-shopper/shopify.py
f
show me an example of node-based customization
t
Hey there! Let me walk you through a super cool way to customize your agents in LiveKit. Think of this like giving your agent a personal touch when it starts or switches roles. It's kind of like how you might greet someone when you walk into a room or say goodbye when you're leaving.
Copy code
class VoicePipelineAgent(Agent):
    def __init__(self):
        super().__init__(
            instructions="You are now using a voice pipeline",
            stt=deepgram.STT(),
            llm=openai.LLM(),
            tts=cartesia.TTS(),
        )

    async def on_enter(self):
        # Custom logic when this agent becomes active
        await self.session.say("I've switched to the voice pipeline agent")

    async def on_exit(self):
        # Custom logic before handing off to another agent
        await self.session.say("Exiting the voice pipeline agent")
on_enter
is like your agent's warm welcome moment - it runs when the agent starts up. You can set up initial states, transfer context, or just say a friendly hello! •
on_exit
is the agent's polite goodbye, happening right before switching to a new agent. Perfect for tidying up, saving important info, or sending a final message. Pretty neat, right? This approach lets you create super smooth, personalized transitions between different stages of your agent workflow. It's all about making your AI interactions feel more natural and responsive! Sources: Voice pipeline nodes | LiveKit Docs | complex-agents/medical_office_triage/triage.py | examples/voice_agents/restaurant_agent.py