creamy-tiger-14805
10/06/2025, 7:15 AMtall-airline-16756
10/06/2025, 7:15 AMcreamy-tiger-14805
10/06/2025, 7:15 AMclass AgentSession(rtc.EventEmitter[EventTypes], Generic[Userdata_T]):
def __init__(
self,
*,
turn_detection: NotGivenOr[TurnDetectionMode] = NOT_GIVEN,
stt: NotGivenOr[stt.STT] = NOT_GIVEN,
vad: NotGivenOr[vad.VAD] = NOT_GIVEN,
llm: NotGivenOr[llm.LLM | llm.RealtimeModel] = NOT_GIVEN,
tts: NotGivenOr[tts.TTS] = NOT_GIVEN,
mcp_servers: NotGivenOr[list[mcp.MCPServer]] = NOT_GIVEN,
userdata: NotGivenOr[Userdata_T] = NOT_GIVEN,
allow_interruptions: bool = True,
discard_audio_if_uninterruptible: bool = True,
min_interruption_duration: float = 0.5,
min_interruption_words: int = 0,
min_endpointing_delay: float = 0.4,
max_endpointing_delay: float = 6.0,
max_tool_steps: int = 3,
video_sampler: NotGivenOr[_VideoSampler | None] = NOT_GIVEN,
user_away_timeout: float | None = 15.0,
false_interruption_timeout: float | None = 2.0,
resume_false_interruption: bool = True,
min_consecutive_speech_delay: float = 0.0,
use_tts_aligned_transcript: NotGivenOr[bool] = NOT_GIVEN,
preemptive_generation: bool = False,
conn_options: NotGivenOr[SessionConnectOptions] = NOT_GIVEN,
loop: asyncio.AbstractEventLoop | None = None,
# deprecated
agent_false_interruption_timeout: NotGivenOr[float | None] = NOT_GIVEN,
) -> None:tall-airline-16756
10/06/2025, 7:15 AMAgentSession class in LiveKit, which plays a central role in managing voice AI applications. It gracefully handles the entire journey of a voice agent—from getting started, engaging in real-time conversations, to wrapping things up. It brings together powerful components like speech-to-text (STT), text-to-speech (TTS), large language models (LLM), and voice activity detection (VAD) to create smooth and interactive voice experiences.
Here are some important initialization parameters to keep in mind:
• stt, vad, llm, `tts`: These are optional instances you can provide for STT, VAD, LLM, and TTS components, tailored to your needs.
• `turn_detection`: This helps decide when a user has finished speaking, making interactions feel natural.
• `allow_interruptions`: Allows users to jump in even while the agent is speaking—handy for dynamic conversations.
• `userdata`: A place to store any custom data you'd like to associate with the session.
• `video_sampler`: Controls how often video is sampled based on user activity, optimizing performance.
Let’s look at the core methods you'll be using:
• `start()`: Kickstarts the session, and you can even link it with a real-time media room.
• `run()`: Starts a one-off interaction using user input, great for quick exchanges.
• `generate_reply()`: Prompts the agent to craft a response, keeping the conversation flowing.
• `aclose()`: Closes the session asynchronously and tidies up resources—important for clean shutdowns.
Some handy properties to be aware of:
• input and `output`: Manage your audio, video, and text streams smoothly.
• user_state and `agent_state`: Keep track of what’s happening with both the user and the agent.
• `history`: Gives you access to the entire conversation history, which is super useful.
• `current_speech`: Lets you control the agent’s ongoing speech if needed.
Beyond these, the class supports event handling and can be extended with external tools, all while being designed for easy testing. This makes it a flexible and reliable choice for a variety of real-time AI applications. If you have any questions or need help getting started, I’m here to support you every step of the way!
Sources: Models | LiveKit Docs | examples/warm-transfer/warm_transfer.py | docs/tools/check_agent_example_coverage.py | Agent Testing & CI/CD | livekit/livekit_composite | DeepWiki | Virtual avatar plugins | LiveKit Docscreamy-tiger-14805
10/06/2025, 7:17 AMturn_detection work?tall-airline-16756
10/06/2025, 7:18 AMturn_detection feature in AgentSession is designed to recognize when a user has finished speaking, making real-time voice conversations feel more natural and smooth. It combines Voice Activity Detection (VAD), speech-to-text (STT) signals, and optional specialized models to achieve this.
How It Works:
• Voice Activity Detection (VAD): This monitors the audio for speech and silence. The min_silence_duration parameter helps decide how long the silence should last before considering that the user has finished speaking.
• Turn Detection Modes: The turn_detection parameter offers several modes to fit different needs:
◦ `"vad"`: Uses VAD to detect when a user’s turn ends.
◦ `"stt"`: Uses cues from the speech-to-text results to identify the end of an utterance.
◦ `"realtime_llm"`: Employs server-side detection with real-time LLMs for turn endings.
◦ `"manual"`: Turns off automatic detection, giving you manual control through methods like commit_user_turn().
◦ Custom models (such as MultilingualModel()): These are context-aware and trained specifically to detect when a turn ends.
• Automatic Fallback: If you don’t choose a mode explicitly, LiveKit smartly picks the best one based on your session setup.
• Endpointing Delays: Once a turn end is detected, the system waits just the right amount of time before responding:
◦ `min_endpointing_delay`: A short delay for high-confidence detections (default is 0.5 seconds).
◦ `max_endpointing_delay`: A longer delay for lower-confidence detections (default is 6.0 seconds).
Custom Turn Detection Models:
LiveKit also offers a plugin that lets you use custom turn detection models with open weights. These support multiple languages and provide high accuracy. You can integrate them with STT engines and real-time LLMs for even more advanced detection capabilities.
If you’d like, I’m happy to share some examples or walk you through implementing any specific mode — just let me know!
Sources: Getting Help & Troubleshooting | livekit/livekit_composite | DeepWiki | How does end-of-utterance detection work in conversations? | examples/voice_agents/push_to_talk.py | Turn detection and interruptions | LiveKit Docs