in agent pipeline agent after each response from u...
# ask-ai
d
in agent pipeline agent after each response from user goes in thinking state adding to latency how to improvise it
t
Thanks for reaching out, Giriraj. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help your agent respond more quickly when it’s in that “thinking” moment after you speak, here are some friendly tips you might find useful: • Try Enabling Preemptive Speech Generation: This handy
preemptive_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.py
b
How can we implement this => 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.
t
When things take a bit longer to process, it’s nice to keep users in the loop and engaged. Here are some friendly ways you can do that: • Predefined Thinking Messages: Simple, reassuring messages like "Let me look that up..." or "One moment while I check..." work great. Here’s a quick example:
Copy code
thinking_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:
Copy code
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:
Copy code
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`:
Copy code
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