dazzling-greece-44016
08/22/2025, 10:47 AMtall-airline-16756
08/22/2025, 10:47 AMtall-airline-16756
08/22/2025, 10:48 AMpreemptive_generation setting lets your agent start crafting its response even before you finish talking. For example: session = AgentSession(preemptive_generation=True).
• Optimize Your Setup: Keeping your agent’s servers close to the LLM, STT, and TTS services can really cut down on waiting time. Also, using streaming APIs and parallel processing where possible helps speed things up.
• Tweak the Endpointing Delay: The min_endpointing_delay is set to 500ms by default, but you can adjust it to make responses quicker—just a heads-up, this might cause a few more interruptions.
• Manage Longer Processing Times: When things get complex, you can keep users engaged with predefined “thinking” messages, dynamic LLM responses, or even some audio playback to fill the wait.
• Enhance the Frontend Experience: Adding visual or audio cues that show when the agent is listening, thinking, or speaking can make the whole interaction feel smoother and more natural.
Out of all these, turning on preemptive generation and fine-tuning your infrastructure usually make the biggest difference in cutting down latency.
Sources: agent_extensions/agent_extensions/rag/handler.py | Events and error handling | LiveKit Docs | Web and mobile frontends | LiveKit Docs | Agent speech and audio | LiveKit Docs | tests/test_agent_session.pybig-restaurant-34425
08/26/2025, 9:27 AMtall-airline-16756
08/26/2025, 9:28 AMthinking_messages = [
"Let me look that up...",
"One moment while I check...",
"Looking into that now..."
]
await agent.say(random.choice(thinking_messages))
• Dynamic LLM Responses: You can also have the LLM create short, context-aware “thinking” messages on the fly. Like this:
thinking_ctx = llm.ChatContext().append(
role="system",
text="Generate a very short message to indicate that we're looking up the answer in the docs"
)
thinking_stream = agent._llm.chat(chat_ctx=thinking_ctx)
await agent.say(thinking_stream, add_to_chat_ctx=False)
• Audio Playback: Adding some sound can make waiting feel less dull—try playing pre-recorded audio or background noises during those moments. For example:
await play_wav_once("let_me_check_that.wav", ctx.room)
Or, if you want continuous sounds like keyboard typing to keep things lively, you can use `BackgroundAudioPlayer`:
background_audio = BackgroundAudioPlayer(
thinking_sound=[
AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING2, volume=0.7),
],
)
await background_audio.start(room=ctx.room, agent_session=session)
Feel free to mix and match these ideas to make the wait feel smooth and natural for your users!
Sources: delay-handling/agent.py | External data and RAG | LiveKit Docs