colossal-manchester-31772
08/28/2025, 12:09 AMtall-airline-16756
08/28/2025, 12:09 AMtall-airline-16756
08/28/2025, 12:09 AMAgentSession is created, you can’t set or change the TTS node. You’ll want to define the TTS node when you’re creating the Agent or AgentSession itself.
If you want to customize how the TTS behaves, a great way is to override the tts_node method in a custom Agent subclass. This lets you tweak or preprocess the text before it goes to the TTS engine. Here’s a quick example:
from livekit.agents.voice import Agent
from livekit.plugins import rime, openai
class CustomTtsAgent(Agent):
async def tts_node(self, text: AsyncIterable[str], model_settings):
async def process_text():
async for chunk in text:
# Modify text before TTS
modified_chunk = chunk.replace("lol", "<laugh>")
yield modified_chunk
return Agent.default.tts_node(self, process_text(), model_settings)
When you’re ready to start the session, just use your custom agent like this:
session = AgentSession()
await session.start(agent=CustomTtsAgent(), room=ctx.room)
If you have any questions or want to chat about a specific use case, I’m here to help!
Sources: examples/drive-thru/drivethru_agent.py | pipeline-tts/short_replies_only.py | Pipeline nodes and hooks | LiveKit Docs | Voice pipeline nodes | LiveKit Docs | Text-to-speech (TTS) integrations | LiveKit Docscolossal-manchester-31772
08/28/2025, 12:12 AMtall-airline-16756
08/28/2025, 12:12 AMtts_node and plug in your own custom TTS object instead of using Agent.default.tts_node. By overriding the tts_node method in your Agent subclass, you get full control to tailor the TTS pipeline exactly how you want.
Here’s a quick example of how you might use a custom TTS object directly:
from livekit.plugins import rime
class CustomTtsAgent(Agent):
async def tts_node(self, text: AsyncIterable[str], model_settings):
# Use a custom TTS object
custom_tts = rime.TTS(voice="custom-voice")
return custom_tts.stream(text, model_settings)
Just make sure your custom TTS object supports streaming through a stream() method that returns an async iterable of audio frames. If it doesn’t support streaming out of the box, you might need to wrap it with a stream adapter.
If you want me to walk you through this or if anything’s unclear, just give me a shout—I’m here to help!
Sources: examples/voice_agents/structured_output.py | Agent speech and audio | LiveKit Docs | pipeline-tts/short_replies_only.pycolossal-manchester-31772
08/28/2025, 12:13 AMtall-airline-16756
08/28/2025, 12:14 AMtts_node implementation:
Sources: livekit-agents/livekit/agents/voice/agent.py | pipeline-tts/tts_node.py | Pipeline nodes and hooks | LiveKit Docs | plugins/elevenlabs/src/tts.test.ts | ElevenLabs TTS integration guide | LiveKit Docscolossal-manchester-31772
08/28/2025, 3:47 AMtall-airline-16756
08/28/2025, 3:48 AMfrom livekit.agents import Agent
from livekit.plugins import elevenlabs, openai
class AgentA(Agent):
def __init__(self):
super().__init__(
tts=elevenlabs.TTS(voice_id="voice_id_A", model="eleven_multilingual_v2")
)
class AgentB(Agent):
def __init__(self):
super().__init__(
tts=openai.TTS(voice="shimmer")
)
2. Use Agent Handoff for Smooth Transitions
You can switch between agents dynamically using a function tool or by updating the session, like this:
from livekit.agents import function_tool
@function_tool
async def switch_to_agent_b(self, context):
return AgentB(), "Switching to Agent B."
Or simply update the session directly:
session.update_agent(AgentB())
3. Share Context Between Agents
To keep the conversation flowing naturally, pass along userdata or chat_ctx like this:
next_agent = AgentB(chat_ctx=context.session.chat_ctx)
4. Optional: Let Agents Hear Each Other
If you want your agents to pick up on each other’s speech, just configure RoomInputOptions like so:
room_input_options=RoomInputOptions(participant_kinds=[rtc.ParticipantKind.PARTICIPANT_KIND_AGENT])
5. A Quick Example
Imagine a storytelling app where a lead agent gathers user input and then hands off to specialist agents with their own unique voices depending on the story. For example:
class LeadAgent(Agent):
@function_tool
async def transfer_to_storyteller(self, context):
return StoryAgent(), "Let me connect you with our storyteller."
Does this sound like what you need? I’d be happy to help with a more detailed example tailored to your specific use case or preferred TTS provider!
Sources: multi-agent/long_or_short_agent.py | main.py | How to Enable Agent-to-Agent Speech Detection in LiveKit | agent/main.py | AI Agents Framework | livekit/livekit_composite | DeepWiki