facing latency in agent response generation what s...
# ask-ai
d
facing latency in agent response generation what steps can be taken to decrease it or have some keyword buzz in if goes in thinking mode something like that give example with code?
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.
Hey there! To help you reduce latency in agent response generation and set up keyword buzz, here’s a friendly step-by-step guide: 1. Reducing Latency: • Try enabling preemptive generation so the LLM and TTS processes kick off as soon as partial transcription is ready. Here’s a quick example:
Copy code
from livekit.agents import AgentSession

session = AgentSession(
    stt=your_stt_instance,
    llm=your_llm_instance,
    tts=your_tts_instance,
    preemptive_generation=True
)
• Also, optimizing network proximity can really help cut down round-trip time for API calls. • Don’t forget to use the UsageCollector to keep an eye on latency metrics like Time To First Token (TTFT). 2. Implementing Keyword Buzz: • Keep an eye on transcription streams and trigger actions whenever certain keywords pop up. Here’s how you might do it:
Copy code
async def on_transcribed(data):
    transcript = data.alternatives[0].text.lower()
    if "urgent" in transcript:
        await session.speech.handle.interrupt()
        await session.say("I understand this is urgent. How can I assist immediately?")

session.on("user_input_transcribed", on_transcribed)
• Or, you can use the
on_user_turn_completed
event to check full transcripts and respond accordingly. If you want, I’m here to help with more examples or tailor things specifically for your setup. Just let me know! Sources: livekit-agents/livekit/agents/voice/agent_session.py | Voice Assistants | livekit/livekit_composite | DeepWiki | How can I reduce latency in voice agents using STT, TTS and LLM? | Agent speech and audio | LiveKit Docs